|
1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // This header is to create platform independent |
|
15 // threading / synchronization API types and values. |
|
16 |
|
17 #ifndef PSU_PLATFORMTHREADING_H |
|
18 #define PSU_PLATFORMTHREADING_H |
|
19 |
|
20 #ifdef WIN32 |
|
21 #include <windows.h> |
|
22 #else |
|
23 #include <pthread.h> |
|
24 #include <semaphore.h> |
|
25 #include <signal.h> |
|
26 #endif |
|
27 |
|
28 #include "platformtypes.h" |
|
29 |
|
30 namespace Psu |
|
31 { |
|
32 |
|
33 #ifdef WIN32 |
|
34 |
|
35 typedef HANDLE PLATFORM_THREAD_T; |
|
36 typedef DWORD PLATFORM_THREADFUNC_RETTYPE; |
|
37 typedef LPTHREAD_START_ROUTINE PLATFORM_THREADFUNC; |
|
38 typedef CRITICAL_SECTION PLATFORM_MUTEX_T; |
|
39 typedef HANDLE PLATFORM_SEMAPHORE_T; |
|
40 typedef DWORD STATUS_T; |
|
41 |
|
42 #ifdef __GNUG__ |
|
43 typedef void (*PLATFORM_INTERRUPT_HANDLER)(ULONG_PTR); |
|
44 #else |
|
45 typedef VOID CALLBACK PLATFORM_INTERRUPT_HANDLER (__in ULONG_PTR); |
|
46 #endif |
|
47 |
|
48 #else // LINUX |
|
49 |
|
50 typedef pthread_t PLATFORM_THREAD_T; |
|
51 typedef void * PLATFORM_THREADFUNC_RETTYPE; |
|
52 typedef void * (*PLATFORM_THREADFUNC)(void *); |
|
53 typedef pthread_mutex_t PLATFORM_MUTEX_T; |
|
54 typedef sem_t PLATFORM_SEMAPHORE_T; |
|
55 typedef int STATUS_T; |
|
56 |
|
57 typedef void (*PLATFORM_INTERRUPT_HANDLER)(int); |
|
58 |
|
59 #define WINAPI |
|
60 |
|
61 #endif |
|
62 |
|
63 #ifndef WIN32 |
|
64 extern const int MicrosecsInMillisec; |
|
65 #endif |
|
66 |
|
67 |
|
68 /** |
|
69 * Creates a thread with the default attributes. |
|
70 */ |
|
71 int platform_create_simple_thread(PLATFORM_THREAD_T * pThreadHandle, |
|
72 PLATFORM_THREADFUNC pFunc, |
|
73 void * param); |
|
74 |
|
75 /** |
|
76 * Releases a thread (does not kill it). |
|
77 */ |
|
78 void platform_release_thread(PLATFORM_THREAD_T threadHandle); |
|
79 |
|
80 /** |
|
81 * Waits for a thread to terminate. |
|
82 */ |
|
83 STATUS_T platform_join_thread(PLATFORM_THREAD_T threadHandle); |
|
84 |
|
85 /** |
|
86 * Initializes a mutex (CRITICAL_SECTION on windows). |
|
87 */ |
|
88 void platform_mutex_init(PLATFORM_MUTEX_T * pMutex); |
|
89 |
|
90 /** |
|
91 * Destroys a mutex (CRITICAL_SECTION on windows). |
|
92 */ |
|
93 void platform_mutex_destroy(PLATFORM_MUTEX_T * pMutex); |
|
94 |
|
95 /** |
|
96 *Locks a mutex ("enters" a CRITICAL_SECTION on windows). |
|
97 */ |
|
98 void platform_mutex_lock(PLATFORM_MUTEX_T * pMutex); |
|
99 |
|
100 /** |
|
101 * Unlocks a mutex ("exits" a CRITICAL_SECTION on windows). |
|
102 */ |
|
103 void platform_mutex_unlock(PLATFORM_MUTEX_T * pMutex); |
|
104 |
|
105 |
|
106 /** |
|
107 * Sleeps for a certain duration for given milliseconds. |
|
108 */ |
|
109 void platform_sleep(int millisecs); |
|
110 |
|
111 /** |
|
112 * Creates a semaphore with the default attributes. |
|
113 */ |
|
114 STATUS_T platform_create_semaphore(PLATFORM_SEMAPHORE_T& semHandle, |
|
115 int initialCount,int maximumCount); |
|
116 |
|
117 |
|
118 /** |
|
119 * wait for the semaphore signal |
|
120 */ |
|
121 STATUS_T platform_wait_for_signal(PLATFORM_SEMAPHORE_T& semHandle); |
|
122 |
|
123 /** |
|
124 * Posts a semaphore signal so that the pending thread can continue |
|
125 */ |
|
126 void platform_signal_semaphore(PLATFORM_SEMAPHORE_T& semHandle); |
|
127 |
|
128 /** |
|
129 * releases a semaphore object/handle. |
|
130 */ |
|
131 void platform_release_semaphore(PLATFORM_SEMAPHORE_T& semHandle); |
|
132 |
|
133 /** |
|
134 * do an interruptable sleep |
|
135 * @param length the length thread should sleep, in milliseconds |
|
136 * @return 0 if the sleep expires normally, non-zero if the sleep has |
|
137 * been interrupted |
|
138 */ |
|
139 int32_t platform_interruptable_sleep(int64_t length); |
|
140 |
|
141 /** |
|
142 * signals a thread to interrupt a sleep |
|
143 * @param handle PLATFORM_THREAD_T handle to the thread |
|
144 * @return whether the interrupt succeeded or not |
|
145 */ |
|
146 bool platform_interrupt_sleep(PLATFORM_THREAD_T handle); |
|
147 |
|
148 #ifndef WIN32 |
|
149 class SignalUSR1Handler |
|
150 { |
|
151 private: |
|
152 SignalUSR1Handler(); |
|
153 ~SignalUSR1Handler(); |
|
154 |
|
155 struct sigaction sa; |
|
156 struct sigaction old; |
|
157 |
|
158 public: |
|
159 static SignalUSR1Handler instance; |
|
160 }; |
|
161 #endif |
|
162 } |
|
163 |
|
164 #endif |