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.

cond_var.h 1.8 kB

4 years ago
4 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Copyright 2019-2021 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 MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_COND_VAR_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_COND_VAR_H_
  18. #include <condition_variable>
  19. #include <functional>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include "minddata/dataset/util/intrp_resource.h"
  24. #include "minddata/dataset/util/intrp_service.h"
  25. #include "minddata/dataset/util/status.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. class CondVar : public IntrpResource {
  29. public:
  30. CondVar();
  31. ~CondVar() noexcept;
  32. Status Wait(std::unique_lock<std::mutex> *lck, const std::function<bool()> &pred);
  33. /// Timed sleep.
  34. /// \param lck lock
  35. /// \param duration time to sleep in ms
  36. /// \return Status code
  37. Status WaitFor(std::unique_lock<std::mutex> *lck, int64_t duration);
  38. void Interrupt() override;
  39. void NotifyOne() noexcept;
  40. void NotifyAll() noexcept;
  41. Status Register(std::shared_ptr<IntrpService> svc);
  42. std::string my_name() const;
  43. Status Deregister();
  44. protected:
  45. std::condition_variable cv_;
  46. std::shared_ptr<IntrpService> svc_;
  47. private:
  48. std::string my_name_;
  49. };
  50. } // namespace dataset
  51. } // namespace mindspore
  52. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_COND_VAR_H_