sipxportlib  Version 3.3
pt_mutex.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_MUTEX_H
23 #define _PT_MUTEX_H
24 
25 #if !defined(__APPLE__) && !defined(ANDROID)
26 /* Also see pt_csem.h */
27 #define SIPX_USE_NATIVE_PTHREADS
28 //#undef SIPX_USE_NATIVE_PTHREADS
29 #endif
30 
31 #include <pthread.h>
32 
33 #ifdef SIPX_USE_NATIVE_PTHREADS // [
34 
35 #define pt_mutex_t pthread_mutex_t
36 #if defined(__linux__) && !defined(PTHREAD_MUTEX_RECURSIVE)
37 # define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
38 #endif
39 
40 
41 //#define pt_mutex_init(mutex) pthread_mutex_init((mutex), NULL)
42 static inline int pt_mutex_init(pthread_mutex_t *mutex)
43 {
44  pthread_mutexattr_t a;
45  pthread_mutexattr_init(&a);
46  pthread_mutexattr_settype(&a,PTHREAD_MUTEX_RECURSIVE);
47  return pthread_mutex_init(mutex, &a);
48 }
49 
50 #define pt_mutex_lock(mutex) pthread_mutex_lock((mutex))
51 #define pt_mutex_timedlock(mutex, timeout) pthread_mutex_timedlock((mutex), (timeout))
52 #define pt_mutex_trylock(mutex) pthread_mutex_trylock((mutex))
53 #define pt_mutex_unlock(mutex) pthread_mutex_unlock((mutex))
54 #define pt_mutex_destroy(mutex) pthread_mutex_destroy((mutex))
55 
56 
57 #else // SIPX_USE_NATIVE_PTHREADS ][
58 
59 /* The default LinuxThreads implementation does not have support for timing
60 * out while waiting for a synchronization object. Since I've already ported
61 * the rest of the OS dependent files to that interface, we can just drop in a
62 * mostly-compatible replacement written in C (like pthreads itself) that uses
63 * the pthread_cond_timedwait function and a mutex to build all the other
64 * synchronization objecs with timeout capabilities. */
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 typedef struct pt_mutex {
71  unsigned int count;
72  pthread_t thread;
73  pthread_mutex_t mutex;
74  pthread_cond_t cond;
75 } pt_mutex_t;
76 
77 void dumpPtMutex(pt_mutex_t* mutex);
78 
79 int pt_mutex_init(pt_mutex_t *mutex);
80 
81 int pt_mutex_lock(pt_mutex_t *mutex);
82 
83 int pt_mutex_timedlock(pt_mutex_t *mutex,const struct timespec *timeout);
84 
85 int pt_mutex_trylock(pt_mutex_t *mutex);
86 
87 int pt_mutex_unlock(pt_mutex_t *mutex);
88 
89 int pt_mutex_destroy(pt_mutex_t *mutex);
90 
91 #ifdef __cplusplus
92 }
93 #endif
94 
95 #endif // SIPX_USE_NATIVE_PTHREADS ]
96 
97 #endif /* _PT_MUTEX_H */
#define pt_mutex_t
Definition: pt_mutex.h:35
#define pt_mutex_lock(mutex)
Definition: pt_mutex.h:50
#define pt_mutex_trylock(mutex)
Definition: pt_mutex.h:52
#define pt_mutex_unlock(mutex)
Definition: pt_mutex.h:53
#define pt_mutex_timedlock(mutex, timeout)
Definition: pt_mutex.h:51
#define pt_mutex_destroy(mutex)
Definition: pt_mutex.h:54