1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #ifndef _omi_thread_h
26 #define _omi_thread_h
27
28 #include "config.h"
29 //#include <string.h>
30 #include <common.h>
31
32 #if !defined(CONFIG_OS_WINDOWS)
33 # include <pthread.h>
34 # include <unistd.h>
35 # include <sys/time.h>
36 # include <sys/types.h>
37 #endif
38
39 BEGIN_EXTERNC
40
41 typedef void* (MI_CALL *ThreadProc)(void* param);
42
43 mike 1.1 #if defined(CONFIG_OS_WINDOWS)
44 typedef MI_Uint32 ThreadHandle;
45 typedef CRITICAL_SECTION Mutex;
46 #else
47 typedef pthread_t ThreadHandle;
48 typedef pthread_mutex_t Mutex;
49 #endif
50
51 /* creates a thread; returns a handle.
52 each successful 'create-thread' has to be followed by 'close-thread-handle' */
53 MI_Result Thread_Create(
54 ThreadProc proc,
55 void* proc_param,
56 ThreadHandle* selfOut);
57
58 /* closes thread handle and frees related system resources;
59 optionally, may wait for thread to complete */
60 MI_Result Thread_Destroy(
61 ThreadHandle self,
62 MI_Boolean wait );
63
64 mike 1.1 /* returns ID of the current thread */
65 ThreadHandle ThreadSelf();
66
67 /* Mutex operations */
68 MI_INLINE MI_Result Mutex_Init( Mutex* self )
69 {
70 #if defined(CONFIG_OS_WINDOWS)
71 InitializeCriticalSection(self);
72 #else
73 if (0 != pthread_mutex_init(self, NULL))
74 return MI_RESULT_FAILED;
75 #endif
76 return MI_RESULT_OK;
77 }
78
79 MI_INLINE MI_Result Mutex_Destroy( Mutex* self )
80 {
81 #if defined(CONFIG_OS_WINDOWS)
82 DeleteCriticalSection(self);
83 #else
84 if (0 != pthread_mutex_destroy(self))
85 mike 1.1 return MI_RESULT_FAILED;
86 #endif
87 return MI_RESULT_OK;
88 }
89
90 MI_INLINE void Mutex_Lock( Mutex* self )
91 {
92 #if defined(CONFIG_OS_WINDOWS)
93 EnterCriticalSection(self);
94 #else
95 pthread_mutex_lock(self);
96 #endif
97 }
98
99 MI_INLINE void Mutex_Unlock( Mutex* self )
100 {
101 #if defined(CONFIG_OS_WINDOWS)
102 LeaveCriticalSection(self);
103 #else
104 pthread_mutex_unlock(self);
105 #endif
106 mike 1.1 }
107
108 MI_INLINE void Sleep_ms(MI_Uint64 ms_sec)
109 {
110 #if defined(CONFIG_OS_WINDOWS)
111 Sleep((DWORD)ms_sec);
112 #else
113 struct timespec rqtp;
114
115 rqtp.tv_sec = (long)(ms_sec/1000);
116 rqtp.tv_nsec = (long)((ms_sec%1000)*1000*1000);
117
118 nanosleep(&rqtp, NULL);
119 #endif
120 }
121
122 END_EXTERNC
123
124 #endif /* _omi_thread_h */
|