1 | /* |
---|
2 | * pthread.c - User level <pthread> library implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_user.h> |
---|
25 | #include <hal_shared_types.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <pthread.h> |
---|
29 | #include <shared_pthread.h> |
---|
30 | #include <almosmkh.h> |
---|
31 | #include <syscalls_numbers.h> |
---|
32 | |
---|
33 | |
---|
34 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
35 | // Threads |
---|
36 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
37 | |
---|
38 | ///////////////////////////////////////////////// |
---|
39 | int pthread_create( pthread_t * trdid, |
---|
40 | const pthread_attr_t * attr, |
---|
41 | void * start_func, |
---|
42 | void * start_args ) |
---|
43 | { |
---|
44 | return hal_user_syscall( SYS_THREAD_CREATE, |
---|
45 | (reg_t)trdid, |
---|
46 | (reg_t)attr, |
---|
47 | (reg_t)start_func, |
---|
48 | (reg_t)start_args ); |
---|
49 | } |
---|
50 | |
---|
51 | ///////////////////////////////////// |
---|
52 | int pthread_join( pthread_t trdid, |
---|
53 | void ** exit_value ) |
---|
54 | { |
---|
55 | return hal_user_syscall( SYS_THREAD_JOIN, |
---|
56 | (reg_t)trdid, |
---|
57 | (reg_t)exit_value, 0, 0 ); |
---|
58 | } |
---|
59 | |
---|
60 | /////////////////////////////////////// |
---|
61 | int pthread_detach( pthread_t trdid ) |
---|
62 | { |
---|
63 | return hal_user_syscall( SYS_THREAD_DETACH, |
---|
64 | (reg_t)trdid, 0, 0, 0 ); |
---|
65 | } |
---|
66 | |
---|
67 | ///////////////////////////////////// |
---|
68 | int pthread_exit( void * exit_value ) |
---|
69 | { |
---|
70 | return hal_user_syscall( SYS_THREAD_EXIT, |
---|
71 | (reg_t)exit_value, 0, 0, 0 ); |
---|
72 | } |
---|
73 | |
---|
74 | ///////////////////////// |
---|
75 | int pthread_yield( void ) |
---|
76 | { |
---|
77 | return hal_user_syscall( SYS_THREAD_YIELD, 0, 0, 0, 0 ); |
---|
78 | } |
---|
79 | |
---|
80 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
81 | // Mutexes |
---|
82 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
83 | |
---|
84 | ////////////////////////////////////////////////////////// |
---|
85 | int pthread_mutex_init( pthread_mutex_t * mutex, |
---|
86 | const pthread_mutexattr_t * attr ) |
---|
87 | { |
---|
88 | if( attr != NULL ) |
---|
89 | { |
---|
90 | printf("\n[ERROR] in %s : <attr> argument not supported\n", __FUNCTION__); |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | return hal_user_syscall( SYS_MUTEX, |
---|
95 | (reg_t)mutex, |
---|
96 | MUTEX_INIT, |
---|
97 | 0, 0 ); |
---|
98 | } |
---|
99 | |
---|
100 | //////////////////////////////////////////////////// |
---|
101 | int pthread_mutex_destroy( pthread_mutex_t * mutex ) |
---|
102 | { |
---|
103 | return hal_user_syscall( SYS_MUTEX, |
---|
104 | (reg_t)mutex, |
---|
105 | MUTEX_DESTROY, |
---|
106 | 0, 0 ); |
---|
107 | } |
---|
108 | |
---|
109 | ///////////////////////////////////////////////// |
---|
110 | int pthread_mutex_lock( pthread_mutex_t * mutex ) |
---|
111 | { |
---|
112 | return hal_user_syscall( SYS_MUTEX, |
---|
113 | (reg_t)mutex, |
---|
114 | MUTEX_LOCK, |
---|
115 | 0, 0 ); |
---|
116 | } |
---|
117 | |
---|
118 | //////////////////////////////////////////////////// |
---|
119 | int pthread_mutex_trylock( pthread_mutex_t * mutex ) |
---|
120 | { |
---|
121 | return hal_user_syscall( SYS_MUTEX, |
---|
122 | (reg_t)mutex, |
---|
123 | MUTEX_TRYLOCK, |
---|
124 | 0, 0 ); |
---|
125 | } |
---|
126 | |
---|
127 | /////////////////////////////////////////////////// |
---|
128 | int pthread_mutex_unlock( pthread_mutex_t * mutex ) |
---|
129 | { |
---|
130 | return hal_user_syscall( SYS_MUTEX, |
---|
131 | (reg_t)mutex, |
---|
132 | MUTEX_UNLOCK, |
---|
133 | 0, 0 ); |
---|
134 | } |
---|
135 | |
---|
136 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
137 | // Condvars |
---|
138 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
139 | |
---|
140 | /////////////////////////////////////////////// |
---|
141 | int pthread_cond_init( pthread_cond_t * cond, |
---|
142 | pthread_condattr_t * attr ) |
---|
143 | { |
---|
144 | if( attr ) |
---|
145 | { |
---|
146 | printf("[ERROR] in %s ; <attr> argument must be NULL\n", __FUNCTION__ ); |
---|
147 | return -1; |
---|
148 | } |
---|
149 | |
---|
150 | return hal_user_syscall( SYS_CONDVAR, |
---|
151 | (reg_t)cond, |
---|
152 | CONDVAR_INIT, |
---|
153 | 0, 0 ); |
---|
154 | } |
---|
155 | |
---|
156 | ///////////////////////////////////////////////// |
---|
157 | int pthread_cond_destroy( pthread_cond_t * cond ) |
---|
158 | { |
---|
159 | return hal_user_syscall( SYS_CONDVAR, |
---|
160 | (reg_t)cond, |
---|
161 | CONDVAR_DESTROY, |
---|
162 | 0, 0 ); |
---|
163 | } |
---|
164 | |
---|
165 | ////////////////////////////////////////////// |
---|
166 | int pthread_cond_wait( pthread_cond_t * cond, |
---|
167 | pthread_mutex_t * mutex ) |
---|
168 | { |
---|
169 | return hal_user_syscall( SYS_CONDVAR, |
---|
170 | (reg_t)cond, |
---|
171 | CONDVAR_WAIT, |
---|
172 | (reg_t)mutex, |
---|
173 | 0 ); |
---|
174 | } |
---|
175 | |
---|
176 | //////////////////////////////////////////////// |
---|
177 | int pthread_cond_signal( pthread_cond_t * cond ) |
---|
178 | { |
---|
179 | return hal_user_syscall( SYS_CONDVAR, |
---|
180 | (reg_t)cond, |
---|
181 | CONDVAR_SIGNAL, |
---|
182 | 0, 0 ); |
---|
183 | } |
---|
184 | |
---|
185 | /////////////////////////////////////////////////// |
---|
186 | int pthread_cond_broadcast( pthread_cond_t * cond ) |
---|
187 | { |
---|
188 | return hal_user_syscall( SYS_CONDVAR, |
---|
189 | (reg_t)cond, |
---|
190 | CONDVAR_BROADCAST, |
---|
191 | 0, 0 ); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
196 | // Barriers |
---|
197 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
198 | |
---|
199 | //////////////////////////////////////////////////////////////// |
---|
200 | int pthread_barrier_init( pthread_barrier_t * barrier, |
---|
201 | const pthread_barrierattr_t * attr, |
---|
202 | unsigned int count ) |
---|
203 | { |
---|
204 | if ( attr ) |
---|
205 | { |
---|
206 | printf("[ERROR] in %s ; <attr> argument must be NULL\n", __FUNCTION__ ); |
---|
207 | return -1; |
---|
208 | } |
---|
209 | |
---|
210 | return hal_user_syscall( SYS_BARRIER, |
---|
211 | (reg_t)barrier, |
---|
212 | BARRIER_INIT, |
---|
213 | (reg_t)count, |
---|
214 | 0 ); |
---|
215 | } |
---|
216 | |
---|
217 | ////////////////////////////////////////////////////////// |
---|
218 | int pthread_barrier_destroy( pthread_barrier_t * barrier ) |
---|
219 | { |
---|
220 | return hal_user_syscall( SYS_BARRIER, |
---|
221 | (reg_t)barrier, |
---|
222 | BARRIER_DESTROY, |
---|
223 | 0, 0 ); |
---|
224 | } |
---|
225 | |
---|
226 | /////////////////////////////////////////////////////// |
---|
227 | int pthread_barrier_wait( pthread_barrier_t * barrier ) |
---|
228 | { |
---|
229 | return hal_user_syscall( SYS_BARRIER, |
---|
230 | (reg_t)barrier, |
---|
231 | BARRIER_WAIT, |
---|
232 | 0, 0 ); |
---|
233 | } |
---|
234 | |
---|
235 | /* |
---|
236 | |
---|
237 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
238 | // The following functions define another implementation for the POSX barrier |
---|
239 | // based on a distributed quadtree implemented in user space, and relying |
---|
240 | // on a busy waiting policy. |
---|
241 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
242 | |
---|
243 | |
---|
244 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
245 | // This recursive function initializes the SQT nodes |
---|
246 | // traversing the SQT from root to bottom |
---|
247 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
248 | static void sqt_barrier_build( pthread_barrier_t * barrier, |
---|
249 | unsigned int x, |
---|
250 | unsigned int y, |
---|
251 | unsigned int level, |
---|
252 | sqt_node_t * parent, |
---|
253 | unsigned int x_size, |
---|
254 | unsigned int y_size, |
---|
255 | unsigned int nthreads ) |
---|
256 | { |
---|
257 | // get target node address |
---|
258 | sqt_node_t * node = barrier->node[x][y][level]; |
---|
259 | |
---|
260 | if (level == 0 ) // terminal case |
---|
261 | { |
---|
262 | // initializes target node |
---|
263 | node->arity = nthreads; |
---|
264 | node->count = nthreads; |
---|
265 | node->sense = 0; |
---|
266 | node->level = 0; |
---|
267 | node->parent = parent; |
---|
268 | node->child[0] = NULL; |
---|
269 | node->child[1] = NULL; |
---|
270 | node->child[2] = NULL; |
---|
271 | node->child[3] = NULL; |
---|
272 | |
---|
273 | #if PTHREAD_BARRIER_DEBUG |
---|
274 | printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] / arity %d / desc %x\n" |
---|
275 | "parent %x / child0 %x / child1 %x / child2 %x / child3 %x\n", |
---|
276 | __FUNCTION__, x, y, level, node->arity, node, node->parent, |
---|
277 | node->child[0], node->child[1], node->child[2], node->child[3] ); |
---|
278 | #endif |
---|
279 | |
---|
280 | } |
---|
281 | else // non terminal case |
---|
282 | { |
---|
283 | unsigned int cx[4]; // x coordinate for children |
---|
284 | unsigned int cy[4]; // y coordinate for children |
---|
285 | unsigned int arity = 0; |
---|
286 | unsigned int i; |
---|
287 | |
---|
288 | // the child0 coordinates are equal to the parent coordinates |
---|
289 | // other children coordinates are incremented depending on the level value |
---|
290 | cx[0] = x; |
---|
291 | cy[0] = y; |
---|
292 | |
---|
293 | cx[1] = x; |
---|
294 | cy[1] = y + (1 << (level-1)); |
---|
295 | |
---|
296 | cx[2] = x + (1 << (level-1)); |
---|
297 | cy[2] = y; |
---|
298 | |
---|
299 | cx[3] = x + (1 << (level-1)); |
---|
300 | cy[3] = y + (1 << (level-1)); |
---|
301 | |
---|
302 | // initializes parent node taken into account the actual number of childs |
---|
303 | // child pointer is NULL if coordinates outside the mesh |
---|
304 | for ( i = 0 ; i < 4 ; i++ ) |
---|
305 | { |
---|
306 | if ( (cx[i] < x_size) && (cy[i] < y_size) ) |
---|
307 | { |
---|
308 | node->child[i] = barrier->node[cx[i]][cy[i]][level-1]; |
---|
309 | arity++; |
---|
310 | } |
---|
311 | else node->child[i] = NULL; |
---|
312 | } |
---|
313 | node->arity = arity; |
---|
314 | node->count = arity; |
---|
315 | node->sense = 0; |
---|
316 | node->level = level; |
---|
317 | node->parent = parent; |
---|
318 | |
---|
319 | #if PTHREAD_BARRIER_DEBUG |
---|
320 | printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] / arity %d / desc %x\n" |
---|
321 | "parent %x / child0 %x / child1 %x / child2 %x / child3 %x\n", |
---|
322 | __FUNCTION__, x, y, level, node->arity, node, node->parent, |
---|
323 | node->child[0], node->child[1], node->child[2], node->child[3] ); |
---|
324 | #endif |
---|
325 | |
---|
326 | // recursive calls for children nodes |
---|
327 | for ( i = 0 ; i < 4 ; i++ ) |
---|
328 | { |
---|
329 | if ( (cx[i] < x_size) && (cy[i] < y_size) ) |
---|
330 | sqt_barrier_build( barrier, |
---|
331 | cx[i], |
---|
332 | cy[i], |
---|
333 | level-1, |
---|
334 | node, |
---|
335 | x_size, |
---|
336 | y_size, |
---|
337 | nthreads ); |
---|
338 | } |
---|
339 | } |
---|
340 | } // end sqt_barrier_build() |
---|
341 | |
---|
342 | //////////////////////////////////////////////////////////////// |
---|
343 | int pthread_barrier_init( pthread_barrier_t * barrier, |
---|
344 | const pthread_barrierattr_t * attr, |
---|
345 | unsigned int count ) |
---|
346 | { |
---|
347 | unsigned int x_size; |
---|
348 | unsigned int y_size; |
---|
349 | unsigned int nthreads; |
---|
350 | |
---|
351 | if( attr != NULL ) |
---|
352 | { |
---|
353 | x_size = attr->x_size; |
---|
354 | y_size = attr->y_size; |
---|
355 | nthreads = attr->nthreads; |
---|
356 | } |
---|
357 | else |
---|
358 | { |
---|
359 | x_size = 1; |
---|
360 | y_size = 1; |
---|
361 | nthreads = count; |
---|
362 | } |
---|
363 | |
---|
364 | // check attributes |
---|
365 | if( (x_size * y_size * nthreads) != count ) |
---|
366 | { |
---|
367 | printf("\[ERROR] in %s : count != x_size * y_size * nthreads/n", __FUNCTION__); |
---|
368 | exit( EXIT_FAILURE ); |
---|
369 | } |
---|
370 | |
---|
371 | // compute SQT levels |
---|
372 | unsigned int levels; |
---|
373 | unsigned int z = (x_size > y_size) ? x_size : y_size; |
---|
374 | levels = (z < 2) ? 1 : (z < 3) ? 2 : (z < 5) ? 3 : (z < 9) ? 4 : 5; |
---|
375 | |
---|
376 | #if PTHREAD_BARRIER_DEBUG |
---|
377 | unsigned int side = (z < 2) ? 1 : (z < 3) ? 2 : (z < 5) ? 4 : (z < 9) ? 8 : 16; |
---|
378 | printf("\n[BARRIER] %s : x_size = %d / y_size = %d / levels = %d / side = %d\n", |
---|
379 | __FUNCTION__ , x_size , y_size , levels , side ); |
---|
380 | #endif |
---|
381 | |
---|
382 | // allocates memory for the SQT nodes and initializes SQT nodes pointers array |
---|
383 | // the actual number of SQT nodes in a cluster(x,y) depends on (x,y): |
---|
384 | // At least 1 node / at most 5 nodes |
---|
385 | unsigned int x; // x coordinate for one SQT node |
---|
386 | unsigned int y; // y coordinate for one SQT node |
---|
387 | unsigned int l; // level for one SQT node |
---|
388 | for ( x = 0 ; x < x_size ; x++ ) |
---|
389 | { |
---|
390 | for ( y = 0 ; y < y_size ; y++ ) |
---|
391 | { |
---|
392 | unsigned int cxy = (x<<QDT_YWIDTH) + y; |
---|
393 | |
---|
394 | for ( l = 0 ; l < levels ; l++ ) |
---|
395 | { |
---|
396 | if ( ( (l == 0) && ((x&0x00) == 0) && ((y&0x00) == 0) ) || |
---|
397 | ( (l == 1) && ((x&0x01) == 0) && ((y&0x01) == 0) ) || |
---|
398 | ( (l == 2) && ((x&0x03) == 0) && ((y&0x03) == 0) ) || |
---|
399 | ( (l == 3) && ((x&0x07) == 0) && ((y&0x07) == 0) ) || |
---|
400 | ( (l == 4) && ((x&0x0F) == 0) && ((y&0x0F) == 0) ) ) |
---|
401 | { |
---|
402 | sqt_node_t * node = remote_malloc( sizeof(sqt_node_t) , cxy ); |
---|
403 | |
---|
404 | if( node == NULL ) |
---|
405 | { |
---|
406 | printf("\n[ERROR] in %s : cannot allocate sqt_node in cluster %x\n", |
---|
407 | __FUNCTION__ , cxy ); |
---|
408 | return -1; |
---|
409 | } |
---|
410 | |
---|
411 | barrier->node[x][y][l] = node; |
---|
412 | |
---|
413 | } |
---|
414 | } |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | // recursively initialize all SQT nodes from root to bottom |
---|
419 | sqt_barrier_build( barrier, |
---|
420 | 0, |
---|
421 | 0, |
---|
422 | levels-1, |
---|
423 | NULL, |
---|
424 | x_size, |
---|
425 | y_size, |
---|
426 | nthreads ); |
---|
427 | |
---|
428 | hal_user_fence(); |
---|
429 | |
---|
430 | return 0; |
---|
431 | |
---|
432 | } // end pthread_barrier_init |
---|
433 | |
---|
434 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
435 | // This recursive function decrements the distributed "count" variables, |
---|
436 | // traversing the SQT from bottom to root. |
---|
437 | // The last arrived thread reset the local node before returning. |
---|
438 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
439 | static void sqt_barrier_decrement( sqt_node_t * node ) |
---|
440 | { |
---|
441 | |
---|
442 | #if PTHREAD_BARRIER_DEBUG |
---|
443 | unsigned int cxy; |
---|
444 | unsigned int lid; |
---|
445 | get_core( &cxy , &lid ); |
---|
446 | printf("\n[BARRIER] %s : core[%x,%d] decrement SQT barrier node %x :\n" |
---|
447 | " level = %d / parent = %x / arity = %d / sense = %d / count = %d\n", |
---|
448 | __FUNCTION__ , cxy , lid , (unsigned int)node , |
---|
449 | node->level , node->parent, node->arity , node->sense , node->count ); |
---|
450 | #endif |
---|
451 | |
---|
452 | unsigned int expected; |
---|
453 | |
---|
454 | // compute expected sense value |
---|
455 | if ( node->sense == 0) expected = 1; |
---|
456 | else expected = 0; |
---|
457 | |
---|
458 | // atomically decrement count |
---|
459 | int count = hal_user_atomic_add( (int *)&node->count , -1 ); |
---|
460 | |
---|
461 | // last arrived thread makes the recursive call |
---|
462 | if ( count == 1 ) // last thread |
---|
463 | { |
---|
464 | // decrement the parent node if the current node is not the root |
---|
465 | if ( node->parent != NULL ) sqt_barrier_decrement( node->parent ); |
---|
466 | |
---|
467 | #if PTHREAD_BARRIER_DEBUG |
---|
468 | printf("\n[BARRIER] %s : core[%x,%d] reset SQT barrier node %x :\n" |
---|
469 | " level = %d / arity = %d / sense = %d / count = %d\n", |
---|
470 | __FUNCTION__ , cxy , lid , (unsigned int)node , |
---|
471 | node->level , node->arity , node->sense , node->count ); |
---|
472 | #endif |
---|
473 | // reset the current node |
---|
474 | node->sense = expected; |
---|
475 | node->count = node->arity; |
---|
476 | |
---|
477 | return; |
---|
478 | } |
---|
479 | else // not the last thread |
---|
480 | { |
---|
481 | while( 1 ) |
---|
482 | { |
---|
483 | // poll sense |
---|
484 | if( node->sense == expected ) break; |
---|
485 | |
---|
486 | // deschedule |
---|
487 | pthread_yield(); |
---|
488 | } |
---|
489 | |
---|
490 | return; |
---|
491 | } |
---|
492 | } // end sqt_barrier_decrement() |
---|
493 | |
---|
494 | /////////////////////////////////////////////////////// |
---|
495 | int pthread_barrier_wait( pthread_barrier_t * barrier ) |
---|
496 | { |
---|
497 | // get calling core cluster |
---|
498 | unsigned int cxy; |
---|
499 | unsigned int lid; |
---|
500 | get_core( &cxy , &lid ); |
---|
501 | |
---|
502 | // get calling core coordinate |
---|
503 | unsigned int x = cxy >> QDT_YWIDTH; |
---|
504 | unsigned int y = cxy & QDT_YMASK; |
---|
505 | |
---|
506 | #if PTHREAD_BARRIER_DEBUG |
---|
507 | printf("\n[BARRIER] %s : core[%x,%d] enter / barrier = %x / node = %x\n", |
---|
508 | __FUNCTION__ , cxy , lid , barrier, barrier->node[x][y][0] ); |
---|
509 | #endif |
---|
510 | |
---|
511 | // recursively decrement count from bottom to root |
---|
512 | sqt_barrier_decrement( barrier->node[x][y][0] ); |
---|
513 | |
---|
514 | hal_user_fence(); |
---|
515 | |
---|
516 | return 0; |
---|
517 | |
---|
518 | } // end pthread_barrier_wait() |
---|
519 | |
---|
520 | */ |
---|
521 | |
---|
522 | |
---|
523 | |
---|
524 | // Local Variables: |
---|
525 | // tab-width: 4 |
---|
526 | // c-basic-offset: 4 |
---|
527 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
528 | // indent-tabs-mode: nil |
---|
529 | // End: |
---|
530 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
531 | |
---|