(file) Return to IPCHpux.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.12 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.2  //
  3 karl  1.8  // 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 karl  1.6  // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.8  // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.9  // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.12 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.2  //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 kumpf 1.3  // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 mike  1.2  // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20            // 
 21 kumpf 1.3  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.2  // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 kumpf 1.3  // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 mike  1.2  // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            // Author: Mike Day (mdday@us.ibm.com) and Roger Kumpf (roger_kumpf@hp.com)
 33            //
 34            // Modified By:
 35            //
 36            //%/////////////////////////////////////////////////////////////////////////////
 37            
 38            
 39            #include <sched.h>
 40            #include <pthread.h>
 41            #include <semaphore.h>
 42            #include <signal.h>
 43            #include <errno.h>
 44            #include <sys/time.h>
 45            #include <time.h>
 46            
 47            
 48 mike  1.2  typedef sem_t PEGASUS_SEMAPHORE_TYPE;
 49            typedef pthread_t PEGASUS_THREAD_TYPE;
 50            typedef pthread_mutex_t PEGASUS_MUTEX_TYPE;
 51            
 52            typedef struct {
 53                sem_t sem;
 54                pthread_t owner;
 55            } PEGASUS_SEM_HANDLE ;
 56            
 57            typedef struct {
 58                pthread_mutex_t mut;
 59                pthread_mutexattr_t mutatt;
 60                pthread_t owner;
 61            } PEGASUS_MUTEX_HANDLE ;
 62            
 63            typedef PEGASUS_MUTEX_HANDLE  PEGASUS_CRIT_TYPE;
 64            
 65            typedef __pthread_cleanup_handler_t PEGASUS_CLEANUP_HANDLE ;
 66            typedef void *PEGASUS_THREAD_RETURN;
 67            
 68            #define PEGASUS_THREAD_CDECL
 69 mike  1.2  
 70            typedef struct {
 71                pthread_t thid;
 72                pthread_attr_t thatt;
 73            } PEGASUS_THREAD_HANDLE ;
 74            
 75            //-----------------------------------------------------------------
 76            /// Conditionals to support native or generic Conditional Semaphore
 77            //-----------------------------------------------------------------
 78            
 79 kumpf 1.7  #define PEGASUS_CONDITIONAL_NATIVE
 80 mike  1.2  
 81            typedef pthread_cond_t PEGASUS_COND_TYPE;
 82            
 83            typedef struct {
 84                pthread_cond_t cond;
 85                pthread_t owner;
 86            } PEGASUS_COND_HANDLE;
 87            
 88            
 89            //-----------------------------------------------------------------
 90            /// Conditionals to support native or generic atomic variables
 91            //-----------------------------------------------------------------
 92            
 93 kumpf 1.7  // #define PEGASUS_ATOMIC_INT_NATIVE
 94 mike  1.2  
 95            
 96            //-----------------------------------------------------------------
 97            /// Conditionals to support native or generic read/write semaphores
 98            //-----------------------------------------------------------------
 99            
100 kumpf 1.7  #define PEGASUS_READWRITE_NATIVE
101 mike  1.2  
102            typedef struct {
103                pthread_rwlock_t rwlock;
104                pthread_t owner;
105            } PEGASUS_RWLOCK_HANDLE;
106            
107            
108            PEGASUS_NAMESPACE_BEGIN
109            inline void pegasus_yield(void)
110            {
111                  sched_yield();
112            }
113            
114            
115            // pthreads cancellation calls 
116            inline void disable_cancel(void)
117            {
118 kumpf 1.11    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
119 mike  1.2  }
120            
121            inline void enable_cancel(void)
122            {
123 kumpf 1.11    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
124 mike  1.2  }
125            
126            
127            // the next two routines are macros that MUST SHARE the same stack frame
128            // they are implemented as macros by glibc. 
129            // native_cleanup_push( void (*func)(void *) ) ;
130            // these ALSO SET CANCEL STATE TO DEFER
131            #define native_cleanup_push( func, arg ) \
132               pthread_cleanup_push_defer_np((func), arg)
133            
134            // native cleanup_pop(Boolean execute) ; 
135            #define native_cleanup_pop(execute) \
136               pthread_cleanup_pop_restore_np(execute)
137            
138            inline void pegasus_sleep(int msec)
139            {
140                struct timespec wait;
141                wait.tv_sec = msec / 1000;
142 kumpf 1.4      wait.tv_nsec = (msec % 1000) * 1000000;
143 mike  1.2      nanosleep(&wait, NULL);
144            }
145            
146            inline void init_crit(PEGASUS_CRIT_TYPE *crit)
147            {
148               pthread_mutexattr_init(&(crit->mutatt));
149               pthread_mutexattr_setspin_np(&(crit->mutatt), PTHREAD_MUTEX_SPINONLY_NP);
150               pthread_mutex_init(&(crit->mut), &(crit->mutatt));
151               crit->owner = 0;
152            }
153            
154            inline void enter_crit(PEGASUS_CRIT_TYPE *crit)
155            {
156               pthread_mutex_lock(&(crit->mut));
157            }
158            
159            inline void try_crit(PEGASUS_CRIT_TYPE *crit)
160            {
161               pthread_mutex_trylock(&(crit->mut));
162            }
163            
164 mike  1.2  inline void exit_crit(PEGASUS_CRIT_TYPE *crit)
165            {
166               pthread_mutex_unlock(&(crit->mut));
167            }
168            
169            inline void destroy_crit(PEGASUS_CRIT_TYPE *crit)
170            {
171               while( EBUSY == pthread_mutex_destroy(&(crit->mut)))
172               {
173                  pegasus_yield();
174               }
175               pthread_mutexattr_destroy(&(crit->mutatt));
176            }
177            
178            static inline int pegasus_gettimeofday(struct timeval *tv) { return(gettimeofday(tv, NULL)); }
179            
180            inline void exit_thread(PEGASUS_THREAD_RETURN rc)
181            {
182              pthread_exit(rc);
183            }
184            
185 mike  1.2  inline PEGASUS_THREAD_TYPE pegasus_thread_self(void) 
186            { 
187               return(pthread_self());
188            }
189            
190 chuck 1.5  // l10n start
191            typedef pthread_key_t PEGASUS_THREAD_KEY_TYPE;
192            
193            inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
194            {
195            	// Note: a destructor is not supported 
196            	// (because not supported on Windows (?))
197            	return pthread_key_create(key, NULL);
198            } 
199            
200            inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
201            {
202            	return pthread_key_delete(key);
203            } 
204            
205            inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
206            {
207            	return pthread_getspecific(key);
208            } 
209            
210            inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
211 chuck 1.5  										 void * value)
212            {
213            	return pthread_setspecific(key, value);
214            } 
215            // l10n end
216            
217 mike  1.2  inline void destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
218            {
219               pthread_cancel(th);
220            }
221            
222            
223            PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2