1 //%2003////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //==============================================================================
26 // Author: Mike Day (mdday@us.ibm.com) and Roger Kumpf (roger_kumpf@hp.com)
30 //%/////////////////////////////////////////////////////////////////////////////
35 #include <semaphore.h>
42 typedef sem_t PEGASUS_SEMAPHORE_TYPE;
43 typedef pthread_t PEGASUS_THREAD_TYPE;
44 typedef pthread_mutex_t PEGASUS_MUTEX_TYPE;
49 } PEGASUS_SEM_HANDLE ;
53 pthread_mutexattr_t mutatt;
55 } PEGASUS_MUTEX_HANDLE ;
57 typedef PEGASUS_MUTEX_HANDLE PEGASUS_CRIT_TYPE;
59 typedef __pthread_cleanup_handler_t PEGASUS_CLEANUP_HANDLE ;
60 typedef void *PEGASUS_THREAD_RETURN;
62 #define PEGASUS_THREAD_CDECL
67 } PEGASUS_THREAD_HANDLE ;
69 //-----------------------------------------------------------------
70 /// Conditionals to support native or generic Conditional Semaphore
71 //-----------------------------------------------------------------
73 #define PEGASUS_CONDITIONAL_NATIVE = 1
75 typedef pthread_cond_t PEGASUS_COND_TYPE;
80 } PEGASUS_COND_HANDLE;
83 //-----------------------------------------------------------------
84 /// Conditionals to support native or generic atomic variables
85 //-----------------------------------------------------------------
87 // linux offers a built-in integer type for atomic access
88 // other unix platforms HPUX, AIX, may have different types
89 // implementors should use the native type for faster operations
91 // ATTN: RK - sig_atomic_t is defined on HP-UX, but the atomic_read()
92 // and atomic_write() methods are not defined. Use the non-native
93 // implementation for now.
95 // #define PEGASUS_ATOMIC_INT_NATIVE = 1
97 // typedef sig_atomic_t PEGASUS_ATOMIC_TYPE ;
100 //-----------------------------------------------------------------
101 /// Conditionals to support native or generic read/write semaphores
102 //-----------------------------------------------------------------
104 #define PEGASUS_READWRITE_NATIVE = 1
107 pthread_rwlock_t rwlock;
109 } PEGASUS_RWLOCK_HANDLE;
112 PEGASUS_NAMESPACE_BEGIN
113 inline void pegasus_yield(void)
119 // pthreads cancellation calls
120 inline void disable_cancel(void)
122 pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, NULL);
125 inline void enable_cancel(void)
127 pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, NULL);
131 // the next two routines are macros that MUST SHARE the same stack frame
132 // they are implemented as macros by glibc.
133 // native_cleanup_push( void (*func)(void *) ) ;
134 // these ALSO SET CANCEL STATE TO DEFER
135 #define native_cleanup_push( func, arg ) \
136 pthread_cleanup_push_defer_np((func), arg)
138 // native cleanup_pop(Boolean execute) ;
139 #define native_cleanup_pop(execute) \
140 pthread_cleanup_pop_restore_np(execute)
142 inline void pegasus_sleep(int msec)
144 struct timespec wait;
145 wait.tv_sec = msec / 1000;
146 wait.tv_nsec = (msec % 1000) * 1000000;
147 nanosleep(&wait, NULL);
150 inline void init_crit(PEGASUS_CRIT_TYPE *crit)
152 pthread_mutexattr_init(&(crit->mutatt));
153 pthread_mutexattr_setspin_np(&(crit->mutatt), PTHREAD_MUTEX_SPINONLY_NP);
154 pthread_mutex_init(&(crit->mut), &(crit->mutatt));
158 inline void enter_crit(PEGASUS_CRIT_TYPE *crit)
160 pthread_mutex_lock(&(crit->mut));
163 inline void try_crit(PEGASUS_CRIT_TYPE *crit)
165 pthread_mutex_trylock(&(crit->mut));
168 inline void exit_crit(PEGASUS_CRIT_TYPE *crit)
170 pthread_mutex_unlock(&(crit->mut));
173 inline void destroy_crit(PEGASUS_CRIT_TYPE *crit)
175 while( EBUSY == pthread_mutex_destroy(&(crit->mut)))
179 pthread_mutexattr_destroy(&(crit->mutatt));
182 static inline int pegasus_gettimeofday(struct timeval *tv) { return(gettimeofday(tv, NULL)); }
184 inline void exit_thread(PEGASUS_THREAD_RETURN rc)
189 inline PEGASUS_THREAD_TYPE pegasus_thread_self(void)
191 return(pthread_self());
195 typedef pthread_key_t PEGASUS_THREAD_KEY_TYPE;
197 inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
199 // Note: a destructor is not supported
200 // (because not supported on Windows (?))
201 return pthread_key_create(key, NULL);
204 inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
206 return pthread_key_delete(key);
209 inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
211 return pthread_getspecific(key);
214 inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
217 return pthread_setspecific(key, value);
221 inline void destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
227 PEGASUS_NAMESPACE_END