X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/cb91af44a1bd7e5f331686df25d5fe80ff571a43..b0943ccb32bf2fbe0bb7f2d4fb41798cc8d0af6b:/src/xbt/xbt_sg_synchro.c diff --git a/src/xbt/xbt_sg_synchro.c b/src/xbt/xbt_sg_synchro.c index d485ba37ed..927d139840 100644 --- a/src/xbt/xbt_sg_synchro.c +++ b/src/xbt/xbt_sg_synchro.c @@ -3,15 +3,14 @@ /* This is the simulation implementation, using simix. */ -/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2007-2015. 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.h" /* This module */ +#include "xbt/synchro_core.h" #include "simgrid/simix.h" /* used implementation */ #include "../simix/smx_private.h" /* FIXME */ @@ -28,7 +27,7 @@ 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; @@ -63,10 +62,10 @@ xbt_thread_t xbt_thread_create(const char *name, void_f_pvoid_t code, res->code = code; res->father_data = SIMIX_process_self_get_data(SIMIX_process_self()); /* char*name = bprintf("%s#%p",SIMIX_process_self_get_name(), param); */ - simcall_process_create(&res->s_process, name, + res->s_process = simcall_process_create(name, xbt_thread_create_wrapper, res, SIMIX_host_self_get_name(), -1.0, 0, NULL, - /*props */ NULL); + /*props */ NULL,0); res->joinable = joinable; res->done = 0; res->cond = xbt_cond_init(); @@ -141,6 +140,11 @@ void xbt_mutex_acquire(xbt_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) { simcall_mutex_unlock((smx_mutex_t) mutex); @@ -185,3 +189,47 @@ void xbt_cond_destroy(xbt_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); +} +