Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a15d28c069fb0ebe822b3c315664c8811560814f
[simgrid.git] / src / simix / smx_synchro.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
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 "private.h"
10 #include "xbt/log.h"
11
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix,
14                                 "Logging specific to SIMIX (synchronization)");
15
16
17 /****************************** Synchronization *******************************/
18
19 /*********************************** Mutex ************************************/
20 smx_mutex_t SIMIX_mutex_init()
21 {
22         smx_mutex_t m = xbt_new0(s_smx_mutex_t,1);
23         s_smx_process_t p; /* useful to initialize sleeping swag */
24         /* structures initialization */
25         m->using = 0;
26         m->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
27         return m;
28 }
29
30 void SIMIX_mutex_lock(smx_mutex_t mutex)
31 {
32         smx_process_t self = SIMIX_process_self();
33
34         xbt_assert0((mutex != NULL), "Invalid parameters");
35         
36         if (mutex->using) {
37                 /* somebody using the mutex, block */
38                 xbt_swag_insert(self, mutex->sleeping);
39                 self->simdata->mutex = mutex;
40                 __SIMIX_process_block(-1,"");
41                 self->simdata->mutex = NULL;
42                 mutex->using = 1;
43         }
44         else {
45                 /* mutex free */
46                 mutex->using = 1;
47         }
48         return;
49 }
50 /* return 1 if the process got the mutex, else 0. */
51 int SIMIX_mutex_trylock(smx_mutex_t mutex)
52 {
53         xbt_assert0((mutex != NULL), "Invalid parameters");
54         
55         if (mutex->using) 
56                 return 0;
57         else {
58                 mutex->using = 1;
59                 return 1;
60         }
61 }
62
63 void SIMIX_mutex_unlock(smx_mutex_t mutex)
64 {
65         smx_process_t p;        /*process to wake up */
66         
67         xbt_assert0((mutex != NULL), "Invalid parameters");
68         
69         if (xbt_swag_size(mutex->sleeping) > 0) {
70                 p = xbt_swag_extract(mutex->sleeping);
71                 mutex->using = 0;
72                 __SIMIX_process_unblock(p);
73         }
74         else {
75                 /* nobody to wape up */
76                 mutex->using = 0;
77         }
78         return;
79 }
80
81 SIMIX_error_t SIMIX_mutex_destroy(smx_mutex_t mutex)
82 {
83         if ( mutex == NULL )
84                 return SIMIX_WARNING;
85         else {
86                 xbt_swag_free(mutex->sleeping);
87                 xbt_free(mutex);
88                 return SIMIX_OK;
89         }
90 }
91
92 /******************************** Conditional *********************************/
93 smx_cond_t SIMIX_cond_init()
94 {
95         return xbt_new0(s_smx_cond_t,1);
96 }
97
98 void SIMIX_cond_signal(smx_cond_t cond)
99 {
100         return;
101 }
102
103 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
104 {
105         return;
106 }
107
108 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
109 {
110         return;
111 }
112
113 void SIMIX_cond_broadcast(smx_cond_t cond)
114 {
115         return;
116 }
117
118 void SIMIX_cond_destroy(smx_cond_t cond)
119 {
120         return;
121 }