2 * Copyright (c) 2007 Malek CHERIER. All rights reserved.
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.
9 #ifndef XBT_WIN_THREAD_H
10 #define XBT_WIN_THREAD_H
13 #include "xbt/sysdep.h"
19 /* type of function pointeur used */
20 typedef DWORD WINAPI (*pfn_start_routine_t)(void*);
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;
26 * This structure represents a windows thread.
28 typedef struct s_win_thread
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;
35 struct s_win_thread_keys
37 win_thread_key_t* keys;
38 win_thread_key_t front;
39 win_thread_key_t back;
42 typedef struct s_win_thread_entry
48 }s_win_thread_entry_t,win_thread_entry_t;
50 typedef struct s_win_threads
52 win_thread_entry_t* threads;
53 win_thread_entry_t front;
54 win_thread_entry_t back;
56 CRITICAL_SECTION lock;
57 }s_win_threads_t,* win_threads_t;
61 * This structure simulates a pthread cond.
71 typedef struct s_win_thread_cond
73 HANDLE events[MAX_EVENTS];
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;
80 /* type of pointer function used by the
81 * win_thread_key_create() function.
83 typedef void (*pfn_key_dstr_func_t) (void*);
86 * This structure represents a windows key
88 struct s_win_thread_key
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;
98 * This structure represents a windows mutext
99 * remark : only encapsulate the mutext handle.
101 typedef struct s_win_thread_mutex
103 CRITICAL_SECTION lock;
104 }s_win_thread_mutex_t,* win_thread_mutex_t;
107 * Windows thread connected functions.
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
119 win_thread_create(win_thread_t* thread, pfn_start_routine_t start_routine,void* param);
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;
128 win_thread_exit(win_thread_t* thread,unsigned long exit_code);
131 * Return the identifier of the current thread.
134 win_thread_self(void);
137 * Windows mutex connected functions;
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.
147 win_thread_mutex_init(win_thread_mutex_t* mutex);
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.
156 win_thread_mutex_lock(win_thread_mutex_t* mutex);
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.
165 win_thread_mutex_unlock(win_thread_mutex_t* mutex);
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.
174 win_thread_mutex_destroy(win_thread_mutex_t* mutex);
177 * Condition connected functions.
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.
187 win_thread_cond_init(win_thread_cond_t* cond);
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.
197 win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex);
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.
206 win_thread_cond_signal(win_thread_cond_t* cond);
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.
216 win_thread_cond_broadcast(win_thread_cond_t* cond);
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.
225 win_thread_cond_destroy(win_thread_cond_t* cond);
228 * Forces the current thread to relinquish use of its processor.
231 win_thread_yield(void);
235 * windows thread key connected functions
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.
246 win_thread_key_create(win_thread_key_t* key, pfn_key_dstr_func_t dstr_func);
249 * @param key The key to delete.
250 * @return If successful the function returns 0.
251 * Otherwise the function returns 1.
254 win_thread_key_delete(win_thread_key_t key);
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.
264 win_thread_setspecific(win_thread_key_t key, const void* p);
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.
273 win_thread_getspecific(win_thread_key_t key);
284 #endif /* !XBT_WIN_THREAD_H */