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.

semaphore.h 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef DATASET_UTIL_SEMAPHORE_H_
  17. #define DATASET_UTIL_SEMAPHORE_H_
  18. #include "dataset/util/cond_var.h"
  19. namespace mindspore {
  20. namespace dataset {
  21. class TaskGroup;
  22. /// \brief A counting semaphore. There are two external functions P and V. P decrements the internal count and will be
  23. /// blocked if the count is 0 (zero). V increments the internal count and wake up one of the waiters.
  24. class Semaphore {
  25. public:
  26. /// \brief Constructor
  27. /// \param init Initial value of the internal counter.
  28. explicit Semaphore(int init) : value_(init) {}
  29. virtual ~Semaphore() {}
  30. /// \brief Decrement the internal counter. Will be blocked if the value is 0.
  31. /// \return Error code. Can get interrupt.
  32. Status P();
  33. /// \brief Increment the internal counter. Wakeup on of the watiers if any.
  34. void V();
  35. /// \brief Peek the internal value
  36. /// \return The internal value
  37. int Peek();
  38. Status Register(TaskGroup *vg);
  39. Status Deregister();
  40. void ResetIntrpState();
  41. private:
  42. int value_;
  43. std::mutex mutex_;
  44. CondVar wait_cond_;
  45. };
  46. } // namespace dataset
  47. } // namespace mindspore
  48. #endif // DATASET_UTIL_SEMAPHORE_H_