X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/dff9e15c44ab6340d27215957c56fa72fad246a2..aa29c3a2434a29e6901f1e98aa0c171ee15d73cc:/src/xbt/xbt_sg_synchro.c diff --git a/src/xbt/xbt_sg_synchro.c b/src/xbt/xbt_sg_synchro.c index c0d02a98e1..0dcca4cbe2 100644 --- a/src/xbt/xbt_sg_synchro.c +++ b/src/xbt/xbt_sg_synchro.c @@ -1,12 +1,10 @@ -/* $Id$ */ - /* xbt_synchro -- Synchronization virtualized depending on whether we are */ /* in simulation or real life (act on simulated processes) */ /* This is the simulation implementation, using simix. */ -/* Copyright 2006,2007 Malek Cherier, Martin Quinson - * All right reserved. */ +/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team. + * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -18,6 +16,9 @@ #include "simix/simix.h" /* used implementation */ #include "simix/datatypes.h" +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt, + "Synchronization mechanism"); + /* the implementation would be cleaner (and faster) with ELF symbol aliasing */ typedef struct s_xbt_thread_ { @@ -26,6 +27,10 @@ typedef struct s_xbt_thread_ { void_f_pvoid_t code; void *userparam; void *father_data; + /* stuff to allow other people to wait on me with xbt_thread_join */ + int joinable:1,done:1; + xbt_cond_t cond; + xbt_mutex_t mutex; } s_xbt_thread_t; static int xbt_thread_create_wrapper(int argc, char *argv[]) @@ -34,13 +39,22 @@ static int xbt_thread_create_wrapper(int argc, char *argv[]) (xbt_thread_t) SIMIX_process_get_data(SIMIX_process_self()); SIMIX_process_set_data(SIMIX_process_self(), t->father_data); (*t->code) (t->userparam); - free(t->name); - free(t); + if (t->joinable) { + t->done=1; + xbt_mutex_acquire(t->mutex); + xbt_cond_broadcast(t->cond); + xbt_mutex_release(t->mutex); + } else { + xbt_mutex_destroy(t->mutex); + xbt_cond_destroy(t->cond); + free(t->name); + free(t); + } return 0; } xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code, - void *param) + void *param,int joinable) { xbt_thread_t res = xbt_new0(s_xbt_thread_t, 1); res->name = xbt_strdup(name); @@ -53,6 +67,10 @@ xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code, SIMIX_host_get_name(SIMIX_host_self ()), 0, NULL, /*props */ NULL); + res->joinable = joinable; + res->done = 0; + res->cond = xbt_cond_init(); + res->mutex = xbt_mutex_init(); // free(name); return res; } @@ -71,7 +89,18 @@ const char *xbt_thread_self_name(void) void xbt_thread_join(xbt_thread_t thread) { - THROW_UNIMPLEMENTED; /* FIXME */ + xbt_mutex_acquire(thread->mutex); + xbt_assert1(thread->joinable,"Cannot join on %p: wasn't created joinable",thread); + if (!thread->done) { + xbt_cond_wait(thread->cond,thread->mutex); + xbt_mutex_release(thread->mutex); + } + + xbt_mutex_destroy(thread->mutex); + xbt_cond_destroy(thread->cond); + free(thread->name); + free(thread); + } void xbt_thread_cancel(xbt_thread_t thread) @@ -83,9 +112,7 @@ void xbt_thread_cancel(xbt_thread_t thread) void xbt_thread_exit() { - xbt_thread_t me = SIMIX_process_get_data(SIMIX_process_self()); - SIMIX_process_kill(me->s_process); - free(me); + SIMIX_process_kill(SIMIX_process_self()); } xbt_thread_t xbt_thread_self(void) @@ -94,9 +121,8 @@ xbt_thread_t xbt_thread_self(void) return p ? SIMIX_process_get_data(p) : NULL; } -void xbt_thread_yield(void) -{ - THROW_UNIMPLEMENTED; /* FIXME */ +void xbt_thread_yield(void) { + SIMIX_process_yield(); } /****** mutex related functions ******/