PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / Common / IPCAix.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: 
27 //
28 // Modified By:
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef IPC_AIX_include
33 #define IPC_AIX_include
34
35 #include <sched.h>
36 #include <pthread.h>
37 #include <semaphore.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <time.h>
42 PEGASUS_NAMESPACE_BEGIN
43
44 typedef sem_t PEGASUS_SEMAPHORE_TYPE;
45 typedef pthread_t PEGASUS_THREAD_TYPE;
46 typedef pthread_mutex_t PEGASUS_MUTEX_TYPE;
47
48 typedef struct {
49     Uint32 waiters;
50     pthread_mutex_t mutex;
51     pthread_cond_t cond;
52     PEGASUS_THREAD_TYPE owner;
53 } PEGASUS_SEM_HANDLE ;
54
55 typedef struct {
56     pthread_mutex_t mut;
57     pthread_mutexattr_t mutatt;
58     pthread_t owner;
59 } PEGASUS_MUTEX_HANDLE ;
60
61 typedef PEGASUS_MUTEX_HANDLE PEGASUS_CRIT_TYPE;
62
63 typedef void *PEGASUS_CLEANUP_HANDLE ;
64 typedef void *PEGASUS_THREAD_RETURN;
65
66 #define PEGASUS_THREAD_CDECL
67
68 typedef struct {
69     pthread_t thid;
70     pthread_attr_t thatt;
71 } PEGASUS_THREAD_HANDLE ;
72
73 //-----------------------------------------------------------------
74 /// Conditionals to support native or generic Conditional Semaphore
75 //-----------------------------------------------------------------
76
77 #define PEGASUS_CONDITIONAL_NATIVE = 1
78
79 typedef pthread_cond_t PEGASUS_COND_TYPE;
80
81 typedef struct {
82     pthread_cond_t cond;
83     pthread_t owner;
84 } PEGASUS_COND_HANDLE;
85
86
87 //-----------------------------------------------------------------
88 /// Conditionals to support native or generic atomic variables
89 //-----------------------------------------------------------------
90
91 // linux offers a built-in integer type for atomic access
92 // other unix platforms HPUX, AIX, may have different types
93 // implementors should use the native type for faster operations
94
95 // ATTN: RK - sig_atomic_t is defined on HP-UX, but the atomic_read()
96 // and atomic_write() methods are not defined.  Use the non-native
97 // implementation for now.
98
99 // #define PEGASUS_ATOMIC_INT_NATIVE = 1
100
101 // typedef sig_atomic_t PEGASUS_ATOMIC_TYPE ;
102
103
104 //-----------------------------------------------------------------
105 /// Conditionals to support native or generic read/write semaphores
106 //-----------------------------------------------------------------
107
108 #define PEGASUS_READWRITE_NATIVE = 1
109
110 typedef struct {
111     pthread_rwlock_t rwlock;
112     pthread_t owner;
113 } PEGASUS_RWLOCK_HANDLE;
114
115
116 //PEGASUS_NAMESPACE_BEGIN
117 inline void pegasus_yield(void)
118 {
119       sched_yield();
120 }
121
122
123 // pthreads cancellation calls 
124 inline void disable_cancel(void)
125 {
126    pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, NULL);
127 }
128
129 inline void enable_cancel(void)
130 {
131    pthread_setcanceltype(PTHREAD_CANCEL_DISABLE, NULL);
132 }
133
134
135 // the next two routines are macros that MUST SHARE the same stack frame
136 // they are implemented as macros by glibc. 
137 // native_cleanup_push( void (*func)(void *) ) ;
138 // these ALSO SET CANCEL STATE TO DEFER
139 //#define native_cleanup_push( func, arg ) \
140 //   pthread_cleanup_push_defer_np((func), arg)
141
142
143 // native cleanup_pop(Boolean execute) ; 
144 //#define native_cleanup_pop(execute) \
145 //   pthread_cleanup_pop_restore_np(execute)
146
147 inline void pegasus_sleep(int msec)
148 {
149     struct timespec wait;
150     wait.tv_sec = msec / 1000;
151     wait.tv_nsec = (msec % 1000) * 1000000;
152     nanosleep(&wait, NULL);
153 }
154
155 inline void init_crit(PEGASUS_CRIT_TYPE *crit)
156 {
157    pthread_mutex_init(&(crit->mut), NULL);
158 }
159
160 inline void enter_crit(PEGASUS_CRIT_TYPE *crit)
161 {
162    pthread_mutex_lock(&(crit->mut));
163 }
164
165 inline void try_crit(PEGASUS_CRIT_TYPE *crit)
166 {
167    pthread_mutex_trylock(&(crit->mut));
168 }
169
170 inline void exit_crit(PEGASUS_CRIT_TYPE *crit)
171 {
172    pthread_mutex_unlock(&(crit->mut));
173 }
174
175 inline void destroy_crit(PEGASUS_CRIT_TYPE *crit)
176 {
177    pthread_mutexattr_destroy(&(crit->mutatt));
178 }
179
180 static inline int pegasus_gettimeofday(struct timeval *tv) { return(gettimeofday(tv, NULL)); }
181
182 inline void exit_thread(PEGASUS_THREAD_RETURN rc)
183 {
184   pthread_exit(rc);
185 }
186
187 inline PEGASUS_THREAD_TYPE pegasus_thread_self(void) 
188
189    return(pthread_self());
190 }
191
192 // l10n start
193 typedef pthread_key_t PEGASUS_THREAD_KEY_TYPE;
194
195 inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
196 {
197         // Note: a destructor is not supported 
198         // (because not supported on Windows (?))
199         return pthread_key_create(key, NULL);
200
201
202 inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
203 {
204         return pthread_key_delete(key);
205
206
207 inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
208 {
209         return pthread_getspecific(key);
210
211
212 inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
213                                                                                  void * value)
214 {
215         return pthread_setspecific(key, value);
216
217 // l10n end
218
219 inline void destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
220 {
221    pthread_cancel(th);
222 }
223
224
225 PEGASUS_NAMESPACE_END
226
227 #endif // IPCAIXInclude