Signed-off-by: Sage Weil <sage@redhat.com>
#ifndef CEPH_Sem_Posix__H
#define CEPH_Sem_Posix__H
+#include "common/ceph_mutex.h"
+
class Semaphore
{
- Mutex m;
- Cond c;
- int count;
+ ceph::mutex m = ceph::make_mutex("Semaphore::m");
+ ceph::condition_variable c;
+ int count = 0;
public:
- Semaphore() : m("Semaphore::m")
- {
- count = 0;
- }
-
void Put()
{
- m.lock();
+ std::lock_guard l(m);
count++;
- c.Signal();
- m.unlock();
+ c.notify_all();
}
void Get()
- {
- m.lock();
+ {
+ std::unique_lock l(m);
while(count <= 0) {
- c.Wait(m);
+ c.wait(l);
}
count--;
- m.unlock();
}
};