Logo AND Algorithmique Numérique Distribuée

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