1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Mozilla browser.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (c) 2008
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** *
37 /*****************************************************************************/
39 * This is a conservative implementation of a posix counting semaphore.
40 * It relies on the state of the underlying OS/2 event semaphore to
41 * control whether sem_wait() blocks or returns immediately, and only
42 * uses its count to change the sem's state from posted to reset.
43 * (A more "activist" approach would use the count to decide whether
44 * to return immediately or to call DosWaitEventSem().)
47 /*****************************************************************************/
52 #include "os2_semaphore.h"
54 #ifndef ERROR_SEM_BUSY
55 #define ERROR_SEM_BUSY 301
58 #define SEM_WAIT 20000
60 /*****************************************************************************/
62 int sem_init(sem_t
*sem
, int pshared
, unsigned value
)
70 psem
= (OS2SEM
*)malloc(sizeof(OS2SEM
));
74 if (DosCreateMutexSem(0, &psem
->hmtx
, 0, 0)) {
79 if (DosCreateEventSem(0, &psem
->hev
, 0, (value
? 1 : 0))) {
80 DosCloseMutexSem(psem
->hmtx
);
90 /*****************************************************************************/
92 int sem_wait(sem_t
*sem
)
101 if (DosWaitEventSem(psem
->hev
, -1))
104 if (DosRequestMutexSem(psem
->hmtx
, SEM_WAIT
))
110 DosResetEventSem(psem
->hev
, &cnt
);
111 DosReleaseMutexSem(psem
->hmtx
);
116 /*****************************************************************************/
118 int sem_post(sem_t
*sem
)
126 if (!DosRequestMutexSem(psem
->hmtx
, SEM_WAIT
)) {
128 DosPostEventSem(psem
->hev
);
129 DosReleaseMutexSem(psem
->hmtx
);
136 /*****************************************************************************/
138 int sem_destroy(sem_t
*sem
)
146 if (DosCloseMutexSem(psem
->hmtx
) == ERROR_SEM_BUSY
) {
147 DosRequestMutexSem(psem
->hmtx
, SEM_WAIT
);
148 DosReleaseMutexSem(psem
->hmtx
);
149 DosCloseMutexSem(psem
->hmtx
);
152 if (DosCloseEventSem(psem
->hev
) == ERROR_SEM_BUSY
) {
153 DosPostEventSem(psem
->hev
);
155 DosCloseEventSem(psem
->hev
);
162 /*****************************************************************************/