From: mquinson Date: Tue, 10 Jul 2007 18:04:52 +0000 (+0000) Subject: add the ability to specify a timeout on waiting a condition in every interfaces ... X-Git-Tag: v3.3~1646 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/cd5e6b169f6a55b9e913fe0f8ea3ec85fbb330bd add the ability to specify a timeout on waiting a condition in every interfaces (but windows, I personnaly dunno how to do it), and use it in the queues git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3714 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/Makefile.am b/src/Makefile.am index c740bba28d..cc48d3eea9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -128,7 +128,7 @@ XBT_SRC=\ xbt/set.c \ xbt/xbt_matrix.c \ \ - xbt/queue.c \ + xbt/xbt_queue.c \ \ xbt/xbt_peer.c \ \ diff --git a/src/Makefile.in b/src/Makefile.in index 94118b60db..8a445cb864 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -80,7 +80,7 @@ am__objects_1 = snprintf.lo xbt_str.lo ex.lo xbt_virtu.lo \ xbt_log_layout_simple.lo xbt_log_layout_format.lo \ mallocator.lo dynar.lo dict.lo dict_elm.lo dict_cursor.lo \ dict_multi.lo heap.lo fifo.lo swag.lo graph.lo set.lo \ - xbt_matrix.lo queue.lo xbt_peer.lo xbt_main.lo config.lo \ + xbt_matrix.lo xbt_queue.lo xbt_peer.lo xbt_main.lo config.lo \ cunit.lo graphxml_parse.lo am__objects_2 = gras.lo transport.lo gras_msg_mod.lo gras_msg_types.lo \ gras_msg_exchange.lo rpc.lo timer.lo process.lo gras_module.lo \ @@ -106,7 +106,7 @@ am__libsimgrid_la_SOURCES_DIST = xbt/snprintf.c xbt/xbt_str.c xbt/ex.c \ xbt/xbt_log_layout_format.c xbt/mallocator.c xbt/dynar.c \ xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c xbt/dict_multi.c \ xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c xbt/set.c \ - xbt/xbt_matrix.c xbt/queue.c xbt/xbt_peer.c xbt/xbt_main.c \ + xbt/xbt_matrix.c xbt/xbt_queue.c xbt/xbt_peer.c xbt/xbt_main.c \ xbt/config.c xbt/cunit.c xbt/graphxml_parse.c surf/maxmin.c \ surf/lagrange.c surf/trace_mgr.c surf/surf.c \ surf/surfxml_parse.c surf/cpu.c surf/network.c \ @@ -170,7 +170,7 @@ am__libsimgrid4java_la_SOURCES_DIST = xbt/snprintf.c xbt/xbt_str.c \ xbt/xbt_log_layout_format.c xbt/mallocator.c xbt/dynar.c \ xbt/dict.c xbt/dict_elm.c xbt/dict_cursor.c xbt/dict_multi.c \ xbt/heap.c xbt/fifo.c xbt/swag.c xbt/graph.c xbt/set.c \ - xbt/xbt_matrix.c xbt/queue.c xbt/xbt_peer.c xbt/xbt_main.c \ + xbt/xbt_matrix.c xbt/xbt_queue.c xbt/xbt_peer.c xbt/xbt_main.c \ xbt/config.c xbt/cunit.c xbt/graphxml_parse.c surf/maxmin.c \ surf/lagrange.c surf/trace_mgr.c surf/surf.c \ surf/surfxml_parse.c surf/cpu.c surf/network.c \ @@ -494,7 +494,7 @@ XBT_SRC = \ xbt/set.c \ xbt/xbt_matrix.c \ \ - xbt/queue.c \ + xbt/xbt_queue.c \ \ xbt/xbt_peer.c \ \ @@ -813,7 +813,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/network_gtnets.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/peermanagement.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/process.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/queue.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rl_dns.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rl_emul.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rl_msg.Plo@am__quote@ @@ -868,6 +867,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_os_thread.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_os_time.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_peer.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_queue.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_rl_synchro.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_sg_synchro.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xbt_str.Plo@am__quote@ @@ -1049,12 +1049,12 @@ xbt_matrix.lo: xbt/xbt_matrix.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o xbt_matrix.lo `test -f 'xbt/xbt_matrix.c' || echo '$(srcdir)/'`xbt/xbt_matrix.c -queue.lo: xbt/queue.c -@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT queue.lo -MD -MP -MF $(DEPDIR)/queue.Tpo -c -o queue.lo `test -f 'xbt/queue.c' || echo '$(srcdir)/'`xbt/queue.c -@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/queue.Tpo $(DEPDIR)/queue.Plo -@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='xbt/queue.c' object='queue.lo' libtool=yes @AMDEPBACKSLASH@ +xbt_queue.lo: xbt/xbt_queue.c +@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT xbt_queue.lo -MD -MP -MF $(DEPDIR)/xbt_queue.Tpo -c -o xbt_queue.lo `test -f 'xbt/xbt_queue.c' || echo '$(srcdir)/'`xbt/xbt_queue.c +@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/xbt_queue.Tpo $(DEPDIR)/xbt_queue.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='xbt/xbt_queue.c' object='xbt_queue.lo' libtool=yes @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o queue.lo `test -f 'xbt/queue.c' || echo '$(srcdir)/'`xbt/queue.c +@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o xbt_queue.lo `test -f 'xbt/xbt_queue.c' || echo '$(srcdir)/'`xbt/xbt_queue.c xbt_peer.lo: xbt/xbt_peer.c @am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT xbt_peer.lo -MD -MP -MF $(DEPDIR)/xbt_peer.Tpo -c -o xbt_peer.lo `test -f 'xbt/xbt_peer.c' || echo '$(srcdir)/'`xbt/xbt_peer.c diff --git a/src/include/xbt/xbt_os_thread.h b/src/include/xbt/xbt_os_thread.h index 0796566c05..a2a1de75d2 100644 --- a/src/include/xbt/xbt_os_thread.h +++ b/src/include/xbt/xbt_os_thread.h @@ -51,6 +51,9 @@ SG_BEGIN_DECL() XBT_PUBLIC(xbt_os_cond_t) xbt_os_cond_init(void); XBT_PUBLIC(void) xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex); + XBT_PUBLIC(void) xbt_os_cond_timedwait(xbt_os_cond_t cond, + xbt_os_mutex_t mutex, + double delay); XBT_PUBLIC(void) xbt_os_cond_signal(xbt_os_cond_t cond); XBT_PUBLIC(void) xbt_os_cond_broadcast(xbt_os_cond_t cond); XBT_PUBLIC(void) xbt_os_cond_destroy(xbt_os_cond_t cond); diff --git a/src/xbt/xbt_os_thread.c b/src/xbt/xbt_os_thread.c index 73fe3a4fdc..81c86e9383 100644 --- a/src/xbt/xbt_os_thread.c +++ b/src/xbt/xbt_os_thread.c @@ -13,6 +13,7 @@ #include "xbt/sysdep.h" #include "xbt/ex.h" #include "portable.h" +#include "xbt/xbt_os_time.h" /* Portable time facilities */ #include "xbt/xbt_os_thread.h" /* This module */ #include "xbt_modinter.h" /* Initialization/finalization of this module */ @@ -165,6 +166,24 @@ void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) { cond,mutex, strerror(errcode)); } +#include +#include +void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) { + int errcode; + struct timespec ts_end; + double end = delay + xbt_os_time(); + ts_end.tv_sec = (time_t) floor(end); + ts_end.tv_nsec = (long) ( ( end - ts_end.tv_sec) * 1000000000); + switch ( (errcode=pthread_cond_timedwait(&(cond->c),&(mutex->m), &ts_end)) ) { + case ETIMEDOUT: + THROW3(timeout_error,errcode,"condition %p (mutex %p) wasn't signaled before timeout (%f)", + cond,mutex, delay); + default: + THROW4(system_error,errcode,"pthread_cond_timedwait(%p,%p,%f) failed: %s", + cond,mutex, delay, strerror(errcode)); + } +} + void xbt_os_cond_signal(xbt_os_cond_t cond) { int errcode; if ((errcode=pthread_cond_signal(&(cond->c)))) @@ -396,6 +415,9 @@ void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex) { /* relock the mutex associated with the condition in accordance with the posix thread specification */ EnterCriticalSection (& mutex->lock); } +void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay) { + THROW_UNIMPLEMENTED; +} void xbt_os_cond_signal(xbt_os_cond_t cond) { int have_waiters; diff --git a/src/xbt/xbt_queue.c b/src/xbt/xbt_queue.c index 30c01e257a..4e645af1a8 100644 --- a/src/xbt/xbt_queue.c +++ b/src/xbt/xbt_queue.c @@ -132,3 +132,66 @@ void xbt_queue_shift(xbt_queue_t queue, void* const dst) { xbt_dynar_shift(queue->data,dst); xbt_cond_signal(queue->not_full); } + + + + +/** @brief Push something to the message exchange queue, with a timeout. + * + * @seealso #xbt_queue_push + */ +void xbt_queue_push_timed(xbt_queue_t queue, const void *src,double delay) { + xbt_mutex_lock(queue->mutex); + while (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data)) { + DEBUG2("Capacity of %p exceded (=%d). Waiting",queue,queue->capacity); + xbt_cond_timedwait(queue->not_full,queue->mutex, delay); + } + xbt_dynar_push(queue->data,src); + xbt_cond_signal(queue->not_empty); +} + + +/** @brief Pop something from the message exchange queue, with a timeout. + * + * @seealso #xbt_queue_pop + * + */ +void xbt_queue_pop_timed(xbt_queue_t queue, void* const dst,double delay) { + xbt_mutex_lock(queue->mutex); + while (xbt_dynar_length(queue->data) == 0) { + DEBUG1("Queue %p empty. Waiting",queue); + xbt_cond_timedwait(queue->not_empty,queue->mutex,delay); + } + xbt_dynar_pop(queue->data,dst); + xbt_cond_signal(queue->not_full); +} + +/** @brief Unshift something to the message exchange queue, with a timeout. + * + * @seealso #xbt_queue_unshift + */ +void xbt_queue_unshift_timed(xbt_queue_t queue, const void *src,double delay) { + xbt_mutex_lock(queue->mutex); + while (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data)) { + DEBUG2("Capacity of %p exceded (=%d). Waiting",queue,queue->capacity); + xbt_cond_timedwait(queue->not_full,queue->mutex,delay); + } + xbt_dynar_unshift(queue->data,src); + xbt_cond_signal(queue->not_empty); +} + + +/** @brief Shift something from the message exchange queue, with a timeout. + * + * @seealso #xbt_queue_shift + * + */ +void xbt_queue_shift_timed(xbt_queue_t queue, void* const dst,double delay) { + xbt_mutex_lock(queue->mutex); + while (xbt_dynar_length(queue->data) == 0) { + DEBUG1("Queue %p empty. Waiting",queue); + xbt_cond_timedwait(queue->not_empty,queue->mutex,delay); + } + xbt_dynar_shift(queue->data,dst); + xbt_cond_signal(queue->not_full); +} diff --git a/src/xbt/xbt_rl_synchro.c b/src/xbt/xbt_rl_synchro.c index 1f42eeaf25..32a2b6998c 100644 --- a/src/xbt/xbt_rl_synchro.c +++ b/src/xbt/xbt_rl_synchro.c @@ -112,6 +112,10 @@ void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) { xbt_os_cond_wait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex ); } +void xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay) { + xbt_os_cond_timedwait( (xbt_os_cond_t)cond, (xbt_os_mutex_t)mutex, delay ); +} + void xbt_cond_signal(xbt_cond_t cond) { xbt_os_cond_signal( (xbt_os_cond_t)cond ); } diff --git a/src/xbt/xbt_sg_synchro.c b/src/xbt/xbt_sg_synchro.c index 080ab79e3d..db4fe50e99 100644 --- a/src/xbt/xbt_sg_synchro.c +++ b/src/xbt/xbt_sg_synchro.c @@ -96,6 +96,10 @@ void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex) { SIMIX_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_cond_wait_timeout( (smx_cond_t)cond , (smx_mutex_t)mutex, delay ); +} + void xbt_cond_signal(xbt_cond_t cond) { SIMIX_cond_signal( (smx_cond_t)cond ); }