X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/0ca1291c2f63ca6235f4b4885c48413a900aade4..0917b3d40163b247c05fe33f60eb71c00a7c6854:/src/xbt/xbt_sg_synchro.c diff --git a/src/xbt/xbt_sg_synchro.c b/src/xbt/xbt_sg_synchro.c index 34d22444b0..3221ba8036 100644 --- a/src/xbt/xbt_sg_synchro.c +++ b/src/xbt/xbt_sg_synchro.c @@ -3,19 +3,17 @@ /* This is the simulation implementation, using simix. */ -/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2007-2014. 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. */ #include "xbt/ex.h" +#include "xbt/synchro_core.h" -#include "xbt/synchro.h" /* This module */ - -#include "simix/simix.h" /* used implementation */ -#include "simix/datatypes.h" -#include "../simix/private.h" /* FIXME */ +#include "simgrid/simix.h" /* used implementation */ +#include "../simix/smx_private.h" /* FIXME */ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt, "Synchronization mechanism"); @@ -29,17 +27,18 @@ typedef struct s_xbt_thread_ { void *userparam; void *father_data; /* stuff to allow other people to wait on me with xbt_thread_join */ - int joinable:1, done:1; + unsigned 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[]) { + smx_process_t self = SIMIX_process_self(); xbt_thread_t t = - (xbt_thread_t) SIMIX_process_self_get_data(); - SIMIX_req_process_set_data(SIMIX_process_self(), t->father_data); - (*t->code) (t->userparam); + (xbt_thread_t) SIMIX_process_self_get_data(self); + simcall_process_set_data(self, t->father_data); + t->code(t->userparam); if (t->joinable) { t->done = 1; xbt_mutex_acquire(t->mutex); @@ -61,12 +60,12 @@ xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code, res->name = xbt_strdup(name); res->userparam = param; res->code = code; - res->father_data = SIMIX_process_self_get_data(); + res->father_data = SIMIX_process_self_get_data(SIMIX_process_self()); /* char*name = bprintf("%s#%p",SIMIX_process_self_get_name(), param); */ - SIMIX_req_process_create(&res->s_process, name, + simcall_process_create(&res->s_process, name, xbt_thread_create_wrapper, res, - SIMIX_host_self_get_name(), 0, NULL, - /*props */ NULL); + SIMIX_host_self_get_name(), -1.0, 0, NULL, + /*props */ NULL,0); res->joinable = joinable; res->done = 0; res->cond = xbt_cond_init(); @@ -106,24 +105,24 @@ void xbt_thread_join(xbt_thread_t thread) void xbt_thread_cancel(xbt_thread_t thread) { - SIMIX_req_process_kill(thread->s_process); + simcall_process_kill(thread->s_process); free(thread->name); free(thread); } void xbt_thread_exit() { - SIMIX_req_process_kill(SIMIX_process_self()); + simcall_process_kill(SIMIX_process_self()); } xbt_thread_t xbt_thread_self(void) { - return SIMIX_process_self_get_data(); + return SIMIX_process_self_get_data(SIMIX_process_self()); } void xbt_thread_yield(void) { - SIMIX_process_yield(); + SIMIX_process_yield(SIMIX_process_self()); } /****** mutex related functions ******/ @@ -133,22 +132,27 @@ struct s_xbt_mutex_ { xbt_mutex_t xbt_mutex_init(void) { - return (xbt_mutex_t) SIMIX_req_mutex_init(); + return (xbt_mutex_t) simcall_mutex_init(); } void xbt_mutex_acquire(xbt_mutex_t mutex) { - SIMIX_req_mutex_lock((smx_mutex_t) mutex); + simcall_mutex_lock((smx_mutex_t) mutex); +} + +int xbt_mutex_try_acquire(xbt_mutex_t mutex) +{ + return simcall_mutex_trylock((smx_mutex_t) mutex); } void xbt_mutex_release(xbt_mutex_t mutex) { - SIMIX_req_mutex_unlock((smx_mutex_t) mutex); + simcall_mutex_unlock((smx_mutex_t) mutex); } void xbt_mutex_destroy(xbt_mutex_t mutex) { - SIMIX_req_mutex_destroy((smx_mutex_t) mutex); + simcall_mutex_destroy((smx_mutex_t) mutex); } /***** condition related functions *****/ @@ -158,30 +162,74 @@ struct s_xbt_cond_ { xbt_cond_t xbt_cond_init(void) { - return (xbt_cond_t) SIMIX_req_cond_init(); + return (xbt_cond_t) simcall_cond_init(); } void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) { - SIMIX_req_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex); + simcall_cond_wait((smx_cond_t) cond, (smx_mutex_t) mutex); } void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) { - SIMIX_req_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay); + simcall_cond_wait_timeout((smx_cond_t) cond, (smx_mutex_t) mutex, delay); } void xbt_cond_signal(xbt_cond_t cond) { - SIMIX_req_cond_signal((smx_cond_t) cond); + simcall_cond_signal((smx_cond_t) cond); } void xbt_cond_broadcast(xbt_cond_t cond) { - SIMIX_req_cond_broadcast((smx_cond_t) cond); + simcall_cond_broadcast((smx_cond_t) cond); } void xbt_cond_destroy(xbt_cond_t cond) { - SIMIX_req_cond_destroy((smx_cond_t) cond); + simcall_cond_destroy((smx_cond_t) cond); +} + +/***** barrier related functions *****/ +typedef struct s_xbt_bar_ { + xbt_mutex_t mutex; + xbt_cond_t cond; + unsigned int arrived_processes; + unsigned int expected_processes; +} s_xbt_bar_; + +xbt_bar_t xbt_barrier_init(unsigned int count) +{ + xbt_bar_t bar = xbt_new0(s_xbt_bar_, 1); + bar->expected_processes = count; + bar->arrived_processes = 0; + bar->mutex = xbt_mutex_init(); + bar->cond = xbt_cond_init(); + return bar; } + + +int xbt_barrier_wait(xbt_bar_t bar) +{ + int ret=0; + xbt_mutex_acquire(bar->mutex); + if (++bar->arrived_processes == bar->expected_processes) { + xbt_cond_broadcast(bar->cond); + xbt_mutex_release(bar->mutex); + ret=XBT_BARRIER_SERIAL_PROCESS; + bar->arrived_processes = 0; + } else { + xbt_cond_wait(bar->cond, bar->mutex); + xbt_mutex_release(bar->mutex); + } + + return ret; +} + +void xbt_barrier_destroy(xbt_bar_t bar) +{ + xbt_mutex_destroy(bar->mutex); + xbt_cond_destroy(bar->cond); + xbt_free(bar); +} +