Rev 2482 | Rev 2492 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2482 | Rev 2483 | ||
---|---|---|---|
Line 46... | Line 46... | ||
46 | 46 | ||
47 | #ifndef FIBRIL_INITIAL_STACK_PAGES_NO |
47 | #ifndef FIBRIL_INITIAL_STACK_PAGES_NO |
48 | #define FIBRIL_INITIAL_STACK_PAGES_NO 1 |
48 | #define FIBRIL_INITIAL_STACK_PAGES_NO 1 |
49 | #endif |
49 | #endif |
50 | 50 | ||
- | 51 | /** This futex serializes access to ready_list, serialized_list and manage_list. |
|
- | 52 | */ |
|
- | 53 | static atomic_t fibril_futex = FUTEX_INITIALIZER; |
|
- | 54 | ||
51 | static LIST_INITIALIZE(ready_list); |
55 | static LIST_INITIALIZE(ready_list); |
52 | static LIST_INITIALIZE(serialized_list); |
56 | static LIST_INITIALIZE(serialized_list); |
53 | static LIST_INITIALIZE(manager_list); |
57 | static LIST_INITIALIZE(manager_list); |
54 | 58 | ||
55 | static void fibril_main(void); |
59 | static void fibril_main(void); |
56 | 60 | ||
57 | static atomic_t fibril_futex = FUTEX_INITIALIZER; |
- | |
58 | /** Number of threads that are in async_serialized mode */ |
61 | /** Number of fibrils that are in async_serialized mode */ |
59 | static int serialized_threads; /* Protected by async_futex */ |
62 | static int serialized_fibrils; /* Protected by async_futex */ |
60 | /** Thread-local count of serialization. If >0, we must not preempt */ |
63 | /** Thread-local count of serialization. If >0, we must not preempt */ |
61 | static __thread int serialization_count; |
64 | static __thread int serialization_count; |
62 | /** Counter for fibrils residing in async_manager */ |
65 | /** Counter for fibrils residing in async_manager */ |
63 | static int fibrils_in_manager; |
66 | static int fibrils_in_manager; |
64 | 67 | ||
Line 90... | Line 93... | ||
90 | free(f); |
93 | free(f); |
91 | } |
94 | } |
92 | 95 | ||
93 | /** Function that spans the whole life-cycle of a fibril. |
96 | /** Function that spans the whole life-cycle of a fibril. |
94 | * |
97 | * |
95 | * Each fibril begins execution in this function. Then the function |
98 | * Each fibril begins execution in this function. Then the function implementing |
96 | * implementing the fibril logic is called. After its return, the return value |
99 | * the fibril logic is called. After its return, the return value is saved. |
97 | * is saved for a potentional joiner. If the joiner exists, it is woken up. The |
- | |
98 | * fibril then switches to another fibril, which cleans up after it. |
100 | * The fibril then switches to another fibril, which cleans up after it. |
99 | */ |
101 | */ |
100 | void fibril_main(void) |
102 | void fibril_main(void) |
101 | { |
103 | { |
102 | fibril_t *f = __tcb_get()->fibril_data; |
104 | fibril_t *f = __tcb_get()->fibril_data; |
103 | 105 | ||
- | 106 | /* Call the implementing function. */ |
|
104 | f->retval = f->func(f->arg); |
107 | f->retval = f->func(f->arg); |
105 | 108 | ||
106 | /* |
- | |
107 | * If there is a joiner, wake it up and save our return value. |
- | |
108 | */ |
- | |
109 | if (f->joiner) { |
- | |
110 | list_append(&f->joiner->link, &ready_list); |
- | |
111 | f->joiner->joinee_retval = f->retval; |
- | |
112 | } |
- | |
113 | - | ||
114 | fibril_schedule_next_adv(FIBRIL_FROM_DEAD); |
109 | fibril_schedule_next_adv(FIBRIL_FROM_DEAD); |
115 | /* not reached */ |
110 | /* not reached */ |
116 | } |
111 | } |
117 | 112 | ||
118 | /** Schedule next fibril. |
113 | /** Schedule next fibril. |
119 | * |
114 | * |
120 | * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be |
115 | * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be |
121 | * held. |
116 | * held. |
122 | * |
117 | * |
123 | * @param stype One of FIBRIL_SLEEP, FIBRIL_PREEMPT, FIBRIL_TO_MANAGER, |
118 | * @param stype Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER, |
124 | * FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter |
119 | * FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter |
125 | * describes the circumstances of the switch. |
120 | * describes the circumstances of the switch. |
126 | * @return Return 0 if there is no ready fibril, |
121 | * @return Return 0 if there is no ready fibril, |
127 | * return 1 otherwise. |
122 | * return 1 otherwise. |
128 | */ |
123 | */ |
Line 133... | Line 128... | ||
133 | 128 | ||
134 | futex_down(&fibril_futex); |
129 | futex_down(&fibril_futex); |
135 | 130 | ||
136 | if (stype == FIBRIL_PREEMPT && list_empty(&ready_list)) |
131 | if (stype == FIBRIL_PREEMPT && list_empty(&ready_list)) |
137 | goto ret_0; |
132 | goto ret_0; |
138 | if (stype == FIBRIL_SLEEP) { |
- | |
139 | if (list_empty(&ready_list) && list_empty(&serialized_list)) |
- | |
140 | goto ret_0; |
- | |
141 | } |
- | |
142 | 133 | ||
143 | if (stype == FIBRIL_FROM_MANAGER) { |
134 | if (stype == FIBRIL_FROM_MANAGER) { |
144 | if (list_empty(&ready_list) && list_empty(&serialized_list)) |
135 | if (list_empty(&ready_list) && list_empty(&serialized_list)) |
145 | goto ret_0; |
136 | goto ret_0; |
146 | /* |
137 | /* |
147 | * Do not preempt if there is not sufficient count of thread |
138 | * Do not preempt if there is not sufficient count of fibril |
148 | * managers. |
139 | * managers. |
149 | */ |
140 | */ |
150 | if (list_empty(&serialized_list) && fibrils_in_manager <= |
141 | if (list_empty(&serialized_list) && fibrils_in_manager <= |
151 | serialized_threads) { |
142 | serialized_fibrils) { |
152 | goto ret_0; |
143 | goto ret_0; |
153 | } |
144 | } |
154 | } |
145 | } |
155 | /* If we are going to manager and none exists, create it */ |
146 | /* If we are going to manager and none exists, create it */ |
156 | if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) { |
147 | if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) { |
Line 188... | Line 179... | ||
188 | } else { |
179 | } else { |
189 | /* |
180 | /* |
190 | * If stype == FIBRIL_TO_MANAGER, don't put ourselves to |
181 | * If stype == FIBRIL_TO_MANAGER, don't put ourselves to |
191 | * any list, we should already be somewhere, or we will |
182 | * any list, we should already be somewhere, or we will |
192 | * be lost. |
183 | * be lost. |
193 | * |
- | |
194 | * The stype == FIBRIL_SLEEP case is similar. The fibril |
- | |
195 | * has an external refernce which can be used to wake it |
- | |
196 | * up once that time has come. |
- | |
197 | */ |
184 | */ |
198 | } |
185 | } |
199 | } |
186 | } |
200 | 187 | ||
201 | /* Choose a new fibril to run */ |
188 | /* Choose a new fibril to run */ |
202 | if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) { |
189 | if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) { |
203 | dstf = list_get_instance(manager_list.next, fibril_t, link); |
190 | dstf = list_get_instance(manager_list.next, fibril_t, link); |
204 | if (serialization_count && stype == FIBRIL_TO_MANAGER) { |
191 | if (serialization_count && stype == FIBRIL_TO_MANAGER) { |
205 | serialized_threads++; |
192 | serialized_fibrils++; |
206 | srcf->flags |= FIBRIL_SERIALIZED; |
193 | srcf->flags |= FIBRIL_SERIALIZED; |
207 | } |
194 | } |
208 | fibrils_in_manager++; |
195 | fibrils_in_manager++; |
209 | 196 | ||
210 | if (stype == FIBRIL_FROM_DEAD) |
197 | if (stype == FIBRIL_FROM_DEAD) |
211 | dstf->clean_after_me = srcf; |
198 | dstf->clean_after_me = srcf; |
212 | } else { |
199 | } else { |
213 | if (!list_empty(&serialized_list)) { |
200 | if (!list_empty(&serialized_list)) { |
214 | dstf = list_get_instance(serialized_list.next, fibril_t, |
201 | dstf = list_get_instance(serialized_list.next, fibril_t, |
215 | link); |
202 | link); |
216 | serialized_threads--; |
203 | serialized_fibrils--; |
217 | } else { |
204 | } else { |
218 | dstf = list_get_instance(ready_list.next, fibril_t, |
205 | dstf = list_get_instance(ready_list.next, fibril_t, |
219 | link); |
206 | link); |
220 | } |
207 | } |
221 | } |
208 | } |
Line 228... | Line 215... | ||
228 | ret_0: |
215 | ret_0: |
229 | futex_up(&fibril_futex); |
216 | futex_up(&fibril_futex); |
230 | return retval; |
217 | return retval; |
231 | } |
218 | } |
232 | 219 | ||
233 | /** Wait for fibril to finish. |
- | |
234 | * |
- | |
235 | * Each fibril can be only joined by one other fibril. Moreover, the joiner must |
- | |
236 | * be from the same thread as the joinee. |
- | |
237 | * |
- | |
238 | * @param fid Fibril to join. |
- | |
239 | * |
- | |
240 | * @return Value returned by the completed fibril. |
- | |
241 | */ |
- | |
242 | int fibril_join(fid_t fid) |
- | |
243 | { |
- | |
244 | fibril_t *f; |
- | |
245 | fibril_t *cur; |
- | |
246 | - | ||
247 | /* Handle fid = Kernel address -> it is wait for call */ |
- | |
248 | f = (fibril_t *) fid; |
- | |
249 | - | ||
250 | /* |
- | |
251 | * The joiner is running so the joinee isn't. |
- | |
252 | */ |
- | |
253 | cur = __tcb_get()->fibril_data; |
- | |
254 | f->joiner = cur; |
- | |
255 | fibril_schedule_next_adv(FIBRIL_SLEEP); |
- | |
256 | - | ||
257 | /* |
- | |
258 | * The joinee fills in the return value. |
- | |
259 | */ |
- | |
260 | return cur->joinee_retval; |
- | |
261 | } |
- | |
262 | - | ||
263 | /** Create a new fibril. |
220 | /** Create a new fibril. |
264 | * |
221 | * |
265 | * @param func Implementing function of the new fibril. |
222 | * @param func Implementing function of the new fibril. |
266 | * @param arg Argument to pass to func. |
223 | * @param arg Argument to pass to func. |
267 | * |
224 | * |
Line 283... | Line 240... | ||
283 | } |
240 | } |
284 | 241 | ||
285 | f->arg = arg; |
242 | f->arg = arg; |
286 | f->func = func; |
243 | f->func = func; |
287 | f->clean_after_me = NULL; |
244 | f->clean_after_me = NULL; |
288 | f->joiner = NULL; |
- | |
289 | f->joinee_retval = 0; |
- | |
290 | f->retval = 0; |
245 | f->retval = 0; |
291 | f->flags = 0; |
246 | f->flags = 0; |
292 | 247 | ||
293 | context_save(&f->ctx); |
248 | context_save(&f->ctx); |
294 | context_set(&f->ctx, FADDR(fibril_main), f->stack, |
249 | context_set(&f->ctx, FADDR(fibril_main), f->stack, |