Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove a level of constness for argv parameter of sg_actor_start/create.
[simgrid.git] / examples / c / synchro-semaphore / synchro-semaphore.c
1 /* Copyright (c) 2013-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/actor.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10 #include "simgrid/semaphore.h"
11
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(semaphores, "Messages specific for this example");
16
17 sg_sem_t sem;
18
19 static void peer(int argc, char* argv[])
20 {
21   int i = 0;
22   while (i < argc) {
23     double wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
24     i++;
25     sg_actor_sleep_for(wait_time);
26     XBT_INFO("Trying to acquire %d (%sblocking)", i, sg_sem_would_block(sem) ? "" : "not ");
27     // cover the two cases: with and without timeout
28     if (i > 1) {
29       while (sg_sem_acquire_timeout(sem, 3.0))
30         XBT_INFO("Timeout.. Try again %d", i);
31     } else {
32       sg_sem_acquire(sem);
33     }
34     XBT_INFO("Acquired %d", i);
35
36     wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
37     i++;
38     sg_actor_sleep_for(wait_time);
39     XBT_INFO("Releasing %d", i);
40     sg_sem_release(sem);
41     XBT_INFO("Released %d", i);
42   }
43   sg_actor_sleep_for(50);
44   XBT_INFO("Done");
45 }
46
47 int main(int argc, char* argv[])
48 {
49   simgrid_init(&argc, argv);
50   simgrid_load_platform(argv[1]);
51
52   sg_host_t h = sg_host_by_name("Fafard");
53
54   sem                      = sg_sem_init(1);
55   XBT_INFO("Semaphore initialized with capacity = %d", sg_sem_get_capacity(sem));
56   const char* aliceTimes[] = {"0", "1", "3", "5", "1", "2", "5", "0"};
57   const char* bobTimes[]   = {"0.9", "1", "1", "2", "2", "0", "0", "5"};
58
59   sg_actor_create("Alice", h, peer, 8, (char**)aliceTimes);
60   sg_actor_create("Bob", h, peer, 8, (char**)bobTimes);
61
62   simgrid_run();
63   sg_sem_destroy(sem);
64   XBT_INFO("Finished\n");
65   return 0;
66 }