Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the ability to specify a timeout on waiting a condition in every interfaces ...
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 10 Jul 2007 18:04:52 +0000 (18:04 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 10 Jul 2007 18:04:52 +0000 (18:04 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3714 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/Makefile.am
src/Makefile.in
src/include/xbt/xbt_os_thread.h
src/xbt/xbt_os_thread.c
src/xbt/xbt_queue.c
src/xbt/xbt_rl_synchro.c
src/xbt/xbt_sg_synchro.c

index c740bba..cc48d3e 100644 (file)
@@ -128,7 +128,7 @@ XBT_SRC=\
   xbt/set.c                                                                  \
   xbt/xbt_matrix.c                                                           \
   \
-  xbt/queue.c                                                                \
+  xbt/xbt_queue.c                                                            \
   \
   xbt/xbt_peer.c                                                             \
   \
index 94118b6..8a445cb 100644 (file)
@@ -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
index 0796566..a2a1de7 100644 (file)
@@ -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);
index 73fe3a4..81c86e9 100644 (file)
@@ -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 <time.h>
+#include <math.h>
+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;
index 30c01e2..4e645af 100644 (file)
@@ -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);
+}
index 1f42eea..32a2b69 100644 (file)
@@ -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 );
 }
index 080ab79..db4fe50 100644 (file)
@@ -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 );
 }