PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / Common / IPCWindows.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)
27 //
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 //%/////////////////////////////////////////////////////////////////////////////
33
34
35 #define _WIN32_WINNT 0x0400
36
37 #include <process.h>  
38
39 //
40 // PLEASE DO NOT REMOVE THE DEFINTION OF FD_SETSIZE!
41 //
42
43 #ifndef FD_SETSIZE
44 # define FD_SETSIZE 1024
45 #endif
46
47 #include <windows.h>
48 #ifndef _WINSOCKAPI_
49 #include <winsock2.h>
50 #endif
51 #include <winbase.h>
52 #include <stdlib.h>
53 #include <sys/types.h>
54 #include <sys/timeb.h> 
55 #include <errno.h>
56 #include <Pegasus/Common/Config.h>
57 #include <Pegasus/Common/Linkage.h>
58
59
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;
64
65 typedef struct {
66       PEGASUS_SEMAPHORE_TYPE  sem;
67       PEGASUS_THREAD_TYPE  owner;
68 } PEGASUS_SEM_HANDLE ;
69
70 typedef struct {
71       HANDLE  mut;
72       PEGASUS_THREAD_TYPE owner;
73 } PEGASUS_MUTEX_HANDLE ;
74
75
76 typedef void *PEGASUS_CLEANUP_HANDLE;
77
78 typedef unsigned PEGASUS_THREAD_RETURN;
79
80 #define PTHREAD_MUTEX_TIMED_NP
81
82 #define PEGASUS_THREAD_CDECL __stdcall
83
84 typedef struct {
85       PEGASUS_THREAD_TYPE thid;
86       void * thatt;
87 } PEGASUS_THREAD_HANDLE ;
88
89
90 //-----------------------------------------------------------------
91 /// Conditionals to support native conditional semaphore object
92 //-----------------------------------------------------------------
93
94 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
95
96 #define PEGASUS_CONDITIONAL_NATIVE = 1 
97 typedef HANDLE PEGASUS_COND_TYPE;
98
99 typedef struct {
100       PEGASUS_COND_TYPE cond;
101       PEGASUS_THREAD_TYPE owner;
102 } PEGASUS_COND_HANDLE;
103
104 #endif // platform conditional  type
105
106 //-----------------------------------------------------------------
107 /// Conditionals to support native or generic atomic variables
108 //-----------------------------------------------------------------
109
110 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
111 #define PEGASUS_ATOMIC_INT_NATIVE = 1
112
113 typedef LONG  PEGASUS_ATOMIC_TYPE ;
114
115 #endif // platform atomic type
116
117 //-----------------------------------------------------------------
118 /// Conditionals to support native or generic read/write semaphores
119 //-----------------------------------------------------------------
120
121 #if defined(PEGASUS_PLATFORM_WIN32_IX86_MSVC)
122
123 // no native rw lock for windows -- use the generic one 
124
125 #endif // platform read/write type
126
127
128 //struct timeval 
129 //{
130 //      long int tv_sec;      long int tv_usec;
131 //};
132
133 struct timezone
134 {
135       int tz_minuteswest;
136       int tz_dsttime;
137 };
138
139
140 // excluded
141
142 inline int pegasus_gettimeofday(struct timeval *tv)
143 {
144         struct _timeb timebuffer;   
145         if (tv == NULL)
146                 return(-1);
147         _ftime( &timebuffer );
148         tv->tv_sec = timebuffer.time;
149         tv->tv_usec = ( timebuffer.millitm * 1000 );
150         return(0);
151
152
153 // Markus: new implementation with higher resolution
154 // that is needed for performance statistics
155 /*
156
157
158
159 THIS ROUTINE IS BROKEN << Thu Mar 20 10:24:14 2003 mdd >> 
160 inline int pegasus_gettimeofday(struct timeval *tv)
161 {
162    if (tv == NULL){
163                    return(-1);
164    }
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 );
171            return(0);
172    } else {
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;
178       return(0);
179    }
180 }
181         
182 */
183 inline int PEGASUS_COMMON_LINKAGE gettimeofday(struct timeval *tv, struct timezone *tz)
184 {
185   return(pegasus_gettimeofday(tv));
186 }
187
188 PEGASUS_NAMESPACE_BEGIN
189
190 inline PEGASUS_COMMON_LINKAGE void pegasus_yield(void)
191 {
192   Sleep(0);
193 }
194
195 // pthreads cancellation calls 
196 inline  PEGASUS_COMMON_LINKAGE void disable_cancel(void)
197 {
198   ;
199 }
200
201 inline  PEGASUS_COMMON_LINKAGE void enable_cancel(void)
202 {
203   ;
204 }
205
206
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 *) { ; }
212
213 inline PEGASUS_COMMON_LINKAGE void native_cleanup_pop(Boolean) { ; }
214
215 inline void PEGASUS_COMMON_LINKAGE init_crit(PEGASUS_CRIT_TYPE *crit)
216 {
217    InitializeCriticalSection(crit);
218 }
219
220 inline void PEGASUS_COMMON_LINKAGE enter_crit(PEGASUS_CRIT_TYPE *crit)
221 {
222    EnterCriticalSection(crit);
223 }
224
225 inline void PEGASUS_COMMON_LINKAGE try_crit(PEGASUS_CRIT_TYPE *crit)
226 {
227   EnterCriticalSection(crit); 
228 }
229
230 inline void PEGASUS_COMMON_LINKAGE exit_crit(PEGASUS_CRIT_TYPE *crit)
231 {
232    LeaveCriticalSection(crit);
233 }
234
235 inline void PEGASUS_COMMON_LINKAGE destroy_crit(PEGASUS_CRIT_TYPE *crit)
236 {
237    DeleteCriticalSection(crit);
238 }
239
240 inline PEGASUS_THREAD_TYPE PEGASUS_COMMON_LINKAGE pegasus_thread_self(void) 
241
242    return((PEGASUS_THREAD_TYPE)GetCurrentThreadId());
243 }
244
245 // l10n start
246 typedef DWORD PEGASUS_THREAD_KEY_TYPE;
247
248 inline Uint32 pegasus_key_create(PEGASUS_THREAD_KEY_TYPE * key)
249 {
250         // Note: destructor is not supported
251         *key = TlsAlloc();
252         if (*key == -1)
253                 return 1;
254         return 0;       
255
256
257 inline Uint32 pegasus_key_delete(PEGASUS_THREAD_KEY_TYPE key)
258 {
259         if (TlsFree(key))
260                 return 0;
261         return 1;                       
262
263
264 inline void * pegasus_get_thread_specific(PEGASUS_THREAD_KEY_TYPE key)
265 {
266         return TlsGetValue(key);
267
268
269 inline Uint32 pegasus_set_thread_specific(PEGASUS_THREAD_KEY_TYPE key,
270                                                                                  void * value)
271 {
272         if (TlsSetValue(key, value))
273                 return 0;
274         return 1;
275
276 // l10n end
277
278 inline void PEGASUS_COMMON_LINKAGE exit_thread(PEGASUS_THREAD_RETURN rc)
279 {
280   _endthreadex(rc);
281 }
282
283 inline void PEGASUS_COMMON_LINKAGE pegasus_sleep(int ms)
284 {
285    if(ms == 0)
286    {
287       Sleep(0);
288       return;
289    }
290    
291    struct _timeb end, now;
292    _ftime( &end );
293    end.time += (ms / 1000);
294    ms -= (ms / 1000);
295    end.millitm += ms;
296         
297    do 
298    {
299       Sleep(0);
300       _ftime(&now);
301       
302    } while( end.millitm > now.millitm && end.time >= now.time);
303 }
304
305
306 inline void PEGASUS_COMMON_LINKAGE destroy_thread(PEGASUS_THREAD_TYPE th, PEGASUS_THREAD_RETURN rc)
307 {
308    TerminateThread(th, rc);
309 }
310
311
312 PEGASUS_NAMESPACE_END
313
314
315