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.

service.cc 2.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Copyright 2019 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 "dataset/util/service.h"
  17. #include <thread>
  18. namespace mindspore {
  19. namespace dataset {
  20. Status Service::ServiceStart() {
  21. do {
  22. UniqueLock lck(&state_lock_);
  23. // No-op if it is already up or some other thread is
  24. // in the process of bring it up.
  25. if (state_ == STATE::kRunning || state_ == STATE::kStartInProg) {
  26. return Status::OK();
  27. }
  28. // If a stop is in progress, we line up after it
  29. // is done.
  30. if (state_ == STATE::kStopInProg) {
  31. std::this_thread::yield();
  32. } else {
  33. state_ = STATE::kStartInProg;
  34. // At this point, we will let go of the lock. This allow others to proceed.
  35. lck.Unlock();
  36. RETURN_IF_NOT_OK(DoServiceStart());
  37. // Lock again to change state.
  38. lck.Lock();
  39. state_ = STATE::kRunning;
  40. return Status::OK();
  41. }
  42. } while (true);
  43. }
  44. Status Service::ServiceStop() noexcept {
  45. do {
  46. UniqueLock lck(&state_lock_);
  47. // No-op if it is already stopped or some other thread is
  48. // in the process of shutting it down
  49. if (state_ == STATE::kStopped || state_ == STATE::kStopInProg) {
  50. return Status::OK();
  51. }
  52. // If a start is in progress, we line up after it
  53. // is done.
  54. if (state_ == STATE::kStartInProg) {
  55. std::this_thread::yield();
  56. } else {
  57. state_ = STATE::kStopInProg;
  58. // At this point, we will let go of the lock. This allows others to proceed.
  59. lck.Unlock();
  60. RETURN_IF_NOT_OK(DoServiceStop());
  61. // Lock again to change state.
  62. lck.Lock();
  63. state_ = STATE::kStopped;
  64. return Status::OK();
  65. }
  66. } while (true);
  67. }
  68. } // namespace dataset
  69. } // namespace mindspore