You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exit_handle.cc 1.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/exit_handle.h"
  17. #include <signal.h>
  18. #include <utility>
  19. namespace mindspore {
  20. namespace serving {
  21. ExitHandle &ExitHandle::Instance() {
  22. static ExitHandle instance;
  23. return instance;
  24. }
  25. void ExitHandle::InitSignalHandle() {
  26. if (!has_inited_.test_and_set()) {
  27. signal(SIGINT, HandleSignal);
  28. signal(SIGTERM, HandleSignal);
  29. }
  30. }
  31. void ExitHandle::MasterWait() {
  32. InitSignalHandle();
  33. auto exit_future = master_exit_requested_.get_future();
  34. exit_future.wait();
  35. }
  36. void ExitHandle::WorkerWait() {
  37. InitSignalHandle();
  38. auto exit_future = worker_exit_requested_.get_future();
  39. exit_future.wait();
  40. }
  41. void ExitHandle::Stop() { HandleSignal(0); }
  42. bool ExitHandle::HasStopped() { return is_exit_; }
  43. void ExitHandle::HandleSignal(int sig) {
  44. auto &instance = Instance();
  45. if (!instance.has_exited_.test_and_set()) {
  46. instance.master_exit_requested_.set_value();
  47. instance.worker_exit_requested_.set_value();
  48. instance.is_exit_.store(true);
  49. }
  50. }
  51. } // namespace serving
  52. } // namespace mindspore

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.