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>
31 #if !defined(__VXWORKS_OS__) && !defined(__PSOS__) && \
32 !defined(WIN32) && !defined(_WIN32_WCE) && !defined(HAVE_PTHREAD_WIN32) && \
33 !defined(HAVE_PTHREAD) && !defined(HAVE_PTH_PTHREAD_H)
34 #error No thread implementation found!
37 #if defined(HAVE_PTHREAD) || defined(HAVE_PTH_PTHREAD_H) || defined(HAVE_PTHREAD_WIN32)
42 osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t));
46 pthread_mutex_init (mut, NULL);
47 return (struct osip_mutex *) mut;
51 osip_mutex_destroy (struct osip_mutex *_mut)
53 osip_mutex_t *mut = (osip_mutex_t *) _mut;
56 pthread_mutex_destroy (mut);
61 osip_mutex_lock (struct osip_mutex *_mut)
63 osip_mutex_t *mut = (osip_mutex_t *) _mut;
66 return pthread_mutex_lock (mut);
70 osip_mutex_unlock (struct osip_mutex *_mut)
72 osip_mutex_t *mut = (osip_mutex_t *) _mut;
75 return pthread_mutex_unlock (mut);
80 #if (defined(HAVE_SEMAPHORE_H) && !defined(__APPLE_CC__)) || defined(HAVE_PTHREAD_WIN32)
82 /* Counting Semaphore is initialized to value */
84 osip_sem_init (unsigned int value)
86 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
90 if (sem_init (sem, 0, value) == 0)
91 return (struct osip_sem *) sem;
97 osip_sem_destroy (struct osip_sem *_sem)
99 osip_sem_t *sem = (osip_sem_t *) _sem;
108 osip_sem_post (struct osip_sem *_sem)
110 osip_sem_t *sem = (osip_sem_t *) _sem;
113 return sem_post (sem);
117 osip_sem_wait (struct osip_sem *_sem)
119 osip_sem_t *sem = (osip_sem_t *) _sem;
122 return sem_wait (sem);
126 osip_sem_trywait (struct osip_sem *_sem)
128 osip_sem_t *sem = (osip_sem_t *) _sem;
131 return sem_trywait (sem);
134 #elif defined (HAVE_SYS_SEM_H)
135 /* support for semctl, semop, semget */
137 #define SEM_PERM 0600
140 osip_sem_init (unsigned int value)
144 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
149 sem->semid = semget (IPC_PRIVATE, 1, IPC_CREAT | SEM_PERM);
150 if (sem->semid == -1)
152 perror ("semget error");
156 val.val = (int) value;
157 i = semctl (sem->semid, 0, SETVAL, val);
160 perror ("semctl error");
164 return (struct osip_sem *) sem;
168 osip_sem_destroy (struct osip_sem *_sem)
171 osip_sem_t *sem = (osip_sem_t *) _sem;
175 semctl (sem->semid, 0, IPC_RMID, val);
181 osip_sem_post (struct osip_sem *_sem)
184 osip_sem_t *sem = (osip_sem_t *) _sem;
191 return semop (sem->semid, &sb, 1);
195 osip_sem_wait (struct osip_sem *_sem)
198 osip_sem_t *sem = (osip_sem_t *) _sem;
205 return semop (sem->semid, &sb, 1);
209 osip_sem_trywait (struct osip_sem *_sem)
212 osip_sem_t *sem = (osip_sem_t *) _sem;
218 sb.sem_flg = IPC_NOWAIT;
219 return semop (sem->semid, &sb, 1);
225 /* use VxWorks implementation */
226 #ifdef __VXWORKS_OS__
231 return (struct osip_mutex *) semMCreate (SEM_Q_FIFO|SEM_DELETE_SAFE);
235 osip_mutex_destroy (struct osip_mutex *_mut)
237 osip_mutex_t *mut = (osip_mutex_t *) _mut;
244 osip_mutex_lock (struct osip_mutex *_mut)
246 osip_mutex_t *mut = (osip_mutex_t *) _mut;
249 return semTake (mut, WAIT_FOREVER);
253 osip_mutex_unlock (struct osip_mutex *_mut)
255 osip_mutex_t *mut = (osip_mutex_t *) _mut;
258 return semGive (mut);
262 osip_sem_init (unsigned int value)
267 x = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
270 initsem = semCCreate (SEM_Q_FIFO, value);
274 return (struct osip_sem *) x;
278 osip_sem_destroy (struct osip_sem *_sem)
280 osip_sem_t *sem = (osip_sem_t *) _sem;
283 semDelete (sem->semId);
289 osip_sem_post (struct osip_sem *_sem)
291 osip_sem_t *sem = (osip_sem_t *) _sem;
294 return semGive (sem->semId);
298 osip_sem_wait (struct osip_sem *_sem)
300 osip_sem_t *sem = (osip_sem_t *) _sem;
303 return semTake (sem->semId, WAIT_FOREVER);
307 osip_sem_trywait (struct osip_sem *_sem)
309 osip_sem_t *sem = (osip_sem_t *) _sem;
312 return semTake (sem->semId, NO_WAIT);
318 #if (defined(WIN32) || defined(_WIN32_WCE)) && !defined(HAVE_PTHREAD_WIN32)
325 osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t));
328 if ((mut->h = CreateMutex (NULL, FALSE, NULL)) != NULL)
329 return (struct osip_mutex *) (mut);
335 osip_mutex_destroy (struct osip_mutex *_mut)
337 osip_mutex_t *mut = (osip_mutex_t *) _mut;
340 CloseHandle (mut->h);
345 osip_mutex_lock (struct osip_mutex *_mut)
348 osip_mutex_t *mut = (osip_mutex_t *) _mut;
352 if ((err = WaitForSingleObject (mut->h, INFINITE)) == WAIT_OBJECT_0)
358 osip_mutex_unlock (struct osip_mutex *_mut)
360 osip_mutex_t *mut = (osip_mutex_t *) _mut;
363 ReleaseMutex (mut->h);
368 osip_sem_init (unsigned int value)
370 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
374 if ((sem->h = CreateSemaphore (NULL, value, LONG_MAX, NULL)) != NULL)
375 return (struct osip_sem *) (sem);
381 osip_sem_destroy (struct osip_sem *_sem)
383 osip_sem_t *sem = (osip_sem_t *) _sem;
386 CloseHandle (sem->h);
392 osip_sem_post (struct osip_sem *_sem)
394 osip_sem_t *sem = (osip_sem_t *) _sem;
397 ReleaseSemaphore (sem->h, 1, NULL);
402 osip_sem_wait (struct osip_sem *_sem)
405 osip_sem_t *sem = (osip_sem_t *) _sem;
409 if ((err = WaitForSingleObject (sem->h, INFINITE)) == WAIT_OBJECT_0)
411 if (err == WAIT_TIMEOUT)
417 osip_sem_trywait (struct osip_sem *_sem)
420 osip_sem_t *sem = (osip_sem_t *) _sem;
424 if ((err = WaitForSingleObject (sem->h, 0)) == WAIT_OBJECT_0)
434 osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t));
436 if (sm_create ("mut", 1, 0, &mut->id) == 0)
437 return (struct osip_mutex *) (mut);
443 osip_mutex_destroy (struct osip_mutex *_mut)
445 osip_mutex_t *mut = (osip_mutex_t *) _mut;
454 osip_mutex_lock (struct osip_mutex *_mut)
456 osip_mutex_t *mut = (osip_mutex_t *) _mut;
459 if (sm_p (mut->id, SM_WAIT, 0) != 0)
466 osip_mutex_unlock (struct osip_mutex *_mut)
468 osip_mutex_t *mut = (osip_mutex_t *) _mut;
478 osip_sem_init (unsigned int value)
480 osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t));
482 if (sm_create ("sem", value, 0, &sem->id) == 0)
483 return (struct osip_sem *) (sem);
489 osip_sem_destroy (struct osip_sem *_sem)
491 osip_sem_t *sem = (osip_sem_t *) _sem;
500 osip_sem_post (struct osip_sem *_sem)
502 osip_sem_t *sem = (osip_sem_t *) _sem;
505 return (sm_v (sem->id));
509 osip_sem_wait (struct osip_sem *_sem)
511 osip_sem_t *sem = (osip_sem_t *) _sem;
514 if (sm_p (sem->id, SM_WAIT, 0) != 0)
520 osip_sem_trywait (struct osip_sem *_sem)
522 osip_sem_t *sem = (osip_sem_t *) _sem;
525 if (sm_p (sem->id, SM_NOWAIT, 0) != 0)
532 #endif /* #ifdef OSIP_MT */