Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ad97cb437453ed516aae0f8e6ec9741e962c2b40
[simgrid.git] / src / xbt / xbt_os_synchro.cpp
1 /* Classical synchro schema, implemented on top of SimGrid                  */
2
3 /* Copyright (c) 2007-2018. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/ex.hpp"
10 #include "xbt/synchro.h"
11
12 #include "simgrid/simix.h" /* used implementation */
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync, xbt, "Synchronization mechanism");
15
16 /****** mutex related functions ******/
17 xbt_mutex_t xbt_mutex_init(void)
18 {
19   return (xbt_mutex_t)simcall_mutex_init();
20 }
21
22 void xbt_mutex_acquire(xbt_mutex_t mutex)
23 {
24   simcall_mutex_lock((smx_mutex_t)mutex);
25 }
26
27 int xbt_mutex_try_acquire(xbt_mutex_t mutex)
28 {
29   return simcall_mutex_trylock((smx_mutex_t)mutex);
30 }
31
32 void xbt_mutex_release(xbt_mutex_t mutex)
33 {
34   simcall_mutex_unlock((smx_mutex_t)mutex);
35 }
36
37 void xbt_mutex_destroy(xbt_mutex_t mutex)
38 {
39   SIMIX_mutex_unref((smx_mutex_t)mutex);
40 }
41
42 /***** condition related functions *****/
43 xbt_cond_t xbt_cond_init(void)
44 {
45   return (xbt_cond_t)simcall_cond_init();
46 }
47
48 void xbt_cond_wait(xbt_cond_t cond, xbt_mutex_t mutex)
49 {
50   simcall_cond_wait((smx_cond_t)cond, (smx_mutex_t)mutex);
51 }
52
53 int xbt_cond_timedwait(xbt_cond_t cond, xbt_mutex_t mutex, double delay)
54 {
55   try {
56     simcall_cond_wait_timeout((smx_cond_t)cond, (smx_mutex_t)mutex, delay);
57   } catch (xbt_ex& e) {
58     if (e.category == timeout_error) {
59       return 1;
60     } else {
61       throw; // rethrow the exceptions that I don't know
62     }
63   }
64   return 0;
65 }
66
67 void xbt_cond_signal(xbt_cond_t cond)
68 {
69   simcall_cond_signal((smx_cond_t)cond);
70 }
71
72 void xbt_cond_broadcast(xbt_cond_t cond)
73 {
74   simcall_cond_broadcast((smx_cond_t)cond);
75 }
76
77 void xbt_cond_destroy(xbt_cond_t cond)
78 {
79   SIMIX_cond_unref((smx_cond_t)cond);
80 }