soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SSemaphore.cpp
1#include <helper/SSemaphore.h>
2#include <windows.h>
3#include <cerrno>
4
5SNSBEGIN
6
7class SemaphorePrivate
8{
9public:
10 HANDLE _hsem;
11};
12
14 _private(*(new SemaphorePrivate()))
15{
16 _private._hsem = ::CreateSemaphore(NULL, 0, 65535/* we need to discuss this max value*/, NULL);
17}
18
20{
21 ::CloseHandle(_private._hsem);
22 delete &_private;
23}
24
26{
27 DWORD ret = ::WaitForSingleObject (_private._hsem, INFINITE);
28
29 if ( ret == WAIT_OBJECT_0)
30 {
31 return RETURN_OK;
32 }
33
34 return RETURN_ERROR; // This is a blocking wait, so any value other than WAIT_OBJECT_0 indicates an error!
35}
36
37int SSemaphore::wait(unsigned int msec)
38{
39 DWORD ret = ::WaitForSingleObject (_private._hsem, msec);
40
41 if ( ret == WAIT_OBJECT_0)
42 {
43 return RETURN_OK;
44 }
45
46 // This is a timed wait, so WAIT_TIMEOUT value must be checked!
47 if ( ret == WAIT_TIMEOUT )
48 {
49 return RETURN_TIMEOUT;
50 }
51
52 return RETURN_ERROR;
53}
54
56{
57 ::ReleaseSemaphore(_private._hsem, 1, NULL);
58}
59SNSEND
int wait()
Waits indefinitely for the semaphore to be notified.
virtual ~SSemaphore()
Destructor. Cleans up the semaphore resources.
void notify()
Notifies the semaphore.
SSemaphore()
Default constructor. Initializes the semaphore.