Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid unsafe things
[simgrid.git] / src / msg / msg_synchro.cpp
1 /* Copyright (c) 2013-2014. 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 "xbt/synchro.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_synchro, msg, "Logging specific to MSG (synchro)");
13
14 /** @addtogroup msg_synchro
15  *
16  *  @{
17  */
18
19 /** @brief creates a semaphore object of the given initial capacity */
20 msg_sem_t MSG_sem_init(int initial_value) {
21   return simcall_sem_init(initial_value);
22 }
23
24 /** @brief locks on a semaphore object */
25 void MSG_sem_acquire(msg_sem_t sem) {
26   simcall_sem_acquire(sem);
27 }
28
29 /** @brief locks on a semaphore object up until the provided timeout expires */
30 msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout) {
31   msg_error_t res = MSG_OK;
32   try {
33     simcall_sem_acquire_timeout(sem,timeout);
34   } catch(xbt_ex& e) {
35     if (e.category == timeout_error)
36       return MSG_TIMEOUT;
37     throw;
38   }
39   return res;
40 }
41
42 /** @brief releases the semaphore object */
43 void MSG_sem_release(msg_sem_t sem) {
44   simcall_sem_release(sem);
45 }
46
47 int MSG_sem_get_capacity(msg_sem_t sem) {
48   return simcall_sem_get_capacity(sem);
49 }
50
51 void MSG_sem_destroy(msg_sem_t sem) {
52   SIMIX_sem_destroy(sem);
53 }
54
55 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
56  *
57  * Note that the returned value may be wrong right after the function call, when you try to use it...
58  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
59  */
60 int MSG_sem_would_block(msg_sem_t sem) {
61   return simcall_sem_would_block(sem);
62 }
63
64 /*-**** barrier related functions ****-*/
65 typedef struct s_msg_bar {
66   xbt_mutex_t mutex;
67   xbt_cond_t cond;
68   unsigned int arrived_processes;
69   unsigned int expected_processes;
70 } s_msg_bar_t;
71
72 /** @brief Initializes a barrier, with count elements */
73 msg_bar_t MSG_barrier_init(unsigned int count) {
74   msg_bar_t bar           = xbt_new0(s_msg_bar, 1);
75   bar->expected_processes = count;
76   bar->arrived_processes  = 0;
77   bar->mutex              = xbt_mutex_init();
78   bar->cond               = xbt_cond_init();
79   return bar;
80 }
81
82 /** @brief Initializes a barrier, with count elements */
83 void MSG_barrier_destroy(msg_bar_t bar) {
84   xbt_mutex_destroy(bar->mutex);
85   xbt_cond_destroy(bar->cond);
86   xbt_free(bar);
87 }
88
89 /** @brief Performs a barrier already initialized */
90 int MSG_barrier_wait(msg_bar_t bar) {
91   xbt_mutex_acquire(bar->mutex);
92   bar->arrived_processes++;
93   if (bar->arrived_processes == bar->expected_processes) {
94     xbt_cond_broadcast(bar->cond);
95     xbt_mutex_release(bar->mutex);
96     bar->arrived_processes = 0;
97     return MSG_BARRIER_SERIAL_PROCESS;
98   }
99
100   xbt_cond_wait(bar->cond, bar->mutex);
101   xbt_mutex_release(bar->mutex);
102   return 0;
103 }
104 /**@}*/