File: BinarySemaphoreWin32.cpp

package info (click to toggle)
passage 4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,692 kB
  • sloc: cpp: 18,864; objc: 272; sh: 134; makefile: 117; perl: 67; ansic: 44
file content (94 lines) | stat: -rw-r--r-- 2,063 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Modification History
 *
 * 2001-January-27		Jason Rohrer
 * Created.
 *
 * 2001-March-4		Jason Rohrer
 * Replaced include of <winbase.h> and <windef.h> with <windows.h> 
 * to fix compile bugs encountered with newer windows compilers. 
 *
 * 2003-August-26   Jason Rohrer
 * Added support for timeouts on wait.
 */
 
#include "minorGems/system/BinarySemaphore.h"

#include <windows.h>


/**
 * Win32-specific implementation of the BinarySemaphore class member functions.
 */


/**
 * Native object pointer A is the semaphore handle.
 * Pointer B is not used.
 */


BinarySemaphore::BinarySemaphore() :
	mSemaphoreValue( 0 ) {

	// allocate a handle on the heap
	mNativeObjectPointerA = (void *)( new HANDLE[1] );
	
	// retrieve handle from the heap
	HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
	
	
	semaphorePointer[0] = CreateSemaphore(  
		(LPSECURITY_ATTRIBUTES) NULL,	// no attributes
		0,		// initial count
		1,		// maximum count
		(LPCTSTR) NULL );		// no name
	
	}

BinarySemaphore::~BinarySemaphore() {
	
	// retrieve handle from the heap
	HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
	
	// destroy the semaphore	
	CloseHandle( semaphorePointer[0] );
	
	// de-allocate the handle from the heap
	delete [] semaphorePointer;
	}



int BinarySemaphore::wait( int inTimeoutInMilliseconds ) {
	
	// retrieve handle from the heap
	HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;

    if( inTimeoutInMilliseconds == -1 ) {
        WaitForSingleObject( semaphorePointer[0], INFINITE );
        return 1;
        }
    else {
        // timeout
        int result = WaitForSingleObject( semaphorePointer[0],
                                          inTimeoutInMilliseconds );

        if( result == WAIT_TIMEOUT ) {
            return 0;
            }
        else {
            return 1;
            }
        }
	}



void BinarySemaphore::signal() {
	
	// retrieve handle from the heap
	HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
	
	ReleaseSemaphore( semaphorePointer[0], 1, (LPLONG) NULL );
	}