Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Augment test to cover sem_acquire_timeout.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 17 Mar 2021 09:15:40 +0000 (10:15 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 17 Mar 2021 10:18:41 +0000 (11:18 +0100)
Also cover sg_sem_would_block and sg_sem_get_capacity.

examples/c/synchro-semaphore/synchro-semaphore.c
examples/c/synchro-semaphore/synchro-semaphore.tesh

index f492e02..409a160 100644 (file)
@@ -23,8 +23,14 @@ static void peer(int argc, char* argv[])
     double wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
     i++;
     sg_actor_sleep_for(wait_time);
-    XBT_INFO("Trying to acquire %d", i);
-    sg_sem_acquire(sem);
+    XBT_INFO("Trying to acquire %d (%sblocking)", i, sg_sem_would_block(sem) ? "" : "not ");
+    // cover the two cases: with and without timeout
+    if (i > 1) {
+      while (sg_sem_acquire_timeout(sem, 3.0))
+        XBT_INFO("Timeout.. Try again %d", i);
+    } else {
+      sg_sem_acquire(sem);
+    }
     XBT_INFO("Acquired %d", i);
 
     wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
@@ -46,6 +52,7 @@ int main(int argc, char* argv[])
   sg_host_t h = sg_host_by_name("Fafard");
 
   sem                      = sg_sem_init(1);
+  XBT_INFO("Semaphore initialized with capacity = %d", sg_sem_get_capacity(sem));
   const char* aliceTimes[] = {"0", "1", "3", "5", "1", "2", "5", "0"};
   const char* bobTimes[]   = {"0.9", "1", "1", "2", "2", "0", "0", "5"};
 
index f6beba5..42b97b3 100644 (file)
@@ -4,35 +4,38 @@ p Testing synchronization with semaphores
 
 ! output sort 19
 $ ${bindir:=.}/c-synchro-semaphore ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
-> [  0.000000] (1:Alice@Fafard) Trying to acquire 1
+> [  0.000000] (0:maestro@) Semaphore initialized with capacity = 1
+> [  0.000000] (1:Alice@Fafard) Trying to acquire 1 (not blocking)
 > [  0.000000] (1:Alice@Fafard) Acquired 1
-> [  0.900000] (2:Bob@Fafard) Trying to acquire 1
+> [  0.900000] (2:Bob@Fafard) Trying to acquire 1 (blocking)
 > [  1.000000] (1:Alice@Fafard) Releasing 2
 > [  1.000000] (2:Bob@Fafard) Acquired 1
 > [  1.000000] (1:Alice@Fafard) Released 2
 > [  2.000000] (2:Bob@Fafard) Releasing 2
 > [  2.000000] (2:Bob@Fafard) Released 2
-> [  3.000000] (2:Bob@Fafard) Trying to acquire 3
+> [  3.000000] (2:Bob@Fafard) Trying to acquire 3 (not blocking)
 > [  3.000000] (2:Bob@Fafard) Acquired 3
-> [  4.000000] (1:Alice@Fafard) Trying to acquire 3
+> [  4.000000] (1:Alice@Fafard) Trying to acquire 3 (blocking)
 > [  5.000000] (2:Bob@Fafard) Releasing 4
 > [  5.000000] (1:Alice@Fafard) Acquired 3
 > [  5.000000] (2:Bob@Fafard) Released 4
-> [  7.000000] (2:Bob@Fafard) Trying to acquire 5
+> [  7.000000] (2:Bob@Fafard) Trying to acquire 5 (blocking)
 > [ 10.000000] (1:Alice@Fafard) Releasing 4
+> [ 10.000000] (2:Bob@Fafard) Timeout.. Try again 5
 > [ 10.000000] (2:Bob@Fafard) Acquired 5
 > [ 10.000000] (1:Alice@Fafard) Released 4
 > [ 10.000000] (2:Bob@Fafard) Releasing 6
 > [ 10.000000] (2:Bob@Fafard) Released 6
-> [ 10.000000] (2:Bob@Fafard) Trying to acquire 7
+> [ 10.000000] (2:Bob@Fafard) Trying to acquire 7 (not blocking)
 > [ 10.000000] (2:Bob@Fafard) Acquired 7
-> [ 11.000000] (1:Alice@Fafard) Trying to acquire 5
+> [ 11.000000] (1:Alice@Fafard) Trying to acquire 5 (blocking)
+> [ 14.000000] (1:Alice@Fafard) Timeout.. Try again 5
 > [ 15.000000] (2:Bob@Fafard) Releasing 8
 > [ 15.000000] (1:Alice@Fafard) Acquired 5
 > [ 15.000000] (2:Bob@Fafard) Released 8
 > [ 17.000000] (1:Alice@Fafard) Releasing 6
 > [ 17.000000] (1:Alice@Fafard) Released 6
-> [ 22.000000] (1:Alice@Fafard) Trying to acquire 7
+> [ 22.000000] (1:Alice@Fafard) Trying to acquire 7 (not blocking)
 > [ 22.000000] (1:Alice@Fafard) Acquired 7
 > [ 22.000000] (1:Alice@Fafard) Releasing 8
 > [ 22.000000] (1:Alice@Fafard) Released 8