Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Commit the autogenerated files
[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 void SIMIX_mutex_destroy(smx_mutex_t mutex)
82 {
83         if ( mutex == NULL )
84                 return ;
85         else {
86                 xbt_swag_free(mutex->sleeping);
87                 xbt_free(mutex);
88                 return ;
89         }
90 }
91
92 /******************************** Conditional *********************************/
93 smx_cond_t SIMIX_cond_init()
94 {
95         smx_cond_t cond = xbt_new0(s_smx_cond_t,1);
96         s_smx_process_t p;
97         
98         cond->sleeping = xbt_swag_new(xbt_swag_offset(p,synchro_hookup));
99         cond->actions = xbt_fifo_new();
100         cond->mutex = NULL;
101         return cond;
102 }
103
104 void SIMIX_cond_signal(smx_cond_t cond)
105 {
106         xbt_assert0((cond != NULL), "Invalid parameters");
107         smx_process_t proc = NULL;
108
109         if (xbt_swag_size(cond->sleeping) >= 1) {
110                 proc = xbt_swag_extract(cond->sleeping);
111                 __SIMIX_process_unblock(proc);
112         }
113
114         return;
115 }
116
117 void SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex)
118 {
119         /* call the function with timeout, with max_duration > 0   
120          * the process is blocked forever */
121         SIMIX_cond_wait_timeout(cond, mutex, -1);
122         return;
123 }
124
125 void SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration)
126 {
127         smx_process_t self = SIMIX_process_self();
128
129         xbt_assert0((mutex != NULL), "Invalid parameters");
130         
131         /* process status */    
132         self->simdata->cond = cond;
133
134         cond->mutex = mutex;
135
136         SIMIX_mutex_unlock(mutex);
137         xbt_swag_insert(self, cond->sleeping);
138         /* creates a new action to be the timeout */
139         if (max_duration >=0) {
140                 __SIMIX_process_block(max_duration);
141                 self->simdata->cond = NULL;
142         }
143         else {
144                 __SIMIX_process_block(-1);
145                 self->simdata->cond = NULL;
146         }
147         /* get the mutex again */
148         SIMIX_mutex_lock(cond->mutex);
149
150         return;
151 }
152
153
154 void SIMIX_cond_broadcast(smx_cond_t cond)
155 {
156         xbt_assert0((cond != NULL), "Invalid parameters");
157         smx_process_t proc = NULL;
158         smx_process_t proc_next = NULL;
159
160         xbt_swag_foreach_safe(proc,proc_next,cond->sleeping) {
161                 __SIMIX_process_unblock(proc);
162                 xbt_swag_remove(proc,cond->sleeping);
163         }
164
165         return;
166 }
167
168 void SIMIX_cond_destroy(smx_cond_t cond)
169 {
170         
171         if ( cond == NULL )
172                 return ;
173         else {
174                 xbt_assert0( xbt_swag_size(cond->sleeping) == 0 , "Cannot destroy conditional");
175
176                 xbt_swag_free(cond->sleeping);
177                 xbt_fifo_free(cond->actions);
178                 xbt_free(cond);
179                 return;
180         }
181 }
182
183 void SIMIX_register_condition_to_action(smx_action_t action, smx_cond_t cond)
184 {
185         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
186
187         xbt_fifo_push(action->simdata->cond_list,cond);
188 }
189
190