1 /* xbt_synchro -- Synchronization virtualized depending on whether we are */
2 /* in simulation or real life (act on simulated processes) */
4 /* This is the simulation implementation, using simix. */
6 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
7 * All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "xbt/synchro.h" /* This module */
16 #include "simix/simix.h" /* used implementation */
17 #include "simix/datatypes.h"
18 #include "../simix/smx_private.h" /* FIXME */
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt,
21 "Synchronization mechanism");
23 /* the implementation would be cleaner (and faster) with ELF symbol aliasing */
25 typedef struct s_xbt_thread_ {
26 smx_process_t s_process; /* keep this first, gras_socket_im_the_server() does funky transtyping in sg_msg.c */
31 /* stuff to allow other people to wait on me with xbt_thread_join */
32 int joinable:1, done:1;
37 static int xbt_thread_create_wrapper(int argc, char *argv[])
39 smx_process_t self = SIMIX_process_self();
41 (xbt_thread_t) SIMIX_process_self_get_data(self);
42 simcall_process_set_data(self, t->father_data);
43 t->code(t->userparam);
46 xbt_mutex_acquire(t->mutex);
47 xbt_cond_broadcast(t->cond);
48 xbt_mutex_release(t->mutex);
50 xbt_mutex_destroy(t->mutex);
51 xbt_cond_destroy(t->cond);
58 xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code,
59 void *param, int joinable)
61 xbt_thread_t res = xbt_new0(s_xbt_thread_t, 1);
62 res->name = xbt_strdup(name);
63 res->userparam = param;
65 res->father_data = SIMIX_process_self_get_data(SIMIX_process_self());
66 /* char*name = bprintf("%s#%p",SIMIX_process_self_get_name(), param); */
67 simcall_process_create(&res->s_process, name,
68 xbt_thread_create_wrapper, res,
69 SIMIX_host_self_get_name(), 0, NULL,
71 res->joinable = joinable;
73 res->cond = xbt_cond_init();
74 res->mutex = xbt_mutex_init();
79 const char *xbt_thread_name(xbt_thread_t t)
84 const char *xbt_thread_self_name(void)
86 xbt_thread_t me = xbt_thread_self();
87 return me ? me->name : "maestro";
91 void xbt_thread_join(xbt_thread_t thread)
93 xbt_mutex_acquire(thread->mutex);
94 xbt_assert(thread->joinable,
95 "Cannot join on %p: wasn't created joinable", thread);
97 xbt_cond_wait(thread->cond, thread->mutex);
98 xbt_mutex_release(thread->mutex);
101 xbt_mutex_destroy(thread->mutex);
102 xbt_cond_destroy(thread->cond);
108 void xbt_thread_cancel(xbt_thread_t thread)
110 simcall_process_kill(thread->s_process);
115 void xbt_thread_exit()
117 simcall_process_kill(SIMIX_process_self());
120 xbt_thread_t xbt_thread_self(void)
122 return SIMIX_process_self_get_data(SIMIX_process_self());
125 void xbt_thread_yield(void)
127 SIMIX_process_yield(SIMIX_process_self());
130 /****** mutex related functions ******/
131 struct s_xbt_mutex_ {
135 xbt_mutex_t xbt_mutex_init(void)
137 return (xbt_mutex_t) simcall_mutex_init();
140 void xbt_mutex_acquire(xbt_mutex_t mutex)
142 simcall_mutex_lock((smx_mutex_t) mutex);
145 void xbt_mutex_release(xbt_mutex_t mutex)
147 simcall_mutex_unlock((smx_mutex_t) mutex);
150 void xbt_mutex_destroy(xbt_mutex_t mutex)
152 simcall_mutex_destroy((smx_mutex_t) mutex);
155 /***** condition related functions *****/
160 xbt_cond_t xbt_cond_init(void)
162 return (xbt_cond_t) simcall_cond_init();
165 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
167 simcall_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex);
170 void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
172 simcall_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay);
175 void xbt_cond_signal(xbt_cond_t cond)
177 simcall_cond_signal((smx_cond_t) cond);
180 void xbt_cond_broadcast(xbt_cond_t cond)
182 simcall_cond_broadcast((smx_cond_t) cond);
185 void xbt_cond_destroy(xbt_cond_t cond)
187 simcall_cond_destroy((smx_cond_t) cond);