Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'coverity_scan' of github.com:mquinson/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 "msg_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/synchro_core.h"
10 #include "xbt/log.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_synchro, msg,
13                                 "Logging specific to MSG (synchro)");
14
15
16 /** @addtogroup msg_synchro
17  *
18  *  @{
19  */
20
21 /********************************* Host **************************************/
22
23 /** @brief creates a semaphore object of the given initial capacity */
24 msg_sem_t MSG_sem_init(int initial_value) {
25   return simcall_sem_init(initial_value);
26 }
27
28 /** @brief locks on a semaphore object */
29 void MSG_sem_acquire(msg_sem_t sem) {
30   simcall_sem_acquire(sem);
31 }
32 /** @brief locks on a semaphore object up until the provided timeout expires */
33 msg_error_t MSG_sem_acquire_timeout(msg_sem_t sem, double timeout) {
34   xbt_ex_t e;
35   msg_error_t res = MSG_OK;
36   TRY {
37     simcall_sem_acquire_timeout(sem,timeout);
38   } CATCH(e) {
39     if (e.category == timeout_error) {
40       res = MSG_TIMEOUT;
41       xbt_ex_free(e);
42     } else {
43       RETHROW;
44     }
45   }
46   return res;
47 }
48 /** @brief releases the semaphore object */
49 void MSG_sem_release(msg_sem_t sem) {
50   simcall_sem_release(sem);
51 }
52 void MSG_sem_get_capacity(msg_sem_t sem) {
53   simcall_sem_get_capacity(sem);
54 }
55
56 void MSG_sem_destroy(msg_sem_t sem) {
57   SIMIX_sem_destroy(sem);
58 }
59 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
60  *
61  * Note that the returned value may be wrong right after the function call, when you try to use it...
62  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
63  */
64 int MSG_sem_would_block(msg_sem_t sem) {
65   return simcall_sem_would_block(sem);
66 }
67
68 /** @brief Initializes a barrier, with count elements */
69 msg_bar_t MSG_barrier_init(unsigned int count) {
70    return (msg_bar_t)xbt_barrier_init(count);
71 }
72
73 /** @brief Initializes a barrier, with count elements */
74 void MSG_barrier_destroy(msg_bar_t bar) {
75   xbt_barrier_destroy((xbt_bar_t)bar);
76 }
77
78 /** @brief Performs a barrier already initialized */
79 int MSG_barrier_wait(msg_bar_t bar) {
80   if(xbt_barrier_wait((xbt_bar_t)bar) == XBT_BARRIER_SERIAL_PROCESS)
81     return MSG_BARRIER_SERIAL_PROCESS;
82   else
83     return 0;
84 }
85
86 /**@}*/