Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Apply the default settings of 'smpi/buffering' too
[simgrid.git] / src / xbt / OsSemaphore.hpp
1 /* Copyright (c) 2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <xbt/base.h>
7
8 #include <condition_variable>
9 #include <mutex>
10
11 namespace simgrid {
12 namespace xbt {
13 class XBT_PUBLIC OsSemaphore {
14 public:
15   explicit inline OsSemaphore(unsigned int capa) : capa_(capa) {}
16
17   inline void acquire()
18   {
19     std::unique_lock<std::mutex> lock(mutex_);
20     condition_.wait(lock, [this]() { return capa_ > 0; });
21     --capa_;
22   }
23
24   inline void release()
25   {
26     std::unique_lock<std::mutex> lock(mutex_);
27     ++capa_;
28     condition_.notify_one();
29   }
30
31 private:
32   unsigned int capa_;
33   std::mutex mutex_;
34   std::condition_variable condition_;
35 };
36 } // namespace xbt
37 } // namespace simgrid