📄 port_sema.c
字号:
/* The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Aymeric MOIZARD jack@atosc.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#ifdef OSIP_MT#include <stdlib.h>#include <stdio.h>#include <osip2/internal.h>#include <osip2/internal.h>#include <osip2/osip_mt.h>#if defined(__rtems__)#include <rtems.h>#endif #if !defined(__VXWORKS_OS__) && !defined(__PSOS__) && \ !defined(WIN32) && !defined(_WIN32_WCE) && !defined(HAVE_PTHREAD_WIN32) && \ !defined(HAVE_PTHREAD) && !defined(HAVE_PTH_PTHREAD_H) && !defined(__rtems__)#error No thread implementation found!#endif#if defined(HAVE_PTHREAD) || defined(HAVE_PTH_PTHREAD_H) || defined(HAVE_PTHREAD_WIN32)struct osip_mutex *osip_mutex_init (){ osip_mutex_t *mut = (osip_mutex_t *) osip_malloc (sizeof (osip_mutex_t)); if (mut == NULL) return NULL; pthread_mutex_init (mut, NULL); return (struct osip_mutex *) mut;}voidosip_mutex_destroy (struct osip_mutex *_mut){ osip_mutex_t *mut = (osip_mutex_t *) _mut; if (mut == NULL) return; pthread_mutex_destroy (mut); osip_free (mut);}intosip_mutex_lock (struct osip_mutex *_mut){ osip_mutex_t *mut = (osip_mutex_t *) _mut; if (mut == NULL) return -1; return pthread_mutex_lock (mut);}intosip_mutex_unlock (struct osip_mutex *_mut){ osip_mutex_t *mut = (osip_mutex_t *) _mut; if (mut == NULL) return -1; return pthread_mutex_unlock (mut);}#endif#if defined(__arc__)/* Counting Semaphore is initialized to value */struct osip_sem *osip_sem_init (unsigned int value){ osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t)); if (sem == NULL) return NULL; sem->_sem_counter=0; sem->_sem_mutex=osip_mutex_init (); if (sem->_sem_mutex!=NULL) return (struct osip_sem *) sem; osip_free (sem); return NULL;}intosip_sem_destroy (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return 0; osip_mutex_destroy(sem->_sem_mutex); osip_free (sem); return 0;}intosip_sem_post (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; osip_mutex_lock(sem->_sem_mutex); sem->_sem_counter++; osip_mutex_unlock(sem->_sem_mutex); return 0;}intosip_sem_wait (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; /* poor emulation... */ while (1) { osip_mutex_lock(sem->_sem_mutex); if (sem->_sem_counter>0) { sem->_sem_counter--; osip_mutex_unlock(sem->_sem_mutex); return 0; } osip_mutex_unlock(sem->_sem_mutex); osip_usleep(1000); } return -1;}intosip_sem_trywait (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; osip_mutex_lock(sem->_sem_mutex); if (sem->_sem_counter>0) { sem->_sem_counter--; osip_mutex_unlock(sem->_sem_mutex); return 0; } osip_mutex_unlock(sem->_sem_mutex); return -1;}#elif (defined(HAVE_SEMAPHORE_H) && !defined(__APPLE_CC__)) || defined(HAVE_PTHREAD_WIN32)/* Counting Semaphore is initialized to value */struct osip_sem *osip_sem_init (unsigned int value){ osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t)); if (sem == NULL) return NULL; if (sem_init (sem, 0, value) == 0) return (struct osip_sem *) sem; osip_free (sem); return NULL;}intosip_sem_destroy (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return 0; sem_destroy (sem); osip_free (sem); return 0;}intosip_sem_post (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; return sem_post (sem);}intosip_sem_wait (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; return sem_wait (sem);}intosip_sem_trywait (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; return sem_trywait (sem);}#elif defined (HAVE_SYS_SEM_H)/* support for semctl, semop, semget */#define SEM_PERM 0600struct osip_sem *osip_sem_init (unsigned int value){ union semun val; int i; osip_sem_t *sem = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t)); if (sem == NULL) return NULL; sem->semid = semget (IPC_PRIVATE, 1, IPC_CREAT | SEM_PERM); if (sem->semid == -1) { perror ("semget error"); osip_free (sem); return NULL; } val.val = (int) value; i = semctl (sem->semid, 0, SETVAL, val); if (i != 0) { perror ("semctl error"); osip_free (sem); return NULL; } return (struct osip_sem *) sem;}intosip_sem_destroy (struct osip_sem *_sem){ union semun val; osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return 0; val.val = 0; semctl (sem->semid, 0, IPC_RMID, val); osip_free (sem); return 0;}intosip_sem_post (struct osip_sem *_sem){ struct sembuf sb; osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; sb.sem_num = 0; sb.sem_op = 1; sb.sem_flg = 0; return semop (sem->semid, &sb, 1);}intosip_sem_wait (struct osip_sem *_sem){ struct sembuf sb; osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; sb.sem_num = 0; sb.sem_op = -1; sb.sem_flg = 0; return semop (sem->semid, &sb, 1);}intosip_sem_trywait (struct osip_sem *_sem){ struct sembuf sb; osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; sb.sem_num = 0; sb.sem_op = -1; sb.sem_flg = IPC_NOWAIT; return semop (sem->semid, &sb, 1);}#endif/* use VxWorks implementation */#ifdef __VXWORKS_OS__struct osip_mutex *osip_mutex_init (){ return (struct osip_mutex *) semMCreate (SEM_Q_FIFO | SEM_DELETE_SAFE);}voidosip_mutex_destroy (struct osip_mutex *_mut){ osip_mutex_t *mut = (osip_mutex_t *) _mut; if (mut == NULL) return; semDelete (mut);}intosip_mutex_lock (struct osip_mutex *_mut){ osip_mutex_t *mut = (osip_mutex_t *) _mut; if (mut == NULL) return -1; return semTake (mut, WAIT_FOREVER);}intosip_mutex_unlock (struct osip_mutex *_mut){ osip_mutex_t *mut = (osip_mutex_t *) _mut; if (mut == NULL) return -1; return semGive (mut);}struct osip_sem *osip_sem_init (unsigned int value){ SEM_ID initsem; osip_sem_t *x; x = (osip_sem_t *) osip_malloc (sizeof (osip_sem_t)); if (x == NULL) return NULL; initsem = semCCreate (SEM_Q_FIFO, value); x->semId = initsem; x->refCnt = value; x->sem_name = NULL; return (struct osip_sem *) x;}intosip_sem_destroy (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return 0; semDelete (sem->semId); osip_free (sem); return 0;}intosip_sem_post (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; return semGive (sem->semId);}intosip_sem_wait (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; return semTake (sem->semId, WAIT_FOREVER);}intosip_sem_trywait (struct osip_sem *_sem){ osip_sem_t *sem = (osip_sem_t *) _sem; if (sem == NULL) return -1; return semTake (sem->semId, NO_WAIT);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -