3 /* xbt_synchro -- Synchronization virtualized depending on whether we are */
4 /* in simulation or real life (act on simulated processes) */
6 /* This is the simulation implementation, using simix. */
8 /* Copyright 2006,2007 Malek Cherier, Martin Quinson
9 * All right reserved. */
11 /* This program is free software; you can redistribute it and/or modify it
12 * under the terms of the license (GNU LGPL) which comes with this package. */
16 #include "xbt/synchro.h" /* This module */
18 #include "simix/simix.h" /* used implementation */
19 #include "simix/datatypes.h"
21 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
23 typedef struct s_xbt_thread_ {
25 smx_process_t s_process;
31 static int xbt_thread_create_wrapper(int argc, char *argv[]) {
32 xbt_thread_t t = (xbt_thread_t)SIMIX_process_get_data(SIMIX_process_self());
33 SIMIX_process_set_data(SIMIX_process_self(),t->father_data);
34 (*t->code)(t->userparam);
38 xbt_thread_t xbt_thread_create(const char*name,void_f_pvoid_t code, void* param) {
39 xbt_thread_t res = xbt_new0(s_xbt_thread_t,1);
40 res->name = xbt_strdup(name);
41 res->userparam = param;
43 res->father_data = SIMIX_process_get_data(SIMIX_process_self());
44 // char*name = bprintf("%s#%p",SIMIX_process_get_name(SIMIX_process_self()), param);
45 res->s_process = SIMIX_process_create(name,
46 xbt_thread_create_wrapper, res,
47 SIMIX_host_get_name(SIMIX_host_self()),
48 0,NULL,/*props*/NULL);
53 const char* xbt_thread_name(xbt_thread_t t) {
57 const char* xbt_thread_self_name(void) {
58 xbt_thread_t me = xbt_thread_self();
59 return me ? me->name : "maestro";
64 xbt_thread_join(xbt_thread_t thread) {
65 THROW_UNIMPLEMENTED; /* FIXME */
69 xbt_thread_cancel(xbt_thread_t thread) {
70 SIMIX_process_kill(thread->s_process);
74 void xbt_thread_exit() {
75 xbt_thread_t me=SIMIX_process_get_data(SIMIX_process_self());
76 SIMIX_process_kill(me->s_process);
80 xbt_thread_t xbt_thread_self(void) {
81 smx_process_t p = SIMIX_process_self();
82 return p ? SIMIX_process_get_data(p) : NULL;
85 void xbt_thread_yield(void) {
86 THROW_UNIMPLEMENTED; /* FIXME */
89 /****** mutex related functions ******/
92 /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
93 xbt_swag_t sleeping; /* list of sleeping process */
95 /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_mutex */
99 xbt_mutex_t xbt_mutex_init(void) {
100 return (xbt_mutex_t)SIMIX_mutex_init();
103 void xbt_mutex_acquire(xbt_mutex_t mutex) {
104 SIMIX_mutex_lock( (smx_mutex_t)mutex) ;
107 void xbt_mutex_release(xbt_mutex_t mutex) {
108 SIMIX_mutex_unlock( (smx_mutex_t)mutex );
111 void xbt_mutex_destroy(xbt_mutex_t mutex) {
112 SIMIX_mutex_destroy( (smx_mutex_t)mutex );
115 /***** condition related functions *****/
118 /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
119 xbt_swag_t sleeping; /* list of sleeping process */
121 xbt_fifo_t actions; /* list of actions */
122 /* KEEP IT IN SYNC WITH src/simix/private.h::struct s_smx_cond */
126 xbt_cond_t xbt_cond_init(void) {
127 return (xbt_cond_t)SIMIX_cond_init();
130 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) {
131 SIMIX_cond_wait( (smx_cond_t)cond , (smx_mutex_t)mutex );
134 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) {
135 SIMIX_cond_wait_timeout( (smx_cond_t)cond , (smx_mutex_t)mutex, delay );
138 void xbt_cond_signal(xbt_cond_t cond) {
139 SIMIX_cond_signal( (smx_cond_t)cond );
142 void xbt_cond_broadcast(xbt_cond_t cond){
143 SIMIX_cond_broadcast( (smx_cond_t)cond );
145 void xbt_cond_destroy(xbt_cond_t cond){
146 SIMIX_cond_destroy( (smx_cond_t)cond );