Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Travis: actually get the packages I just made available
[simgrid.git] / src / msg / msg_synchro.cpp
1 /* Copyright (c) 2013-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "xbt/ex.hpp"
7
8 #include "msg_private.hpp"
9 #include "src/simix/smx_private.hpp"
10 #include "src/simix/smx_synchro_private.hpp"
11 #include "xbt/synchro.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_synchro, msg, "Logging specific to MSG (synchro)");
14
15 /** @addtogroup msg_synchro
16  *
17  *  @{
18  */
19
20 /** @brief creates a semaphore object of the given initial capacity */
21 msg_sem_t MSG_sem_init(int initial_value) {
22   return simgrid::simix::simcall([initial_value] { return SIMIX_sem_init(initial_value); });
23 }
24
25 /** @brief locks on a semaphore object */
26 void MSG_sem_acquire(msg_sem_t sem) {
27   simcall_sem_acquire(sem);
28 }
29
30 /** @brief locks on a semaphore object up until the provided timeout expires */
31 msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout) {
32   return simcall_sem_acquire_timeout(sem, timeout) ? MSG_TIMEOUT : MSG_OK;
33 }
34
35 /** @brief releases the semaphore object */
36 void MSG_sem_release(msg_sem_t sem) {
37   simgrid::simix::simcall([sem] { SIMIX_sem_release(sem); });
38 }
39
40 int MSG_sem_get_capacity(msg_sem_t sem) {
41   return simgrid::simix::simcall([sem] { return SIMIX_sem_get_capacity(sem); });
42 }
43
44 void MSG_sem_destroy(msg_sem_t sem) {
45   SIMIX_sem_destroy(sem);
46 }
47
48 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
49  *
50  * Note that the returned value may be wrong right after the function call, when you try to use it...
51  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
52  */
53 int MSG_sem_would_block(msg_sem_t sem) {
54   return simgrid::simix::simcall([sem] { return SIMIX_sem_would_block(sem); });
55 }
56
57 /*-**** barrier related functions ****-*/
58 struct s_msg_bar_t {
59   xbt_mutex_t mutex;
60   xbt_cond_t cond;
61   unsigned int arrived_processes;
62   unsigned int expected_processes;
63 };
64
65 /** @brief Initializes a barrier, with count elements */
66 msg_bar_t MSG_barrier_init(unsigned int count) {
67   msg_bar_t bar           = new s_msg_bar_t;
68   bar->expected_processes = count;
69   bar->arrived_processes  = 0;
70   bar->mutex              = xbt_mutex_init();
71   bar->cond               = xbt_cond_init();
72   return bar;
73 }
74
75 /** @brief Initializes a barrier, with count elements */
76 void MSG_barrier_destroy(msg_bar_t bar) {
77   xbt_mutex_destroy(bar->mutex);
78   xbt_cond_destroy(bar->cond);
79   delete bar;
80 }
81
82 /** @brief Performs a barrier already initialized */
83 int MSG_barrier_wait(msg_bar_t bar) {
84   xbt_mutex_acquire(bar->mutex);
85   bar->arrived_processes++;
86   XBT_DEBUG("waiting %p %u/%u", bar, bar->arrived_processes, bar->expected_processes);
87   if (bar->arrived_processes == bar->expected_processes) {
88     xbt_cond_broadcast(bar->cond);
89     xbt_mutex_release(bar->mutex);
90     bar->arrived_processes = 0;
91     return MSG_BARRIER_SERIAL_PROCESS;
92   }
93
94   xbt_cond_wait(bar->cond, bar->mutex);
95   xbt_mutex_release(bar->mutex);
96   return 0;
97 }
98 /**@}*/