Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid costly exceptions when looking into a map.
[simgrid.git] / src / msg / msg_synchro.cpp
1 /* Copyright (c) 2013-2017. 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 "xbt/ex.hpp"
8
9 #include "msg_private.h"
10 #include "src/simix/smx_private.h"
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::kernelImmediate([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   msg_error_t res = MSG_OK;
33   try {
34     simcall_sem_acquire_timeout(sem,timeout);
35   } catch(xbt_ex& e) {
36     if (e.category == timeout_error)
37       return MSG_TIMEOUT;
38     throw;
39   }
40   return res;
41 }
42
43 /** @brief releases the semaphore object */
44 void MSG_sem_release(msg_sem_t sem) {
45   simgrid::simix::kernelImmediate([sem] { SIMIX_sem_release(sem); });
46 }
47
48 int MSG_sem_get_capacity(msg_sem_t sem) {
49   return simgrid::simix::kernelImmediate([sem] { return SIMIX_sem_get_capacity(sem); });
50 }
51
52 void MSG_sem_destroy(msg_sem_t sem) {
53   SIMIX_sem_destroy(sem);
54 }
55
56 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
57  *
58  * Note that the returned value may be wrong right after the function call, when you try to use it...
59  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
60  */
61 int MSG_sem_would_block(msg_sem_t sem) {
62   return simgrid::simix::kernelImmediate([sem] { return SIMIX_sem_would_block(sem); });
63 }
64
65 /*-**** barrier related functions ****-*/
66 typedef struct s_msg_bar {
67   xbt_mutex_t mutex;
68   xbt_cond_t cond;
69   unsigned int arrived_processes;
70   unsigned int expected_processes;
71 } s_msg_bar_t;
72
73 /** @brief Initializes a barrier, with count elements */
74 msg_bar_t MSG_barrier_init(unsigned int count) {
75   msg_bar_t bar           = xbt_new0(s_msg_bar, 1);
76   bar->expected_processes = count;
77   bar->arrived_processes  = 0;
78   bar->mutex              = xbt_mutex_init();
79   bar->cond               = xbt_cond_init();
80   return bar;
81 }
82
83 /** @brief Initializes a barrier, with count elements */
84 void MSG_barrier_destroy(msg_bar_t bar) {
85   xbt_mutex_destroy(bar->mutex);
86   xbt_cond_destroy(bar->cond);
87   xbt_free(bar);
88 }
89
90 /** @brief Performs a barrier already initialized */
91 int MSG_barrier_wait(msg_bar_t bar) {
92   xbt_mutex_acquire(bar->mutex);
93   bar->arrived_processes++;
94   XBT_DEBUG("waiting %p %u/%u", bar, bar->arrived_processes, bar->expected_processes);
95   if (bar->arrived_processes == bar->expected_processes) {
96     xbt_cond_broadcast(bar->cond);
97     xbt_mutex_release(bar->mutex);
98     bar->arrived_processes = 0;
99     return MSG_BARRIER_SERIAL_PROCESS;
100   }
101
102   xbt_cond_wait(bar->cond, bar->mutex);
103   xbt_mutex_release(bar->mutex);
104   return 0;
105 }
106 /**@}*/