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)
28 // Modified By: Arthur Pichlkostner
29 // (checked in: Markus Mueller sedgewick_de@yahoo.de)
30 // Mary Hinton (m.hinton@verizon.net)
31 // Steve Hills (steve.hills@ncr.com)
32 //%/////////////////////////////////////////////////////////////////////////////
35 #define _WIN32_WINNT 0x0400
40 // PLEASE DO NOT REMOVE THE DEFINTION OF FD_SETSIZE!
44 # define FD_SETSIZE 1024
53 #include <sys/types.h>
54 #include <sys/timeb.h>
56 #include <Pegasus/Common/Config.h>
57 #include <Pegasus/Common/Linkage.h>
60 typedef CRITICAL_SECTION PEGASUS_CRIT_TYPE;
61 typedef HANDLE PEGASUS_SEMAPHORE_TYPE;
62 typedef HANDLE PEGASUS_THREAD_TYPE;
63 typedef HANDLE PEGASUS_MUTEX_TYPE;
66 PEGASUS_SEMAPHORE_TYPE sem;
67 PEGASUS_THREAD_TYPE owner;
68 } PEGASUS_SEM_HANDLE ;
72 PEGASUS_THREAD_TYPE owner;
73 } PEGASUS_MUTEX_HANDLE ;
76 typedef void *PEGASUS_CLEANUP_HANDLE;
78 typedef unsigned PEGASUS_THREAD_RETURN;
80 #define PTHREAD_MUTEX_TIMED_NP
82 #define PEGASUS_THREAD_CDECL __stdcall
85 PEGASUS_THREAD_TYPE thid;
87 } PEGASUS_THREAD_HANDLE ;
90 //-----------------------------------------------------------------
91 /// Conditionals to support native conditional semaphore object
92 //-----------------------------------------------------------------
94 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
96 #define PEGASUS_CONDITIONAL_NATIVE = 1
97 typedef HANDLE PEGASUS_COND_TYPE;
100 PEGASUS_COND_TYPE cond;
101 PEGASUS_THREAD_TYPE owner;
102 } PEGASUS_COND_HANDLE;
104 #endif // platform conditional type
106 //-----------------------------------------------------------------
107 /// Conditionals to support native or generic atomic variables
108 //-----------------------------------------------------------------
110 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
111 #define PEGASUS_ATOMIC_INT_NATIVE = 1
113 typedef LONG PEGASUS_ATOMIC_TYPE ;
115 #endif // platform atomic type
117 //-----------------------------------------------------------------
118 /// Conditionals to support native or generic read/write semaphores
119 //-----------------------------------------------------------------
121 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
123 // no native rw lock for windows -- use the generic one
125 #endif // platform read/write type
130 // long int tv_sec; long int tv_usec;
142 inline int pegasus_gettimeofday(struct timeval *tv)
144 struct _timeb timebuffer;
147 _ftime( &timebuffer );
148 tv->tv_sec = timebuffer.time;
149 tv->tv_usec = ( timebuffer.millitm * 1000 );
153 // Markus: new implementation with higher resolution
154 // that is needed for performance statistics
159 THIS ROUTINE IS BROKEN << Thu Mar 20 10:24:14 2003 mdd >>
160 inline int pegasus_gettimeofday(struct timeval *tv)
165 LARGE_INTEGER frequency;
166 if (!QueryPerformanceFrequency(&frequency)){
167 struct _timeb timebuffer;
168 _ftime( &timebuffer );
169 tv->tv_sec = timebuffer.time;
170 tv->tv_usec = ( timebuffer.millitm * 1000 );
173 LARGE_INTEGER counter;
174 QueryPerformanceCounter(&counter);
175 tv->tv_sec = (__int64)((counter.QuadPart)/(frequency.QuadPart));
176 tv->tv_usec = ((__int64)
177 ((counter.QuadPart)*1000000/(frequency.QuadPart)))%1000000;
183 inline int PEGASUS_COMMON_LINKAGE gettimeofday(struct timeval *tv, struct timezone *tz)
185 return(pegasus_gettimeofday(tv));
188 PEGASUS_NAMESPACE_BEGIN
190 inline PEGASUS_COMMON_LINKAGE void pegasus_yield(void)
195 // pthreads cancellation calls
196 inline PEGASUS_COMMON_LINKAGE void disable_cancel(void)
201 inline PEGASUS_COMMON_LINKAGE void enable_cancel(void)
207 // Windows does not have equivalent functionality with Unix-like
208 // operating systems. Be careful using these next two
209 // macros. There is no pop routine in windows. Further, windows
210 // does not allow passing parameters to exit functions. !!
211 inline PEGASUS_COMMON_LINKAGE void native_cleanup_push( void (*)(void *), void *) { ; }
213 inline PEGASUS_COMMON_LINKAGE void native_cleanup_pop(Boolean) { ; }
215 inline void PEGASUS_COMMON_LINKAGE init_crit(PEGASUS_CRIT_TYPE *crit)
217 InitializeCriticalSection(crit);
220 inline void PEGASUS_COMMON_LINKAGE enter_crit(PEGASUS_CRIT_TYPE *crit)
222 EnterCriticalSection(crit);
225 inline void PEGASUS_COMMON_LINKAGE try_crit(PEGASUS_CRIT_TYPE *crit)
227 EnterCriticalSection(crit);
230 inline void PEGASUS_COMMON_LINKAGE exit_crit(PEGASUS_CRIT_TYPE *crit)
232 LeaveCriticalSection(crit);
235 inline void PEGASUS_COMMON_LINKAGE destroy_crit(PEGASUS_CRIT_TYPE *crit)
237 DeleteCriticalSection(crit);
240 inline PEGASUS_THREAD_TYPE PEGASUS_COMMON_LINKAGE pegasus_thread_self(void)
242 return((PEGASUS_THREAD_TYPE)GetCurrentThreadId());
246 typedef DWORD PEGASUS_THREAD_KEY_TYPE;
248 inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
250 // Note: destructor is not supported
257 inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
264 inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
266 return TlsGetValue(key);
269 inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
272 if (TlsSetValue(key, value))
278 inline void PEGASUS_COMMON_LINKAGE exit_thread(PEGASUS_THREAD_RETURN rc)
283 inline void PEGASUS_COMMON_LINKAGE pegasus_sleep(int ms)
291 struct _timeb end, now;
293 end.time += (ms / 1000);
302 } while( end.millitm > now.millitm && end.time >= now.time);
306 inline void PEGASUS_COMMON_LINKAGE destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
308 TerminateThread(th, rc);
312 PEGASUS_NAMESPACE_END