Add semaphore support for OS/2
[liboggplay.git] / src / liboggplay / os2_semaphore.c
blob80bb6faaa325c389274b3e17c0f430f4ea727724
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
12 * License.
14 * The Original Code is the Mozilla browser.
16 * The Initial Developer of the Original Code is
17 * Richard Walsh
18 * Portions created by the Initial Developer are Copyright (c) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
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 /*****************************************************************************/
38 /*
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 /*****************************************************************************/
49 #include <stdlib.h>
50 #define INCL_DOS
51 #include <os2.h>
52 #include "os2_semaphore.h"
54 #ifndef ERROR_SEM_BUSY
55 #define ERROR_SEM_BUSY 301
56 #endif
58 #define SEM_WAIT 20000
60 /*****************************************************************************/
62 int sem_init(sem_t *sem, int pshared, unsigned value)
64 OS2SEM * psem;
66 if (!sem)
67 return -1;
68 *sem = 0;
70 psem = (OS2SEM*)malloc(sizeof(OS2SEM));
71 if (!psem)
72 return -1;
74 if (DosCreateMutexSem(0, &psem->hmtx, 0, 0)) {
75 free(psem);
76 return -1;
79 if (DosCreateEventSem(0, &psem->hev, 0, (value ? 1 : 0))) {
80 DosCloseMutexSem(psem->hmtx);
81 free(psem);
82 return -1;
85 psem->cnt = value;
86 *sem = psem;
87 return 0;
90 /*****************************************************************************/
92 int sem_wait(sem_t *sem)
94 OS2SEM * psem;
95 ULONG cnt;
97 if (!sem || !*sem)
98 return -1;
99 psem = *sem;
101 if (DosWaitEventSem(psem->hev, -1))
102 return -1;
104 if (DosRequestMutexSem(psem->hmtx, SEM_WAIT))
105 return -1;
107 if (psem->cnt)
108 psem->cnt--;
109 if (!psem->cnt)
110 DosResetEventSem(psem->hev, &cnt);
111 DosReleaseMutexSem(psem->hmtx);
113 return 0;
116 /*****************************************************************************/
118 int sem_post(sem_t *sem)
120 OS2SEM * psem;
122 if (!sem || !*sem)
123 return -1;
124 psem = *sem;
126 if (!DosRequestMutexSem(psem->hmtx, SEM_WAIT)) {
127 psem->cnt++;
128 DosPostEventSem(psem->hev);
129 DosReleaseMutexSem(psem->hmtx);
130 return 0;
133 return -1;
136 /*****************************************************************************/
138 int sem_destroy(sem_t *sem)
140 OS2SEM * psem;
142 if (!sem || !*sem)
143 return -1;
144 psem = *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);
154 DosSleep(1);
155 DosCloseEventSem(psem->hev);
158 free(psem);
159 return 0;
162 /*****************************************************************************/