sipxportlib  Version 3.3
pt_csem.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 /* SIPX_USE_NATIVE_PTHREADS define enable use of standard pthread implementation
12 * of synchonization primitives instead of sipX re-implementation of them. This
13 * re-implementation was created to work around the fact that the LinuxThreads
14 * implementation of pthreads did not have a timed version of semaphore wait
15 * (sem_timedwait()). These days NPTL is widely adopted, thus solving this
16 * problem more efficiently. So, if you're not forced to use a pthreads
17 * implementation without sem_timedwait() (e.g. on OS X) it is better to
18 * define SIPX_USE_NATIVE_PTHREADS.
19 * See the original comment about the sipX implementation below #else.
20 */
21 
22 #ifndef _PT_CSEM_H
23 #define _PT_CSEM_H
24 
25 #if !defined(__APPLE__) && !defined(ANDROID)
26 /* Also see pt_mutex.h */
27 #define SIPX_USE_NATIVE_PTHREADS
28 //#undef SIPX_USE_NATIVE_PTHREADS
29 #endif
30 
31 #ifdef SIPX_USE_NATIVE_PTHREADS // [
32 
33 #include <semaphore.h>
34 
35 #define pt_sem_t sem_t
36 
37 #define pt_sem_init(sem, max, count) sem_init((sem), 0, (count))
38 #define pt_sem_wait(sem) sem_wait((sem))
39 #define pt_sem_timedwait(sem, timeout) sem_timedwait((sem), (timeout))
40 #define pt_sem_trywait(sem) sem_trywait((sem))
41 #define pt_sem_post(sem) sem_post((sem))
42 #define pt_sem_getvalue(sem) sem_getvalue((sem))
43 #define pt_sem_destroy(sem) sem_destroy((sem))
44 
45 
46 #else // SIPX_USE_NATIVE_PTHREADS ][
47 
48 /* The default LinuxThreads implementation does not have support for timing
49 * out while waiting for a synchronization object. Since I've already ported
50 * the rest of the OS dependent files to that interface, we can just drop in a
51 * mostly-compatible replacement written in C (like pthreads itself) that uses
52 * the pthread_cond_timedwait function and a mutex to build all the other
53 * synchronization objecs with timeout capabilities. */
54 
55 #include <pthread.h>
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 typedef struct pt_sem {
62  unsigned int count;
63  unsigned int max;
64  pthread_mutex_t mutex;
65  pthread_cond_t cond;
66 } pt_sem_t;
67 
68 int pt_sem_init(pt_sem_t *sem, unsigned int max, unsigned int count);
69 
70 int pt_sem_wait(pt_sem_t *sem);
71 
72 int pt_sem_timedwait(pt_sem_t *sem,const struct timespec *timeout);
73 
74 int pt_sem_trywait(pt_sem_t *sem);
75 
76 int pt_sem_post(pt_sem_t *sem);
77 
78 int pt_sem_getvalue(pt_sem_t *sem);
79 
80 int pt_sem_destroy(pt_sem_t *sem);
81 
82 #ifdef __cplusplus
83 }
84 #endif
85 
86 #endif // SIPX_USE_NATIVE_PTHREADS ]
87 
88 #endif /* _PT_CSEM_H */
#define pt_sem_post(sem)
Definition: pt_csem.h:41
#define pt_sem_wait(sem)
Definition: pt_csem.h:38
#define pt_sem_destroy(sem)
Definition: pt_csem.h:43
#define pt_sem_init(sem, max, count)
Definition: pt_csem.h:37
#define pt_sem_t
Definition: pt_csem.h:35
#define pt_sem_getvalue(sem)
Definition: pt_csem.h:42
#define pt_sem_timedwait(sem, timeout)
Definition: pt_csem.h:39
#define pt_sem_trywait(sem)
Definition: pt_csem.h:40