1313
1414#include <errno.h>
1515
16- typedef struct {
16+ typedef struct
17+ {
1718int m_numSems ;
18- off_t m_semaphoreHandles ;// offset from beginning of header
19- off_t m_semaphoreCounts ;// offset from beginning of header
20- }win32_sem_set_hdr ;
19+ off_t m_semaphoreHandles ;
20+ //offset from beginning of header
21+ off_t m_semaphoreCounts ;
22+ //offset from beginning of header
23+ }win32_sem_set_hdr ;
2124
2225/* Control of a semaphore pool. The pool is an area in which we stored all
2326** the semIds of the pool. The first long is the number of semaphore
@@ -27,30 +30,32 @@ typedef struct {
2730int
2831semctl (int semId ,int semNum ,int flag ,union semun semun )
2932{
30- win32_sem_set_hdr * the_set = (win32_sem_set_hdr * ) MAKE_PTR (semId );
33+ win32_sem_set_hdr * the_set = (win32_sem_set_hdr * ) MAKE_PTR (semId );
3134
3235/* semNum might be 0 */
3336/* semun.array contains the sem initial values */
34- int * sem_counts = (int * ) ((off_t )the_set + the_set -> m_semaphoreCounts );
37+ int * sem_counts = (int * ) ((off_t )the_set + the_set -> m_semaphoreCounts );
3538
3639/* Fix the count of all sem of the pool to semun.array */
3740if (flag == SETALL )
3841{
39- int i ;
42+ int i ;
4043struct sembuf sops ;
44+
4145sops .sem_flg = IPC_NOWAIT ;
4246
43- for (i = 0 ;i < the_set -> m_numSems ;++ i ) {
47+ for (i = 0 ;i < the_set -> m_numSems ;++ i )
48+ {
4449if (semun .array [i ]== sem_counts [i ])
45- continue ;/* Nothing to do */
50+ continue ;/* Nothing to do */
4651
4752if (semun .array [i ]< sem_counts [i ])
4853sops .sem_op = -1 ;
4954else
5055sops .sem_op = 1 ;
5156
5257sops .sem_num = i ;
53-
58+
5459/* Quickly lock/unlock the semaphore (if we can) */
5560if (semop (semId ,& sops ,1 )< 0 )
5661return -1 ;
@@ -61,16 +66,18 @@ semctl(int semId, int semNum, int flag, union semun semun)
6166/* Fix the count of one semaphore to semun.val */
6267else if (flag == SETVAL )
6368{
64- if (semun .val != sem_counts [semNum ]) {
69+ if (semun .val != sem_counts [semNum ])
70+ {
6571struct sembuf sops ;
72+
6673sops .sem_flg = IPC_NOWAIT ;
6774sops .sem_num = semNum ;
6875
6976if (semun .val < sem_counts [semNum ])
7077sops .sem_op = -1 ;
7178else
7279sops .sem_op = 1 ;
73-
80+
7481/* Quickly lock/unlock the semaphore (if we can) */
7582if (semop (semId ,& sops ,1 )< 0 )
7683return -1 ;
@@ -82,8 +89,8 @@ semctl(int semId, int semNum, int flag, union semun semun)
8289/* Delete the pool */
8390else if (flag == IPC_RMID )
8491{
85- int i ;
86- HANDLE * sem_handles = (HANDLE * ) ((off_t )the_set + the_set -> m_semaphoreHandles );
92+ int i ;
93+ HANDLE * sem_handles = (HANDLE * ) ((off_t )the_set + the_set -> m_semaphoreHandles );
8794
8895/* Loop over all semaphore to delete them */
8996for (i = 0 ;i < the_set -> m_numSems ;++ i )
@@ -94,15 +101,11 @@ semctl(int semId, int semNum, int flag, union semun semun)
94101
95102/* Get the current semaphore count */
96103else if (flag == GETNCNT )
97- {
98104return the_set -> m_numSems ;
99- }
100105
101106/* Get the current semaphore count of the first semaphore in the pool */
102107else if (flag == GETVAL )
103- {
104108return sem_counts [semNum ];
105- }
106109
107110/* Other commands not yet supported */
108111else
@@ -116,17 +119,17 @@ semctl(int semId, int semNum, int flag, union semun semun)
116119int
117120semget (int semKey ,int semNum ,int flags )
118121{
119- char semname [32 ];
120- char cur_num [20 ];
121- DWORD last_error ;
122- char * num_part ;
123- bool ans = true;
122+ char semname [32 ];
123+ char cur_num [20 ];
124+ DWORD last_error ;
125+ char * num_part ;
126+ bool ans = true;
124127SECURITY_ATTRIBUTES sec_attrs ;
125- HANDLE cur_handle ;
126- bool found = false;
127- Size sem_set_size = sizeof (win32_sem_set_hdr )+ semNum * (sizeof (HANDLE )+ sizeof (int ));
128- HANDLE * sem_handles = NULL ;
129- int * sem_counts = NULL ;
128+ HANDLE cur_handle ;
129+ bool found = false;
130+ Size sem_set_size = sizeof (win32_sem_set_hdr )+ semNum * (sizeof (HANDLE )+ sizeof (int ));
131+ HANDLE * sem_handles = NULL ;
132+ int * sem_counts = NULL ;
130133
131134sec_attrs .nLength = sizeof (sec_attrs );
132135sec_attrs .lpSecurityDescriptor = NULL ;
@@ -135,23 +138,27 @@ semget(int semKey, int semNum, int flags)
135138sprintf (semname ,"PG_SEMSET.%d." ,semKey );
136139num_part = semname + strlen (semname );
137140
138- strcpy (num_part ,_itoa (_getpid ()* -1 ,cur_num ,10 ));/* For shared memory, include the pid */
139- win32_sem_set_hdr * new_set = (win32_sem_set_hdr * )ShmemInitStruct (semname ,sem_set_size ,& found );
141+ strcpy (num_part ,_itoa (_getpid ()* -1 ,cur_num ,10 ));/* For shared memory,
142+ * include the pid */
143+ win32_sem_set_hdr * new_set = (win32_sem_set_hdr * )ShmemInitStruct (semname ,sem_set_size ,& found );
140144
141- if (found ) {
145+ if (found )
146+ {
142147/* This should *never* happen */
143148errno = EEXIST ;
144149return -1 ;
145150}
146151
147152new_set -> m_numSems = semNum ;
148- new_set -> m_semaphoreHandles = sizeof (win32_sem_set_hdr );// array starts after header
149- new_set -> m_semaphoreCounts = new_set -> m_semaphoreHandles + (sizeof (HANDLE )* semNum );
153+ new_set -> m_semaphoreHandles = sizeof (win32_sem_set_hdr );
154+ //array starts after header
155+ new_set -> m_semaphoreCounts = new_set -> m_semaphoreHandles + (sizeof (HANDLE )* semNum );
150156
151- sem_handles = (HANDLE * ) ((off_t )new_set + new_set -> m_semaphoreHandles );
152- sem_counts = (int * ) ((off_t )new_set + new_set -> m_semaphoreCounts );
157+ sem_handles = (HANDLE * ) ((off_t )new_set + new_set -> m_semaphoreHandles );
158+ sem_counts = (int * ) ((off_t )new_set + new_set -> m_semaphoreCounts );
153159
154- for (int i = 0 ;i < semNum && ans ;++ i ) {
160+ for (int i = 0 ;i < semNum && ans ;++ i )
161+ {
155162strcpy (num_part ,_itoa (i ,cur_num ,10 ));
156163
157164if (flags & IPC_CREAT )
@@ -174,11 +181,13 @@ semget(int semKey, int semNum, int flags)
174181}
175182}
176183
177- if (ans ) {
184+ if (ans )
178185return MAKE_OFFSET (new_set );
179- }else {
180- // Blow away what we've got right now...
181- for (int i = 0 ;i < semNum ;++ i ) {
186+ else
187+ {
188+ /* Blow away what we've got right now... */
189+ for (int i = 0 ;i < semNum ;++ i )
190+ {
182191if (sem_handles [i ])
183192CloseHandle (sem_handles [i ]);
184193else
@@ -193,13 +202,17 @@ semget(int semKey, int semNum, int flags)
193202int
194203semop (int semId ,struct sembuf * sops ,int nsops )
195204{
196- win32_sem_set_hdr * the_set = (win32_sem_set_hdr * ) MAKE_PTR (semId );
197- HANDLE * sem_handles = (HANDLE * ) ((off_t )the_set + the_set -> m_semaphoreHandles );
198- int * sem_counts = (int * ) ((off_t )the_set + the_set -> m_semaphoreCounts );
199- HANDLE cur_handle ;
205+ win32_sem_set_hdr * the_set = (win32_sem_set_hdr * ) MAKE_PTR (semId );
206+ HANDLE * sem_handles = (HANDLE * ) ((off_t )the_set + the_set -> m_semaphoreHandles );
207+ int * sem_counts = (int * ) ((off_t )the_set + the_set -> m_semaphoreCounts );
208+ HANDLE cur_handle ;
200209
201- if (nsops != 1 ) {
202- /* Not supported (we return on 1st success, and don't cancel earlier ops) */
210+ if (nsops != 1 )
211+ {
212+ /*
213+ * Not supported (we return on 1st success, and don't cancel
214+ * earlier ops)
215+ */
203216errno = E2BIG ;
204217return -1 ;
205218}
@@ -208,23 +221,27 @@ semop(int semId, struct sembuf * sops, int nsops)
208221
209222if (sops [0 ].sem_op == -1 )
210223{
211- DWORD ret ;
224+ DWORD ret ;
225+
212226if (sops [0 ].sem_flg & IPC_NOWAIT )
213227ret = WaitForSingleObject (cur_handle ,0 );
214228else
215229ret = WaitForSingleObject (cur_handle ,INFINITE );
216230
217- if (ret == WAIT_OBJECT_0 ) {
231+ if (ret == WAIT_OBJECT_0 )
232+ {
218233/* We got it! */
219234sem_counts [sops [0 ].sem_num ]-- ;
220235return 0 ;
221- }else if (ret == WAIT_TIMEOUT )
236+ }
237+ else if (ret == WAIT_TIMEOUT )
222238/* Couldn't get it */
223239errno = EAGAIN ;
224240else
225241errno = EIDRM ;
226242}
227- else if (sops [0 ].sem_op > 0 ) {
243+ else if (sops [0 ].sem_op > 0 )
244+ {
228245/* Don't want the lock anymore */
229246sem_counts [sops [0 ].sem_num ]++ ;
230247ReleaseSemaphore (cur_handle ,sops [0 ].sem_op ,NULL );