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.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "minddata/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. // Call the real implementation from the derived class.
  37. Status rc = DoServiceStart();
  38. // If we hit any error, change the state back into the initial state.
  39. // It is possible that the user may want to drive a clean up by calling
  40. // ServiceStop but if it will end up in a loop because of the state is still
  41. // kStartInProg.
  42. if (rc.IsError()) {
  43. lck.Lock();
  44. state_ = STATE::kStopped;
  45. lck.Unlock();
  46. return rc;
  47. }
  48. // Lock again to change state.
  49. lck.Lock();
  50. state_ = STATE::kRunning;
  51. return Status::OK();
  52. }
  53. } while (true);
  54. }
  55. Status Service::ServiceStop() noexcept {
  56. do {
  57. UniqueLock lck(&state_lock_);
  58. // No-op if it is already stopped or some other thread is
  59. // in the process of shutting it down
  60. if (state_ == STATE::kStopped || state_ == STATE::kStopInProg) {
  61. return Status::OK();
  62. }
  63. // If a start is in progress, we line up after it
  64. // is done.
  65. if (state_ == STATE::kStartInProg) {
  66. std::this_thread::yield();
  67. } else {
  68. state_ = STATE::kStopInProg;
  69. // At this point, we will let go of the lock. This allows others to proceed.
  70. lck.Unlock();
  71. RETURN_IF_NOT_OK(DoServiceStop());
  72. // Lock again to change state.
  73. lck.Lock();
  74. state_ = STATE::kStopped;
  75. return Status::OK();
  76. }
  77. } while (true);
  78. }
  79. } // namespace dataset
  80. } // namespace mindspore