Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This is already exported from the corresponding .h (./src/include/surf/surf.h)
[simgrid.git] / src / xbt / win_thread.h
1 /* 
2  * Copyright (c) 2007 Malek CHERIER. All rights reserved.                                       
3  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package.     
7  */
8  
9 #ifndef XBT_WIN_THREAD_H
10 #define XBT_WIN_THREAD_H
11
12 #include <windows.h>
13 #include "xbt/sysdep.h"
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /* type of function pointeur used */
20 typedef  DWORD WINAPI (*pfn_start_routine_t)(void*);
21
22 typedef struct s_win_thread_key s_win_thread_key_t,* win_thread_key_t;
23 typedef struct s_win_thread_keys s_win_thread_keys_t,* win_thread_keys_t;
24
25 /*
26  * This structure represents a windows thread.
27  */
28 typedef struct s_win_thread
29 {
30         HANDLE handle;                  /* the win thread handle        */
31         unsigned long id;               /* the win thread id            */
32         win_thread_keys_t keys; 
33 }s_win_thread_t,* win_thread_t;
34
35 struct s_win_thread_keys
36 {
37         win_thread_key_t* keys;
38         win_thread_key_t front;
39         win_thread_key_t back;
40 };
41
42 typedef struct s_win_thread_entry
43 {
44         HANDLE handle;
45         win_thread_t thread;
46         win_thread_t next;
47         win_thread_t prev;
48 }s_win_thread_entry_t,win_thread_entry_t;
49
50 typedef struct s_win_threads
51 {
52         win_thread_entry_t* threads;
53         win_thread_entry_t front;
54         win_thread_entry_t back;
55         unsigned int size;
56         CRITICAL_SECTION lock;
57 }s_win_threads_t,* win_threads_t;
58
59         
60 /*
61  * This structure simulates a pthread cond.
62  */
63
64  enum
65  {
66     SIGNAL = 0,
67     BROADCAST = 1,
68     MAX_EVENTS = 2
69  };
70
71 typedef struct s_win_thread_cond
72 {
73         HANDLE events[MAX_EVENTS];
74
75         unsigned int waiters_count;                     /* the number of waiters                        */
76         CRITICAL_SECTION waiters_count_lock;/* protect access to waiters_count  */
77 }s_win_thread_cond_t,* win_thread_cond_t;
78
79
80 /* type of pointer function used by the 
81  * win_thread_key_create() function.
82  */
83 typedef void (*pfn_key_dstr_func_t) (void*);
84
85 /*
86  * This structure represents a windows key
87  */
88 struct s_win_thread_key
89 {
90         unsigned long value;                    /* the key value */
91         pfn_key_dstr_func_t dstr_func;  /* the pointer to the destruction function */
92         win_thread_key_t next;
93         win_thread_key_t prev;
94 };
95
96
97 /*
98  * This structure represents a windows mutext
99  * remark : only encapsulate the mutext handle.
100  */
101 typedef struct s_win_thread_mutex
102 {
103         CRITICAL_SECTION lock;
104 }s_win_thread_mutex_t,* win_thread_mutex_t;
105
106 /*
107  * Windows thread connected functions.
108  */
109
110 /*
111  * Create a win thread.
112  * @param thread The address of the thread to create
113  * @param start_routine The thread function.
114  * @param param A optional pointer to the thread function parameters.
115  * @return If successful the function returns 0. Otherwise the function
116  *  return 1.
117  */ 
118 int
119 win_thread_create(win_thread_t* thread, pfn_start_routine_t start_routine,void* param);
120
121 /*
122  * Terminate the win thread.
123  * @param thread An address to the thread to exit.
124  * @param exit_code The exit code returned by the thread.
125  * @return This function always returns 0;
126  */ 
127 int
128 win_thread_exit(win_thread_t* thread,unsigned long exit_code);
129
130 /*
131  * Return the identifier of the current thread.
132  */
133 win_thread_t
134 win_thread_self(void);
135
136 /*
137  * Windows mutex connected functions;
138  */
139  
140 /*
141  * Create a windows mutex.
142  * @param mutex The address to the mutex to create.
143  * @return If successful the function returns 0. Otherwise
144  *      the function returns 1.
145  */
146 int
147 win_thread_mutex_init(win_thread_mutex_t* mutex);
148
149 /*
150  * Lock a windows mutex.
151  * @param mutex The address to the mutex to lock.
152  * @return If successful the function returns 0.
153  *  Otherwise the function return 1.
154  */
155 int
156 win_thread_mutex_lock(win_thread_mutex_t* mutex);
157
158 /*
159  * Unlock a windows mutex.
160  * @param mutex The address to the mutex to unlock.
161  * @return If successful the function returns 0.
162  *  Otherwise the function return 1.
163  */
164 int
165 win_thread_mutex_unlock(win_thread_mutex_t* mutex);
166
167 /* 
168  * Destroy a windows mutex.
169  * @param mutex The address of the mutex to destroy.
170  * @return If successful the function return 0.
171  *  Otherwise the function returns 1.
172  */
173 int
174 win_thread_mutex_destroy(win_thread_mutex_t* mutex);
175
176 /*
177  * Condition connected functions.
178  */
179
180 /*
181  * Create a condition.
182  * @param cond The address to the condition to create.
183  * @return If successful the function returns 0.
184  *  Otherwise the function returns 1.
185  */
186 int
187 win_thread_cond_init(win_thread_cond_t* cond);
188
189 /*
190  * Wait about a condition.
191  * @param cond The address to the condition to wait about.
192  * @param mutex The address to the mutex associated to the condition.
193  * @return If successful the function returns 0.
194  *  Otherwise the function return 1.
195  */
196 int
197 win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex);
198
199 /*
200  * Signal that a condition is verified.
201  * @param cond The address of the condition concerned by the signal.
202  * @return If successful the function return 0.
203  *  Otherwise the function return 1.
204  */
205 int
206 win_thread_cond_signal(win_thread_cond_t* cond);
207
208 /*
209  * Unblock all threads waiting for a condition.
210  * @param cond The address of the condition to broadcast.
211  * @return If successful the function return 0.
212  * Otherwise the function return 1.
213  */
214
215 int 
216 win_thread_cond_broadcast(win_thread_cond_t* cond);
217
218 /*
219  * Destroy a condition.
220  * @param The address to the condition to destroy.
221  * @return If successful the function returns 0.
222  *  Otherwise the function return 1.
223  */
224 int
225 win_thread_cond_destroy(win_thread_cond_t* cond);
226
227 /*
228  * Forces the current thread to relinquish use of its processor.
229  */
230 void
231 win_thread_yield(void);
232
233
234 /*
235  * windows thread key connected functions
236  */
237
238 /*
239  * Create a windows thread key.
240  * @param key The pointer to the key to create.
241  * @param dstr_func The pointer to the cleanup function.
242  * @return If successful the function returns .Otherwise
243  * the function returns 1.
244  */
245 int 
246 win_thread_key_create(win_thread_key_t* key, pfn_key_dstr_func_t dstr_func);
247 /*
248  * Destroy a key.
249  * @param key The key to delete.
250  * @return If successful the function returns 0.
251  * Otherwise the function returns 1.
252  */
253 int 
254 win_thread_key_delete(win_thread_key_t key);
255
256 /*
257  * Update or set the value associated to a key.
258  * @param key The key concerned by the operation.
259  * @param value The value to set or update.
260  * @return If successful the function returns 0.
261  * Otherwise the function returns 1.
262  */
263 int 
264 win_thread_setspecific(win_thread_key_t key, const void* p);
265
266 /*
267  * Return the value associated to a key.
268  * @param key The key concerned by the operation.
269  * @return If successful the function returns the value.
270  * Otherwise the function returns NULL.
271  */
272 void* 
273 win_thread_getspecific(win_thread_key_t key);
274
275
276
277
278
279 #ifdef __cplusplus
280 extern }
281 #endif
282
283
284 #endif /* !XBT_WIN_THREAD_H */