3 /* xbt_synchro -- Synchronization virtualized depending on whether we are */
4 /* in simulation or real life (act on simulated processes) */
6 /* This is the real life implementation, using xbt_os_thread to be portable */
7 /* to windows and linux. */
9 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
10 * All right reserved. */
12 /* This program is free software; you can redistribute it and/or modify it
13 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "xbt/sysdep.h"
19 #include "xbt/synchro.h" /* This module */
20 #include "xbt/xbt_os_thread.h" /* The implementation we use */
22 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
26 /* KEEP IT IN SYNC WITH xbt_os_thread (both win and lin parts) */
30 pvoid_f_pvoid_t *start_routine;
32 HANDLE handle; /* the win thread handle */
33 unsigned long id; /* the win thread id */
34 pvoid_f_pvoid_t *start_routine;
39 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
41 return (xbt_thread_t)xbt_os_thread_create(start_routine,param);
45 xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
46 xbt_os_thread_join( (xbt_os_thread_t)thread, thread_return );
49 void xbt_thread_exit(int *retval) {
50 xbt_os_thread_exit( retval );
53 xbt_thread_t xbt_thread_self(void) {
54 return (xbt_thread_t)xbt_os_thread_self();
57 void xbt_thread_yield(void) {
58 xbt_os_thread_yield();
60 /****** mutex related functions ******/
62 /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
66 CRITICAL_SECTION lock;
70 xbt_mutex_t xbt_mutex_init(void) {
71 return (xbt_mutex_t)xbt_os_mutex_init();
74 void xbt_mutex_lock(xbt_mutex_t mutex) {
75 xbt_os_mutex_lock( (xbt_os_mutex_t)mutex );
78 void xbt_mutex_unlock(xbt_mutex_t mutex) {
79 xbt_os_mutex_unlock( (xbt_os_mutex_t)mutex );
82 void xbt_mutex_destroy(xbt_mutex_t mutex) {
83 xbt_os_mutex_destroy( (xbt_os_mutex_t)mutex );
87 enum { /* KEEP IT IN SYNC WITH OS IMPLEM */
94 /***** condition related functions *****/
95 typedef struct xbt_cond_ {
96 /* KEEP IT IN SYNC WITH OS IMPLEMENTATION (both win and lin) */
100 HANDLE events[MAX_EVENTS];
102 unsigned int waiters_count; /* the number of waiters */
103 CRITICAL_SECTION waiters_count_lock; /* protect access to waiters_count */
107 xbt_cond_t xbt_cond_init(void) {
108 return (xbt_cond_t) xbt_os_cond_init();
111 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
112 xbt_os_cond_wait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex );
115 void xbt_cond_signal(xbt_cond_t cond) {
116 xbt_os_cond_signal( (xbt_os_cond_t)cond );
119 void xbt_cond_broadcast(xbt_cond_t cond){
120 xbt_os_cond_broadcast( (xbt_os_cond_t)cond );
122 void xbt_cond_destroy(xbt_cond_t cond){
123 xbt_os_cond_destroy( (xbt_os_cond_t)cond );