#ifndef MSP_FRAMEWORK_SEMAPHORE_H_
#define MSP_FRAMEWORK_SEMAPHORE_H_
-#include <pthread.h>
#include "mutex.h"
+#include "types.h"
#include "../time/timedelta.h"
namespace Msp {
{
public:
#ifndef WIN32
- Semaphore() { pthread_cond_init(&cond, 0); }
+ Semaphore() { pthread_cond_init(&sem, 0); }
//Mutex &get_mutex() { return mutex; }
- int signal() { MutexLock l(mutex); return pthread_cond_signal(&cond); }
- int broadcast() { MutexLock l(mutex); return pthread_cond_broadcast(&cond); }
- int wait() { mutex.lock(); return pthread_cond_wait(&cond, &mutex.mutex); }
+ int signal() { MutexLock l(mutex); return pthread_cond_signal(&sem); }
+ int broadcast() { MutexLock l(mutex); return pthread_cond_broadcast(&sem); }
+ int wait() { mutex.lock(); return pthread_cond_wait(&sem, &mutex.mutex); }
int wait(const Time::TimeDelta &);
- ~Semaphore() { pthread_cond_destroy(&cond); }
+ ~Semaphore() { pthread_cond_destroy(&sem); }
+#else
+ Semaphore() { sem=CreateSemaphore(0, 0, 32, 0); }
+ int signal() { return !ReleaseSemaphore(sem, 1, 0); }
+ int broadcast() { return !ReleaseSemaphore(sem, 32, 0); }
+ int wait() { return WaitForSingleObject(sem, INFINITE)==WAIT_OBJECT_0; }
+ int wait(const Time::TimeDelta &);
+ ~Semaphore() { CloseHandle(sem); }
#endif
private:
+#ifndef WIN32
Mutex mutex;
- pthread_cond_t cond;
+#endif
+ SemaphoreHandle sem;
};
}