source: trunk/libs/mini-libpthread/pthread.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 14.6 KB
RevLine 
[439]1/*
2 * pthread.c - User side pthread related functions implementation.
3 *
[440]4 * Author     Alain Greiner (2016,2017,2018)
[439]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 <stdio.h>
26#include <stdlib.h>
27#include <pthread.h>
[444]28#include <assert.h>
29#include <almos-mkh.h>
30#include <syscalls_numbers.h>
[439]31
32#define PTHREAD_MUTEX_DEBUG     0
33#define PTHREAD_BARRIER_DEBUG   0
34
35#define  reg_t     int
36
37////////////////////////////////////////////////////////////////////////////////////////////
38//                  Threads
39////////////////////////////////////////////////////////////////////////////////////////////
40
41/////////////////////////////////////////////////
42int pthread_create( pthread_t            * trdid,
43                    const pthread_attr_t * attr,
44                    void                 * start_func,
45                    void                 * start_args )
46{
47    return hal_user_syscall( SYS_THREAD_CREATE,
48                             (reg_t)trdid,
49                             (reg_t)attr,
50                             (reg_t)start_func,
51                             (reg_t)start_args );
52}
53
54/////////////////////////////////////
55int pthread_join( pthread_t    trdid,
56                  void      ** exit_value )
57{
58    return hal_user_syscall( SYS_THREAD_JOIN,
59                             (reg_t)exit_value, 0, 0, 0 );
60}
61
62///////////////////////////////////////
63int pthread_detach( pthread_t   trdid )
64{
65    return hal_user_syscall( SYS_THREAD_DETACH,
66                             (reg_t)trdid, 0, 0, 0 );
67}
68
69/////////////////////////////////////
70int pthread_exit( void * exit_value )
71{
72    return hal_user_syscall( SYS_THREAD_EXIT,
73                             (reg_t)exit_value, 0, 0, 0 );
74}
75
76///////////////////
77int pthread_yield()
78{
79    return hal_user_syscall( SYS_THREAD_YIELD, 0, 0, 0, 0 );
80}
81
82////////////////////////////////////////////////////////////////////////////////////////////
83//                            Barriers
84////////////////////////////////////////////////////////////////////////////////////////////
85
86////////////////////////////////////////////////////////////////////////////////////////////
87// This recursive function initializes the SQT nodes
88// traversing the SQT from root to bottom
89////////////////////////////////////////////////////////////////////////////////////////////
90static void sqt_barrier_build( pthread_barrier_t  * barrier, 
91                               unsigned int         x,
92                               unsigned int         y,
93                               unsigned int         level,
94                               sqt_node_t         * parent,
95                               unsigned int         x_size,
96                               unsigned int         y_size,
97                               unsigned int         nthreads ) 
98{
99    // get target node address
100    sqt_node_t * node = barrier->node[x][y][level];
101   
102    if (level == 0 )        // terminal case
103    {
104        // initializes target node
105        node->arity    = nthreads;   
106        node->count    = nthreads;   
107        node->sense    = 0;   
108        node->level    = 0;   
109        node->parent   = parent;
110        node->child[0] = NULL;
111        node->child[1] = NULL;
112        node->child[2] = NULL;
113        node->child[3] = NULL;
114
115#if PTHREAD_BARRIER_DEBUG
[440]116printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] / arity %d / desc %x\n"
117"parent %x / child0 %x / child1 %x / child2 %x / child3 %x\n", 
118__FUNCTION__, x, y, level, node->arity, node, node->parent, 
[439]119node->child[0], node->child[1], node->child[2], node->child[3] );
120#endif
121
122    }
123    else                   // non terminal case
124    {
125        unsigned int cx[4];   // x coordinate for children
126        unsigned int cy[4];   // y coordinate for children
127        unsigned int arity = 0;
128        unsigned int i;
129
130        // the child0 coordinates are equal to the parent coordinates
131        // other children coordinates are incremented depending on the level value
132        cx[0] = x;
133        cy[0] = y;
134
135        cx[1] = x;
136        cy[1] = y + (1 << (level-1));
137
138        cx[2] = x + (1 << (level-1));
139        cy[2] = y;
140
141        cx[3] = x + (1 << (level-1));
142        cy[3] = y + (1 << (level-1));
143
144        // initializes parent node taken into account the actual number of childs
145        // child pointer is NULL if coordinates outside the mesh
146        for ( i = 0 ; i < 4 ; i++ )
147        {
148            if ( (cx[i] < x_size) && (cy[i] < y_size) ) 
149            {
150                node->child[i] = barrier->node[cx[i]][cy[i]][level-1];
151                arity++;
152            }
153            else  node->child[i] = NULL;
154        }
155        node->arity    = arity; 
156        node->count    = arity;
157        node->sense    = 0;
158        node->level    = level;
159        node->parent   = parent;
160
161#if PTHREAD_BARRIER_DEBUG
[440]162printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] / arity %d / desc %x\n"
163"parent %x / child0 %x / child1 %x / child2 %x / child3 %x\n", 
164__FUNCTION__, x, y, level, node->arity, node, node->parent,
[439]165node->child[0], node->child[1], node->child[2], node->child[3] );
166#endif
167
168        // recursive calls for children nodes
169        for ( i = 0 ; i < 4 ; i++ )
170        {
171            if ( (cx[i] < x_size) && (cy[i] < y_size) ) 
172            sqt_barrier_build( barrier, 
173                               cx[i], 
174                               cy[i], 
175                               level-1, 
176                               node, 
177                               x_size,
178                               y_size,
179                               nthreads );
180        }
181    }
182}  // end sqt_barrier_build()
183
184////////////////////////////////////////////////////////////////
185int pthread_barrier_init( pthread_barrier_t           * barrier,
186                          const pthread_barrierattr_t * attr,
187                          unsigned int                  count )
188{
189    unsigned int x_size;
190    unsigned int y_size;
191    unsigned int nthreads;
192
193    if( attr != NULL )
194    {
195        x_size   = attr->x_size;
196        y_size   = attr->y_size;
197        nthreads = attr->nthreads;
198    }
199    else
200    {
201        x_size   = 1;
202        y_size   = 1;
203        nthreads = count;
204    }
205
206    // check attributes
207    assert( x_size <= QDT_XMAX );
208    assert( y_size <= QDT_YMAX );
209    assert( x_size * y_size * nthreads == count );
210   
211    // compute SQT levels
212    unsigned int levels; 
213    unsigned int z = (x_size > y_size) ? x_size : y_size;
214    levels = (z < 2) ? 1 : (z < 3) ? 2 : (z < 5) ? 3 : (z < 9) ? 4 : 5;
215
216#if PTHREAD_BARRIER_DEBUG
217unsigned int side = (z < 2) ? 1 : (z < 3) ? 2 : (z < 5) ? 4 : (z < 9) ? 8 : 16;
218printf("\n[BARRIER] %s : x_size = %d / y_size = %d / levels = %d / side = %d\n",
219__FUNCTION__ , x_size , y_size , levels , side );
220#endif
221
222    // allocates memory for the SQT nodes and initializes SQT nodes pointers array
223    // the actual number of SQT nodes in a cluster(x,y) depends on (x,y):
224    // At least 1 node / at most 5 nodes
225    unsigned int x;          // x coordinate for one SQT node
226    unsigned int y;          // y coordinate for one SQT node
227    unsigned int l;          // level for one SQT node
228    for ( x = 0 ; x < x_size ; x++ )
229    {
230        for ( y = 0 ; y < y_size ; y++ )
231        {
232            unsigned int cxy = (x<<QDT_YWIDTH) + y;
233               
234            for ( l = 0 ; l < levels ; l++ )         
235            {
236                if ( ( (l == 0) && ((x&0x00) == 0) && ((y&0x00) == 0) ) ||
237                     ( (l == 1) && ((x&0x01) == 0) && ((y&0x01) == 0) ) ||
238                     ( (l == 2) && ((x&0x03) == 0) && ((y&0x03) == 0) ) ||
239                     ( (l == 3) && ((x&0x07) == 0) && ((y&0x07) == 0) ) ||
240                     ( (l == 4) && ((x&0x0F) == 0) && ((y&0x0F) == 0) ) )
241                 {
242                     sqt_node_t * node = remote_malloc( sizeof(sqt_node_t) , cxy ); 
243
244                     if( node == NULL )
245                     {
246                         printf("\n[ERROR] in %s : cannot allocate sqt_node in cluster %x\n",
247                         __FUNCTION__ , cxy );
248                         return -1;
249                     }
250
251                     barrier->node[x][y][l] = node;
252
253                 }
254            }
255        }
256    }
257           
258    // recursively initialize all SQT nodes from root to bottom
259    sqt_barrier_build( barrier,
260                       0,       
261                       0,
262                       levels-1,
263                       NULL,
264                       x_size,
265                       y_size,
266                       nthreads );
267
268    hal_user_fence();
269
270    return 0;
271
272}  // end pthread_barrier_init
273
274//////////////////////////////////////////////////////////////////////////////////////////
275// This recursive function decrements the distributed "count" variables,
276// traversing the SQT from bottom to root.
277// The last arrived thread reset the local node before returning.
278//////////////////////////////////////////////////////////////////////////////////////////
279static void sqt_barrier_decrement( sqt_node_t * node )
280{
281
282#if PTHREAD_BARRIER_DEBUG
283unsigned int    cxy;
284unsigned int    lid;
285get_core( &cxy , &lid );
286printf("\n[BARRIER] %s : core[%x,%d] decrement SQT barrier node %x :\n"
287" level = %d / parent = %x / arity = %d / sense = %d / count = %d\n",
288__FUNCTION__ , cxy , lid , (unsigned int)node , 
289node->level , node->parent, node->arity , node->sense , node->count );
290#endif
291
292    unsigned int expected;
293   
294    // compute expected sense value
295    if ( node->sense == 0) expected = 1;
296    else                   expected = 0;
297
298    // atomically decrement count
299    int count = hal_user_atomic_add( (int *)&node->count , -1 );
300
301    // last arrived thread makes the recursive call
302    if ( count == 1 )                                     // last thread 
303    {
304        // decrement the parent node if the current node is not the root
305        if ( node->parent != NULL )  sqt_barrier_decrement( node->parent );
306
307        // reset the current node
308        node->sense = expected;
309        node->count = node->arity;
310
311#if PTHREAD_BARRIER_DEBUG
312printf("\n[BARRIER] %s : core[%x,%d] reset SQT barrier node %x :\n"
313" level = %d / arity = %d / sense = %d / count = %d\n",
314__FUNCTION__ , cxy , lid , (unsigned int)node , 
315node->level , node->arity , node->sense , node->count );
316#endif
317        return;
318    }
319    else                                               // not the last thread
320    {
321        // poll sense
322        while( 1 )
323        {
324            if( node->sense == expected ) break;
325        }
326
327        return;
328    }
329} // end sqt_barrier_decrement()
330   
331///////////////////////////////////////////////////////
332int pthread_barrier_wait( pthread_barrier_t * barrier )
333{
334    // get calling core cluster
335    unsigned int    cxy;
336    unsigned int    lid;
337    get_core( &cxy , &lid );
338
339    // get calling core coordinate
340    unsigned int    x = cxy >> QDT_YWIDTH;
341    unsigned int    y = cxy &  QDT_YMASK;
342
343#if PTHREAD_BARRIER_DEBUG
344printf("\n[BARRIER] %s : enter for core[%x,%d] / barrier = %x / node = %x\n", 
345__FUNCTION__ , cxy , lid , barrier, barrier->node[x][y][0] );
346#endif
347
348    // recursively decrement count from bottom to root
349    sqt_barrier_decrement( barrier->node[x][y][0] );
350
351    hal_user_fence();
352
353    return 0;
354
355}  // end pthread_barrier_wait()
356
357////////////////////////////////////////////////////////////////////////////////////////////
358//                               Mutexes
359////////////////////////////////////////////////////////////////////////////////////////////
360
361//////////////////////////////////////////////////////////
362int pthread_mutex_init( pthread_mutex_t           * mutex,
363                        const pthread_mutexattr_t * attr )
364{
365    if( attr != NULL )
366    {
367        printf("\n[ERROR] in %s : <attr> argument not supported\n", __FUNCTION__);
368        return -1;
369    }
370
371    mutex->current = 0;
372    mutex->free    = 0;
373
374#if PTHEAD_MUTEX_DEBUG
375unsigned int cxy;
376unsigned int lid;
377get_core( &cxy , &lid );
378printf("\n[MUTEX DEBUG] %s : core[%x,%d] initializes mutex %x\n",
379__FUNCTION__, cxy, lid, mutex );
380#endif
381
382    return 0;
383}
384
385/////////////////////////////////////////////////
386int pthread_mutex_lock( pthread_mutex_t * mutex ) 
387{
388    unsigned int          ticket;
389
390    // get next free ticket
391    ticket = (unsigned int)hal_user_atomic_add( (int *)&mutex->free, 1 );
392
393#if PTHREAD_MUTEX_DEBUG
394unsigned int cxy;
395unsigned int lid;
396get_core( &cxy , &lid );
397printf("\n[MUTEX DEBUG] %s : core[%x,%d] get ticket %d\n",
398" / mutex = %x / current = %d / free = %d\n",
399__FUNCTION__, cxy, lid, ticket, mutex, mutex->current, mutex->free );
400#endif
401
402    // poll the current index
403    while( 1 )
404    {
405        if( mutex->current == ticket) break;
406    }
407               
408#if PTHREAD_MUTEX_DEBUG
409printf("\n[MUTEX DEBUG] %s : core[%x,%d] get mutex %x / current = %d / free = %d\n",
410__FUNCTION__, cxy, lid, mutex, mutex->current, mutex->free );
411#endif
412
413    return 0;
414}
415
416////////////////////////////////////////////////////
417int pthread_mutex_trylock( pthread_mutex_t * mutex )
418{
419    unsigned int          ticket;
420
421    // get next free ticket
422    ticket = (unsigned int)hal_user_atomic_add( (int *)&mutex->free, 1 );
423
424#if PTHREAD_MUTEX_DEBUG
425unsigned int    cxy;
426unsigned int    lid;
427get_core( &cxy, &lid );
428printf("\n[MUTEX DEBUG] %s : core[%x,%d] get ticket = %d"
429" / mutex = %x / current = %d / free = %d\n",
430__FUNCTION__, cxy, lid, ticket, mutex, mutex->current, mutex->free );
431#endif
432
433    // test ticket
434    if( ticket == mutex->current ) return 0;       // success
435    else                           return -1;      // failure
436}
437   
438///////////////////////////////////////////////////
439int pthread_mutex_unlock( pthread_mutex_t * mutex )
440{
441    hal_user_fence();
442
443    mutex->current = mutex->current + 1;
444
445#if PTHREAD_MUTEX_DEBUG
446unsigned int    cxy;
447unsigned int    lid;
448get_core( &cxy , &lid );
449printf("\n[MUTEX_DEBUG] %s : core[%x,%d] releases mutex %x"
450" / current = %d / free = %d\n",
451__FUNCTION__, cxy, lid, mutex, mutex->current, mutex->free );
452#endif
453
454    return 0;
455}
456
457
458// Local Variables:
459// tab-width: 4
460// c-basic-offset: 4
461// c-file-offsets:((innamespace . 0)(inline-open . 0))
462// indent-tabs-mode: nil
463// End:
464// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
465
Note: See TracBrowser for help on using the repository browser.