PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / Common / IPCHpux.h
1 //%2003////////////////////////////////////////////////////////////////////////
2 //
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.
7 //
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:
14 // 
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.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Day (mdday@us.ibm.com) and Roger Kumpf (roger_kumpf@hp.com)
27 //
28 // Modified By:
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32
33 #include <sched.h>
34 #include <pthread.h>
35 #include <semaphore.h>
36 #include <signal.h>
37 #include <errno.h>
38 #include <sys/time.h>
39 #include <time.h>
40
41
42 typedef sem_t PEGASUS_SEMAPHORE_TYPE;
43 typedef pthread_t PEGASUS_THREAD_TYPE;
44 typedef pthread_mutex_t PEGASUS_MUTEX_TYPE;
45
46 typedef struct {
47     sem_t sem;
48     pthread_t owner;
49 } PEGASUS_SEM_HANDLE ;
50
51 typedef struct {
52     pthread_mutex_t mut;
53     pthread_mutexattr_t mutatt;
54     pthread_t owner;
55 } PEGASUS_MUTEX_HANDLE ;
56
57 typedef PEGASUS_MUTEX_HANDLE  PEGASUS_CRIT_TYPE;
58
59 typedef __pthread_cleanup_handler_t PEGASUS_CLEANUP_HANDLE ;
60 typedef void *PEGASUS_THREAD_RETURN;
61
62 #define PEGASUS_THREAD_CDECL
63
64 typedef struct {
65     pthread_t thid;
66     pthread_attr_t thatt;
67 } PEGASUS_THREAD_HANDLE ;
68
69 //-----------------------------------------------------------------
70 /// Conditionals to support native or generic Conditional Semaphore
71 //-----------------------------------------------------------------
72
73 #define PEGASUS_CONDITIONAL_NATIVE = 1
74
75 typedef pthread_cond_t PEGASUS_COND_TYPE;
76
77 typedef struct {
78     pthread_cond_t cond;
79     pthread_t owner;
80 } PEGASUS_COND_HANDLE;
81
82
83 //-----------------------------------------------------------------
84 /// Conditionals to support native or generic atomic variables
85 //-----------------------------------------------------------------
86
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
90
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.
94
95 // #define PEGASUS_ATOMIC_INT_NATIVE = 1
96
97 // typedef sig_atomic_t PEGASUS_ATOMIC_TYPE ;
98
99
100 //-----------------------------------------------------------------
101 /// Conditionals to support native or generic read/write semaphores
102 //-----------------------------------------------------------------
103
104 #define PEGASUS_READWRITE_NATIVE = 1
105
106 typedef struct {
107     pthread_rwlock_t rwlock;
108     pthread_t owner;
109 } PEGASUS_RWLOCK_HANDLE;
110
111
112 PEGASUS_NAMESPACE_BEGIN
113 inline void pegasus_yield(void)
114 {
115       sched_yield();
116 }
117
118
119 // pthreads cancellation calls 
120 inline void disable_cancel(void)
121 {
122    pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, NULL);
123 }
124
125 inline void enable_cancel(void)
126 {
127    pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, NULL);
128 }
129
130
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)
137
138 // native cleanup_pop(Boolean execute) ; 
139 #define native_cleanup_pop(execute) \
140    pthread_cleanup_pop_restore_np(execute)
141
142 inline void pegasus_sleep(int msec)
143 {
144     struct timespec wait;
145     wait.tv_sec = msec / 1000;
146     wait.tv_nsec = (msec % 1000) * 1000000;
147     nanosleep(&wait, NULL);
148 }
149
150 inline void init_crit(PEGASUS_CRIT_TYPE *crit)
151 {
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));
155    crit->owner = 0;
156 }
157
158 inline void enter_crit(PEGASUS_CRIT_TYPE *crit)
159 {
160    pthread_mutex_lock(&(crit->mut));
161 }
162
163 inline void try_crit(PEGASUS_CRIT_TYPE *crit)
164 {
165    pthread_mutex_trylock(&(crit->mut));
166 }
167
168 inline void exit_crit(PEGASUS_CRIT_TYPE *crit)
169 {
170    pthread_mutex_unlock(&(crit->mut));
171 }
172
173 inline void destroy_crit(PEGASUS_CRIT_TYPE *crit)
174 {
175    while( EBUSY == pthread_mutex_destroy(&(crit->mut)))
176    {
177       pegasus_yield();
178    }
179    pthread_mutexattr_destroy(&(crit->mutatt));
180 }
181
182 static inline int pegasus_gettimeofday(struct timeval *tv) { return(gettimeofday(tv, NULL)); }
183
184 inline void exit_thread(PEGASUS_THREAD_RETURN rc)
185 {
186   pthread_exit(rc);
187 }
188
189 inline PEGASUS_THREAD_TYPE pegasus_thread_self(void) 
190
191    return(pthread_self());
192 }
193
194 // l10n start
195 typedef pthread_key_t PEGASUS_THREAD_KEY_TYPE;
196
197 inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
198 {
199         // Note: a destructor is not supported 
200         // (because not supported on Windows (?))
201         return pthread_key_create(key, NULL);
202
203
204 inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
205 {
206         return pthread_key_delete(key);
207
208
209 inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
210 {
211         return pthread_getspecific(key);
212
213
214 inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
215                                                                                  void * value)
216 {
217         return pthread_setspecific(key, value);
218
219 // l10n end
220
221 inline void destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
222 {
223    pthread_cancel(th);
224 }
225
226
227 PEGASUS_NAMESPACE_END