1 keith.petley 1.1 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 keith.petley 1.1 //==============================================================================
23 //
24 // Author: Mike Day (mdday@us.ibm.com) and Roger Kumpf (roger_kumpf@hp.com)
25 //
26 // Modified By: Keith Petley (keithp@veritas.com)
27 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30
31 #include <sched.h>
32 #include <pthread.h>
33 #include <semaphore.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <sys/time.h>
37 #include <time.h>
38
39
40 typedef sem_t PEGASUS_SEMAPHORE_TYPE;
41 typedef pthread_t PEGASUS_THREAD_TYPE;
42 typedef pthread_mutex_t PEGASUS_MUTEX_TYPE;
43 keith.petley 1.1
44 typedef struct {
45 sem_t sem;
46 pthread_t owner;
47 } PEGASUS_SEM_HANDLE ;
48
49 typedef struct {
50 pthread_mutex_t mut;
51 pthread_mutexattr_t mutatt;
52 pthread_t owner;
53 } PEGASUS_MUTEX_HANDLE ;
54
55 typedef PEGASUS_MUTEX_HANDLE PEGASUS_CRIT_TYPE;
56
57 typedef void *PEGASUS_CLEANUP_HANDLE ;
58 typedef void *PEGASUS_THREAD_RETURN;
59
60 #define PEGASUS_THREAD_CDECL
61
62 typedef struct {
63 pthread_t thid;
64 keith.petley 1.1 pthread_attr_t thatt;
65 } PEGASUS_THREAD_HANDLE ;
66
67 //-----------------------------------------------------------------
68 /// Conditionals to support native or generic Conditional Semaphore
69 //-----------------------------------------------------------------
70
71 #define PEGASUS_CONDITIONAL_NATIVE 1
72
73 typedef pthread_cond_t PEGASUS_COND_TYPE;
74
75 typedef struct {
76 pthread_cond_t cond;
77 pthread_t owner;
78 } PEGASUS_COND_HANDLE;
79
80
81 //-----------------------------------------------------------------
82 /// Conditionals to support native or generic atomic variables
83 //-----------------------------------------------------------------
84
85 keith.petley 1.1 // linux offers a built-in integer type for atomic access
86 // other unix platforms HPUX, AIX, may have different types
87 // implementors should use the native type for faster operations
88
89 // #define PEGASUS_ATOMIC_INT_NATIVE = 1
90
91 // typedef sig_atomic_t PEGASUS_ATOMIC_TYPE ;
92
93
94 //-----------------------------------------------------------------
95 /// Conditionals to support native or generic read/write semaphores
96 //-----------------------------------------------------------------
97
98 #ifndef SUNOS_5_6
99 #define PEGASUS_READWRITE_NATIVE 1
100
101 typedef struct {
102 pthread_rwlock_t rwlock;
103 pthread_t owner;
104 } PEGASUS_RWLOCK_HANDLE;
105 #endif //SUNOS_5_6
106 keith.petley 1.1
107 PEGASUS_NAMESPACE_BEGIN
108 inline void pegasus_yield(void)
109 {
110 sched_yield();
111 }
112
113
114 // pthreads cancellation calls
115 inline void disable_cancel(void)
116 {
117 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
118 }
119
120 inline void enable_cancel(void)
121 {
122 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
123 }
124
125 inline void pegasus_sleep(int msec)
126 {
127 keith.petley 1.1 struct timespec wait;
128 wait.tv_sec = msec / 1000;
129 wait.tv_nsec = (msec % 1000) * 1000000;
130 nanosleep(&wait, NULL);
131 }
132
133 static inline int pegasus_gettimeofday(struct timeval *tv)
134 {
135 return(gettimeofday(tv, NULL));
136 }
137
138 inline void exit_thread(PEGASUS_THREAD_RETURN rc)
139 {
140 pthread_exit(rc);
141 }
142
143 inline PEGASUS_THREAD_TYPE pegasus_thread_self(void)
144 {
145 return(pthread_self());
146 }
147
148 keith.petley 1.1 // l10n start
149 typedef pthread_key_t PEGASUS_THREAD_KEY_TYPE;
150
151 inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
152 {
153 // Note: a destructor is not supported
154 // (because not supported on Windows (?))
155 return pthread_key_create(key, NULL);
156 }
157
158 inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
159 {
160 return pthread_key_delete(key);
161 }
162
163 inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
164 {
165 return pthread_getspecific(key);
166 }
167
168 inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
169 keith.petley 1.1 void * value)
170 {
171 return pthread_setspecific(key, value);
172 }
173 // l10n end
174
175 inline void destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
176 {
177 pthread_cancel(th);
178 }
179
180
181 PEGASUS_NAMESPACE_END
|