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
22 #ifndef __PORT_CONDV_H__
23 #define __PORT_CONDV_H__
29 #include <osip2/internal.h>
30 #include <osip2/osip_mt.h>
31 #include <osip2/osip_condv.h>
33 #if !defined(__VXWORKS_OS__) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__PSOS__)
34 #if defined(HAVE_PTHREAD) || defined(HAVE_PTH_PTHREAD_H)
37 #include <sys/types.h>
38 #include <sys/timeb.h>
45 osip_cond_t *cond = (osip_cond_t *) osip_malloc (sizeof (osip_cond_t));
46 if (cond && (pthread_cond_init (&cond->cv, NULL) == 0))
48 return (struct osip_cond *) (cond);
56 osip_cond_destroy (struct osip_cond *_cond)
60 ret = pthread_cond_destroy (&_cond->cv);
66 osip_cond_signal (struct osip_cond *_cond)
68 return pthread_cond_signal (&_cond->cv);
73 osip_cond_wait (struct osip_cond *_cond, struct osip_mutex *_mut)
75 return pthread_cond_wait (&_cond->cv, (pthread_mutex_t *) _mut);
80 osip_cond_timedwait (struct osip_cond *_cond, struct osip_mutex *_mut,
81 const struct timespec *abstime)
83 return pthread_cond_timedwait (&_cond->cv, (pthread_mutex_t *) _mut,
84 (const struct timespec *) abstime);
87 #error NO thread implementation found
91 #if defined (WIN32) || defined (_WIN32_WCE)
92 #include <sys/types.h>
93 #include <sys/timeb.h>
97 osip_cond_t *cond = (osip_cond_t *) osip_malloc (sizeof (osip_cond_t));
98 if ((cond->mut = osip_mutex_init ()) != NULL)
100 cond->sem = osip_sem_init (0); /* initially locked */
101 return (struct osip_cond *) (cond);
109 osip_cond_destroy (struct osip_cond *_cond)
111 if (_cond->sem == NULL)
114 osip_sem_destroy (_cond->sem);
116 if (_cond->mut == NULL)
119 osip_mutex_destroy (_cond->mut);
125 osip_cond_signal (struct osip_cond *_cond)
127 return osip_sem_post (_cond->sem);
132 osip_cond_wait (struct osip_cond *_cond, struct osip_mutex *_mut)
134 int ret1 = 0, ret2 = 0, ret3 = 0;
136 if (osip_mutex_lock (_cond->mut))
139 if (osip_mutex_unlock (_mut))
142 ret1 = osip_sem_wait (_cond->sem);
144 ret2 = osip_mutex_lock (_mut);
146 ret3 = osip_mutex_unlock (_cond->mut);
148 if (ret1 || ret2 || ret3)
153 #define OSIP_CLOCK_REALTIME 4002
156 __osip_clock_gettime (unsigned int clock_id, struct timespec *tp)
158 struct _timeb time_val;
160 if (clock_id != OSIP_CLOCK_REALTIME)
167 tp->tv_sec = time_val.time;
168 tp->tv_nsec = time_val.millitm * 1000000;
173 _delta_time (const struct timespec *start, const struct timespec *end)
177 difx = ((end->tv_sec - start->tv_sec) * 1000) +
178 ((end->tv_nsec - start->tv_nsec) / 1000000);
185 osip_cond_timedwait (struct osip_cond *_cond, struct osip_mutex *_mut,
186 const struct timespec *abstime)
191 HANDLE sem = *((HANDLE *) _cond->sem);
199 __osip_clock_gettime (OSIP_CLOCK_REALTIME, &now);
201 timeout_ms = _delta_time (&now, abstime);
203 return 1; /* ETIMEDOUT; */
205 if (osip_mutex_unlock (_mut))
208 dwRet = WaitForSingleObject (sem, timeout_ms);
210 if (osip_mutex_lock (_mut))
219 return 1; /* ETIMEDOUT; */
231 /* use VxWorks implementation */
232 #ifdef __VXWORKS_OS__