File: BinarySemaphoreLinux.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (192 lines) | stat: -rw-r--r-- 4,634 bytes parent folder | download | duplicates (24)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Modification History
 *
 * 2001-January-11		Jason Rohrer
 * Created.
 *
 * 2003-August-26   Jason Rohrer
 * Added support for timeouts on wait.
 *
 * 2006-February-28   Jason Rohrer
 * Fixed bug in sub-second timeout computation.
 */
 
#include "minorGems/system/BinarySemaphore.h"
#include "minorGems/system/Time.h"
#include <pthread.h>


/**
 * Linux-specific implementation of the BinarySemaphore class member functions.
 *
 * May also be compatible with other POSIX-like systems.
 *
 * To compile:
 * g++ -lpthread
 */

/**
 * Native object pointer A is the condition variable.
 * Pointer B is the mutex that must go along with it.
 */


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

	// allocate a condition variable structure on the heap
	mNativeObjectPointerA = (void *)( new pthread_cond_t[1] );
	
	// get a pointer to the cond
	pthread_cond_t *condPointer = 
		(pthread_cond_t *)mNativeObjectPointerA;
	
	// init the cond
	pthread_cond_init( &( condPointer[0] ), NULL );
	
	
	// allocate a mutex structure on the heap
	mNativeObjectPointerB = (void *)( new pthread_mutex_t[1] );
	
	// get a pointer to the mutex
	pthread_mutex_t *mutexPointer = 
		(pthread_mutex_t *)mNativeObjectPointerB;
	
	// init the mutex
	pthread_mutex_init( &( mutexPointer[0] ), NULL );
	
	}

BinarySemaphore::~BinarySemaphore() {
	
	// get a pointer to the cond
	pthread_cond_t *condPointer = 
		(pthread_cond_t *)mNativeObjectPointerA;
	
	// destroy the cond
	pthread_cond_destroy( &( condPointer[0] ) );
	
	// de-allocate the cond structure from the heap
	delete [] condPointer;
	
	
	// get a pointer to the mutex
	pthread_mutex_t *mutexPointer = 
		(pthread_mutex_t *)mNativeObjectPointerB;
	
	// destroy the mutex	
	pthread_mutex_destroy( &( mutexPointer[0] ) );
	
	// de-allocate the mutex structure from the heap
	delete [] mutexPointer;
	}



int BinarySemaphore::wait( int inTimeoutInMilliseconds ) {

    int returnValue = 1;
    
	// get a pointer to the cond
	pthread_cond_t *condPointer = 
		(pthread_cond_t *)mNativeObjectPointerA;
	
	// get a pointer to the mutex
	pthread_mutex_t *mutexPointer = 
		(pthread_mutex_t *)mNativeObjectPointerB;
	
	
	// lock the mutex
	pthread_mutex_lock( &( mutexPointer[0] ) );
	
	if( mSemaphoreValue == 0 ) {
		// wait on condition variable, which automatically unlocks
		// the passed-in mutex

        if( inTimeoutInMilliseconds == -1 ) {
            // no timeout
            pthread_cond_wait( &( condPointer[0] ), &( mutexPointer[0] ) );
            }
        else {
            // use timeout version

            unsigned long nsecPerSecond = 1000000000;
            unsigned long nsecPerMillisecond = 1000000;


            unsigned long currentSec;
            unsigned long currentMS;

            Time::getCurrentTime( &currentSec, &currentMS );

            unsigned long currentNS = currentMS * nsecPerMillisecond;

            
            long timeoutSec = inTimeoutInMilliseconds / 1000;
            long extraMS = inTimeoutInMilliseconds % 1000;

            long extraNS = extraMS * nsecPerMillisecond;

            

            unsigned long absTimeoutSec = currentSec + timeoutSec;
            unsigned long absTimeoutNsec = currentNS + extraNS;

            // check for nsec overflow
            if( absTimeoutNsec > nsecPerSecond ) {
                absTimeoutSec += 1;
                absTimeoutNsec -= nsecPerSecond;
                }
            
            struct timespec abstime;
            abstime.tv_sec = absTimeoutSec;
            abstime.tv_nsec = absTimeoutNsec;

            int result = pthread_cond_timedwait( &( condPointer[0] ),
                                                 &( mutexPointer[0] ),
                                                 &abstime );

            if( result != 0 ) {
                // timed out
                returnValue = 0;
                }
            }
        
		// mutex is apparently re-locked when we return from cond_wait
		
		}
		
	// decrement the semaphore value
	mSemaphoreValue = 0;
	
	// unlock the mutex again
	pthread_mutex_unlock( &( mutexPointer[0] ) );

    return returnValue;
	}



void BinarySemaphore::signal() {
	
	// get a pointer to the cond
	pthread_cond_t *condPointer = 
		(pthread_cond_t *)mNativeObjectPointerA;
	
	// get a pointer to the mutex
	pthread_mutex_t *mutexPointer = 
		(pthread_mutex_t *)mNativeObjectPointerB;
	
	
	// lock the mutex
	pthread_mutex_lock( &( mutexPointer[0] ) );
	
	// increment the semaphore value
	mSemaphoreValue = 1;
	
	pthread_cond_signal( &( condPointer[0] ) );
		
		
	// unlock the mutex
	pthread_mutex_unlock( &( mutexPointer[0] ) );
	}