2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3 Copyright (C) 2001,2002,2003 Aymeric MOIZARD jack@atosc.org
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <osip2/internal.h>
27 #include <osip2/internal.h>
28 #include <osip2/osip_mt.h>
30 #if !defined(__VXWORKS_OS__) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__PSOS__)
31 #if defined(HAVE_PTHREAD) || defined(HAVE_PTH_PTHREAD_H)
36 osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t));
40 pthread_mutex_init (mut, NULL);
41 return (struct osip_mutex *) mut;
45 osip_mutex_destroy (struct osip_mutex *_mut)
47 osip_mutex_t *mut = (osip_mutex_t *) _mut;
50 pthread_mutex_destroy (mut);
55 osip_mutex_lock (struct osip_mutex *_mut)
57 osip_mutex_t *mut = (osip_mutex_t *) _mut;
60 return pthread_mutex_lock (mut);
64 osip_mutex_unlock (struct osip_mutex *_mut)
66 osip_mutex_t *mut = (osip_mutex_t *) _mut;
69 return pthread_mutex_unlock (mut);
73 #error NO thread implementation found
76 #if defined (HAVE_SEMAPHORE_H) && !defined(__APPLE_CC__)
78 /* Counting Semaphore is initialized to value */
80 osip_sem_init (unsigned int value)
82 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
84 if (sem_init (sem, 0, value) == 0)
85 return (struct osip_sem *) sem;
91 osip_sem_destroy (struct osip_sem *_sem)
93 osip_sem_t *sem = (osip_sem_t *) _sem;
102 osip_sem_post (struct osip_sem *_sem)
104 osip_sem_t *sem = (osip_sem_t *) _sem;
107 return sem_post (sem);
111 osip_sem_wait (struct osip_sem *_sem)
113 osip_sem_t *sem = (osip_sem_t *) _sem;
116 return sem_wait (sem);
120 osip_sem_trywait (struct osip_sem *_sem)
122 osip_sem_t *sem = (osip_sem_t *) _sem;
125 return sem_trywait (sem);
128 #elif defined (HAVE_SYS_SEM_H)
129 /* support for semctl, semop, semget */
131 #define SEM_PERM 0600
134 osip_sem_init (unsigned int value)
138 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
140 sem->semid = semget (IPC_PRIVATE, 1, IPC_CREAT | SEM_PERM);
141 if (sem->semid == -1)
143 perror ("semget error");
147 val.val = (int) value;
148 i = semctl (sem->semid, 0, SETVAL, val);
151 perror ("semctl error");
155 return (struct osip_sem *) sem;
159 osip_sem_destroy (struct osip_sem *_sem)
162 osip_sem_t *sem = (osip_sem_t *) _sem;
166 semctl (sem->semid, 0, IPC_RMID, val);
172 osip_sem_post (struct osip_sem *_sem)
175 osip_sem_t *sem = (osip_sem_t *) _sem;
182 return semop (sem->semid, &sb, 1);
186 osip_sem_wait (struct osip_sem *_sem)
189 osip_sem_t *sem = (osip_sem_t *) _sem;
196 return semop (sem->semid, &sb, 1);
200 osip_sem_trywait (struct osip_sem *_sem)
203 osip_sem_t *sem = (osip_sem_t *) _sem;
209 sb.sem_flg = IPC_NOWAIT;
210 return semop (sem->semid, &sb, 1);
215 /* use VxWorks implementation */
216 #ifdef __VXWORKS_OS__
220 return (struct osip_mutex *) semMCreate (0);
224 osip_mutex_destroy (struct osip_mutex *_mut)
226 osip_mutex_t *mut = (osip_mutex_t *) _mut;
233 osip_mutex_lock (struct osip_mutex *_mut)
235 osip_mutex_t *mut = (osip_mutex_t *) mut;
238 return semTake (mut, WAIT_FOREVER);
242 osip_mutex_unlock (struct osip_mutex *_mut)
244 osip_mutex_t *mut = (osip_mutex_t *) _mut;
247 return semGive (mut);
251 osip_sem_init (unsigned int value)
256 x = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
259 initsem = semCCreate (SEM_Q_FIFO, value);
263 return (struct osip_sem *) x;
267 osip_sem_destroy (struct osip_sem *_sem)
269 osip_sem_t *sem = (osip_sem_t *) _sem;
272 semDelete (sem->semId);
278 osip_sem_post (struct osip_sem *_sem)
280 osip_sem_t *sem = (osip_sem_t *) _sem;
283 return semGive (sem->semId);
287 osip_sem_wait (struct osip_sem *_sem)
289 osip_sem_t *sem = (osip_sem_t *) _sem;
292 return semTake (sem->semId, WAIT_FOREVER);
296 osip_sem_trywait (struct osip_sem *_sem)
298 osip_sem_t *sem = (osip_sem_t *) _sem;
301 return semTake (sem->semId, NO_WAIT);
305 #if defined (WIN32) || defined (_WIN32_WCE)
311 osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t));
313 if ((mut->h = CreateMutex (NULL, FALSE, NULL)) != NULL)
314 return (struct osip_mutex *) (mut);
320 osip_mutex_destroy (struct osip_mutex *_mut)
322 osip_mutex_t *mut = (osip_mutex_t *) _mut;
325 CloseHandle (mut->h);
330 osip_mutex_lock (struct osip_mutex *_mut)
333 osip_mutex_t *mut = (osip_mutex_t *) _mut;
337 if ((err = WaitForSingleObject (mut->h, INFINITE)) == WAIT_OBJECT_0)
343 osip_mutex_unlock (struct osip_mutex *_mut)
345 osip_mutex_t *mut = (osip_mutex_t *) _mut;
348 ReleaseMutex (mut->h);
353 osip_sem_init (unsigned int value)
355 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
357 if ((sem->h = CreateSemaphore (NULL, value, LONG_MAX, NULL)) != NULL)
358 return (struct osip_sem *) (sem);
364 osip_sem_destroy (struct osip_sem *_sem)
366 osip_sem_t *sem = (osip_sem_t *) _sem;
369 CloseHandle (sem->h);
375 osip_sem_post (struct osip_sem *_sem)
377 osip_sem_t *sem = (osip_sem_t *) _sem;
380 ReleaseSemaphore (sem->h, 1, NULL);
385 osip_sem_wait (struct osip_sem *_sem)
388 osip_sem_t *sem = (osip_sem_t *) _sem;
392 if ((err = WaitForSingleObject (sem->h, INFINITE)) == WAIT_OBJECT_0)
394 if (err == WAIT_TIMEOUT)
400 osip_sem_trywait (struct osip_sem *_sem)
403 osip_sem_t *sem = (osip_sem_t *) _sem;
407 if ((err = WaitForSingleObject (sem->h, 0)) == WAIT_OBJECT_0)
417 osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t));
419 if (sm_create ("mut", 1, 0, &mut->id) == 0)
420 return (struct osip_mutex *) (mut);
426 osip_mutex_destroy (struct osip_mutex *_mut)
428 osip_mutex_t *mut = (osip_mutex_t *) _mut;
437 osip_mutex_lock (struct osip_mutex *_mut)
439 osip_mutex_t *mut = (osip_mutex_t *) _mut;
442 if (sm_p (mut->id, SM_WAIT, 0) != 0)
449 osip_mutex_unlock (struct osip_mutex *_mut)
451 osip_mutex_t *mut = (osip_mutex_t *) _mut;
461 osip_sem_init (unsigned int value)
463 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
465 if (sm_create ("sem", value, 0, &sem->id) == 0)
466 return (struct osip_sem *) (sem);
472 osip_sem_destroy (struct osip_sem *_sem)
474 osip_sem_t *sem = (osip_sem_t *) _sem;
483 osip_sem_post (struct osip_sem *_sem)
485 osip_sem_t *sem = (osip_sem_t *) _sem;
488 return (sm_v (sem->id));
492 osip_sem_wait (struct osip_sem *_sem)
494 osip_sem_t *sem = (osip_sem_t *) _sem;
497 if (sm_p (sem->id, SM_WAIT, 0) != 0)
503 osip_sem_trywait (struct osip_sem *_sem)
505 osip_sem_t *sem = (osip_sem_t *) _sem;
508 if (sm_p (sem->id, SM_NOWAIT, 0) != 0)