Changeset 3494ca9 for libcfa/src


Ignore:
Timestamp:
Feb 19, 2023, 5:39:56 PM (16 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
c910709
Parents:
ed52dd5
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/pthread.cfa

    red52dd5 r3494ca9  
    5050struct pthread_keys {
    5151        bool in_use;
    52         void (*destructor)( void * );
     52        void (* destructor)( void * );
    5353        Sequence(pthread_values) threads;
    5454};
     
    6262
    6363static void init_pthread_storage() {
    64         for ( int i = 0; i < PTHREAD_KEYS_MAX; i++) {
     64        for ( int i = 0; i < PTHREAD_KEYS_MAX; i++ ) {
    6565                cfa_pthread_keys_storage[i]{};
    6666        }
     
    9797static void init(pthread_cond_t * pcond) {
    9898        static_assert(sizeof(pthread_cond_t) >= sizeof(cfa2pthr_cond_var_t),"sizeof(pthread_t) < sizeof(cfa2pthr_cond_var_t)");
    99         cfa2pthr_cond_var_t * _cond = (cfa2pthr_cond_var_t*)pcond;
     99        cfa2pthr_cond_var_t * _cond = (cfa2pthr_cond_var_t *)pcond;
    100100        ?{}(*_cond);
    101101}
     
    103103static cfa2pthr_cond_var_t * get(pthread_cond_t * pcond) {
    104104        static_assert(sizeof(pthread_cond_t) >= sizeof(cfa2pthr_cond_var_t),"sizeof(pthread_t) < sizeof(cfa2pthr_cond_var_t)");
    105         return (cfa2pthr_cond_var_t*)pcond;
    106 }
    107 
    108 static void destroy(pthread_cond_t* cond) {
     105        return (cfa2pthr_cond_var_t *)pcond;
     106}
     107
     108static void destroy(pthread_cond_t * cond) {
    109109        static_assert(sizeof(pthread_cond_t) >= sizeof(cfa2pthr_cond_var_t),"sizeof(pthread_t) < sizeof(cfa2pthr_cond_var_t)");
    110110        ^?{}(*get(cond));
     
    115115
    116116/* mutex helper routines */
    117 static void mutex_check(pthread_mutex_t* t) {
     117static void mutex_check(pthread_mutex_t * t) {
    118118        // Use double check to improve performance.
    119119        // Check is safe on x86; volatile prevents compiler reordering
    120         volatile pthread_mutex_t *const mutex_ = t;
     120        volatile pthread_mutex_t * const mutex_ = t;
    121121
    122122        // SKULLDUGGERY: not a portable way to access the kind field, /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h
     
    135135
    136136
    137 static void init(pthread_mutex_t* plock) {
     137static void init(pthread_mutex_t * plock) {
    138138        static_assert(sizeof(pthread_mutex_t) >= sizeof(simple_owner_lock),"sizeof(pthread_mutex_t) < sizeof(simple_owner_lock)");
    139         simple_owner_lock* _lock = (simple_owner_lock*)plock;
     139        simple_owner_lock * _lock = (simple_owner_lock *)plock;
    140140        ?{}(*_lock);
    141141}
    142142
    143 static simple_owner_lock* get(pthread_mutex_t* plock) {
     143static simple_owner_lock * get(pthread_mutex_t * plock) {
    144144        static_assert(sizeof(pthread_mutex_t) >= sizeof(simple_owner_lock),"sizeof(pthread_mutex_t) < sizeof(simple_owner_lock)");
    145         return (simple_owner_lock*)plock;
    146 }
    147 
    148 static void destroy(pthread_mutex_t* plock) {
     145        return (simple_owner_lock *)plock;
     146}
     147
     148static void destroy(pthread_mutex_t * plock) {
    149149        static_assert(sizeof(pthread_mutex_t) >= sizeof(simple_owner_lock),"sizeof(pthread_mutex_t) < sizeof(simple_owner_lock)");
    150150        ^?{}(*get(plock));
     
    156156                int detachstate;
    157157                size_t stacksize;
    158                 void *stackaddr;
     158                void * stackaddr;
    159159                int policy;
    160160                int inheritsched;
     
    162162} typedef cfaPthread_attr_t;
    163163
    164 static const cfaPthread_attr_t default_attrs{
     164static const cfaPthread_attr_t default_attrs {
    165165        0,
    166166        0,
    167         (size_t)65000,
    168         (void *)NULL,
     167        65_000,
     168        NULL,
    169169        0,
    170170        0,
     
    172172};
    173173
    174 static cfaPthread_attr_t* get(const pthread_attr_t* attr) {
    175         static_assert(sizeof(pthread_attr_t) >= sizeof(cfaPthread_attr_t),"sizeof(pthread_attr_t) < sizeof(cfaPthread_attr_t)");
    176         return (cfaPthread_attr_t*)attr;
     174static cfaPthread_attr_t * get(const pthread_attr_t * attr) {
     175        static_assert(sizeof(pthread_attr_t) >= sizeof(cfaPthread_attr_t), "sizeof(pthread_attr_t) < sizeof(cfaPthread_attr_t)");
     176        return (cfaPthread_attr_t *)attr;
    177177}
    178178
     
    189189
    190190        // pthreads return value
    191         void *joinval;
     191        void * joinval;
    192192
    193193        // pthread attributes
    194194        pthread_attr_t pthread_attr;
    195195
    196         void *(*start_routine)(void *);
    197         void *start_arg;
     196        void *(* start_routine)(void *);
     197        void * start_arg;
    198198
    199199        // thread local data
    200         pthread_values* pthreadData;
     200        pthread_values * pthreadData;
    201201
    202202        // flag used for tryjoin
     
    206206/* thread part routines */
    207207//  cfaPthread entry point
    208 void main( cfaPthread & _thread ) with( _thread ) {
    209         joinval = start_routine( start_arg );
     208void main(cfaPthread & _thread) with(_thread) {
     209        joinval = start_routine(start_arg);
    210210        isTerminated = true;
    211211}
    212212
    213213static cfaPthread * lookup( pthread_t p ) {
    214         static_assert(sizeof(pthread_t) >= sizeof(cfaPthread*),"sizeof(pthread_t) < sizeof(cfaPthread*)");
    215         return (cfaPthread*)p;
    216 }
    217 
    218 static void pthread_deletespecific_( pthread_values* values )  { // see uMachContext::invokeTask
    219         pthread_values* value;
    220         pthread_keys* key;
     214        static_assert(sizeof(pthread_t) >= sizeof(cfaPthread *),"sizeof(pthread_t) < sizeof(cfaPthread *)");
     215        return (cfaPthread *)p;
     216}
     217
     218static void pthread_deletespecific_( pthread_values * values )  { // see uMachContext::invokeTask
     219        pthread_values * value;
     220        pthread_keys * key;
    221221        bool destcalled = true;
    222222        if (values != NULL) {
     
    224224                        destcalled = false;
    225225                        lock(key_lock);
    226                         for ( int i = 0; i < PTHREAD_KEYS_MAX; i++) {
     226                        for ( int i = 0; i < PTHREAD_KEYS_MAX; i++ ) {
    227227                                // for each valid key
    228228                                if ( values[i].in_use) {
     
    254254}
    255255
    256 static void ?{}(cfaPthread &t, pthread_t* _thread, const pthread_attr_t * _attr,void *(*start_routine)(void *), void * arg) {
    257         static_assert(sizeof(pthread_t) >= sizeof(cfaPthread*), "pthread_t too small to hold a pointer: sizeof(pthread_t) < sizeof(cfaPthread*)");
     256static void ?{}(cfaPthread & t, pthread_t * _thread, const pthread_attr_t * _attr,void *(* start_routine)(void *), void * arg) {
     257        static_assert(sizeof(pthread_t) >= sizeof(cfaPthread *), "pthread_t too small to hold a pointer: sizeof(pthread_t) < sizeof(cfaPthread *)");
    258258
    259259        // set up user thread stackSize
    260260        cfaPthread_attr_t * attr = get(_attr);
    261         ((thread&)t) { attr ? attr->stacksize: DEFAULT_STACK_SIZE };
     261        ((thread&)t){ attr ? attr->stacksize: DEFAULT_STACK_SIZE };
    262262
    263263        // initialize _thread & cfaPthread id
     
    277277        //######################### Pthread Attrs #########################
    278278
    279         int pthread_attr_init(pthread_attr_t *attr) libcfa_public __THROW {
    280                 cfaPthread_attr_t* _attr = get(attr);
     279        int pthread_attr_init(pthread_attr_t * attr) libcfa_public __THROW {
     280                cfaPthread_attr_t * _attr = get(attr);
    281281                ?{}(*_attr, default_attrs);
    282282                return 0;
    283283        }
    284         int pthread_attr_destroy(pthread_attr_t *attr) libcfa_public __THROW {
     284        int pthread_attr_destroy(pthread_attr_t * attr) libcfa_public __THROW {
    285285                ^?{}(*get(attr));
    286286                return 0;
    287287        }
    288288
    289         int pthread_attr_setscope( pthread_attr_t *attr, int contentionscope ) libcfa_public __THROW {
     289        int pthread_attr_setscope( pthread_attr_t * attr, int contentionscope ) libcfa_public __THROW {
    290290                get( attr )->contentionscope = contentionscope;
    291291                return 0;
    292292        } // pthread_attr_setscope
    293293
    294         int pthread_attr_getscope( const pthread_attr_t *attr, int *contentionscope ) libcfa_public __THROW {
     294        int pthread_attr_getscope( const pthread_attr_t * attr, int * contentionscope ) libcfa_public __THROW {
    295295                *contentionscope = get( attr )->contentionscope;
    296296                return 0;
    297297        } // pthread_attr_getscope
    298298
    299         int pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate ) libcfa_public __THROW {
     299        int pthread_attr_setdetachstate( pthread_attr_t * attr, int detachstate ) libcfa_public __THROW {
    300300                get( attr )->detachstate = detachstate;
    301301                return 0;
    302302        } // pthread_attr_setdetachstate
    303303
    304         int pthread_attr_getdetachstate( const pthread_attr_t *attr, int *detachstate ) libcfa_public __THROW {
     304        int pthread_attr_getdetachstate( const pthread_attr_t * attr, int * detachstate ) libcfa_public __THROW {
    305305                *detachstate = get( attr )->detachstate;
    306306                return 0;
    307307        } // pthread_attr_getdetachstate
    308308
    309         int pthread_attr_setstacksize( pthread_attr_t *attr, size_t stacksize ) libcfa_public __THROW {
     309        int pthread_attr_setstacksize( pthread_attr_t * attr, size_t stacksize ) libcfa_public __THROW {
    310310                get( attr )->stacksize = stacksize;
    311311                return 0;
    312312        } // pthread_attr_setstacksize
    313313
    314         int pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ) libcfa_public __THROW {
     314        int pthread_attr_getstacksize( const pthread_attr_t * attr, size_t * stacksize ) libcfa_public __THROW {
    315315                *stacksize = get( attr )->stacksize;
    316316                return 0;
     
    325325        } // pthread_attr_setguardsize
    326326
    327         int pthread_attr_setstackaddr( pthread_attr_t *attr, void *stackaddr ) libcfa_public __THROW {
     327        int pthread_attr_setstackaddr( pthread_attr_t * attr, void * stackaddr ) libcfa_public __THROW {
    328328                get( attr )->stackaddr = stackaddr;
    329329                return 0;
    330330        } // pthread_attr_setstackaddr
    331331
    332         int pthread_attr_getstackaddr( const pthread_attr_t *attr, void **stackaddr ) libcfa_public __THROW {
     332        int pthread_attr_getstackaddr( const pthread_attr_t * attr, void ** stackaddr ) libcfa_public __THROW {
    333333                *stackaddr = get( attr )->stackaddr;
    334334                return 0;
    335335        } // pthread_attr_getstackaddr
    336336
    337         int pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ) libcfa_public __THROW {
     337        int pthread_attr_setstack( pthread_attr_t * attr, void * stackaddr, size_t stacksize ) libcfa_public __THROW {
    338338                get( attr )->stackaddr = stackaddr;
    339339                get( attr )->stacksize = stacksize;
     
    341341        } // pthread_attr_setstack
    342342
    343         int pthread_attr_getstack( const pthread_attr_t *attr, void **stackaddr, size_t *stacksize ) libcfa_public __THROW {
     343        int pthread_attr_getstack( const pthread_attr_t * attr, void ** stackaddr, size_t * stacksize ) libcfa_public __THROW {
    344344                *stackaddr = get( attr )->stackaddr;
    345345                *stacksize = get( attr )->stacksize;
     
    350350        // already running thread threadID. It shall be called on unitialized attr
    351351        // and destroyed with pthread_attr_destroy when no longer needed.
    352         int pthread_getattr_np( pthread_t threadID, pthread_attr_t *attr ) libcfa_public __THROW { // GNU extension
     352        int pthread_getattr_np( pthread_t threadID, pthread_attr_t * attr ) libcfa_public __THROW { // GNU extension
    353353                check_nonnull(attr);
    354354
     
    362362        //######################### Threads #########################
    363363
    364         int pthread_create(pthread_t * _thread, const pthread_attr_t * attr, void *(*start_routine)(void *), void * arg) libcfa_public __THROW {
    365                 cfaPthread *t = alloc();
    366                 (*t) {_thread, attr, start_routine, arg};
    367                 return 0;
    368         }
    369 
    370 
    371         int pthread_join(pthread_t _thread, void **value_ptr) libcfa_public __THROW {
     364        int pthread_create(pthread_t * _thread, const pthread_attr_t * attr, void *(* start_routine)(void *), void * arg) libcfa_public __THROW {
     365                cfaPthread * t = alloc();
     366                (*t){_thread, attr, start_routine, arg};
     367                return 0;
     368        }
     369
     370        int pthread_join(pthread_t _thread, void ** value_ptr) libcfa_public __THROW {
    372371                // if thread is invalid
    373372                if (_thread == NULL) return EINVAL;
     
    375374
    376375                // get user thr pointer
    377                 cfaPthread* p = lookup(_thread);
     376                cfaPthread * p = lookup(_thread);
    378377                try {
    379378                        join(*p);
     
    388387        }
    389388
    390         int pthread_tryjoin_np(pthread_t _thread, void **value_ptr) libcfa_public __THROW {
     389        int pthread_tryjoin_np(pthread_t _thread, void ** value_ptr) libcfa_public __THROW {
    391390                // if thread is invalid
    392391                if (_thread == NULL) return EINVAL;
    393392                if (_thread == pthread_self()) return EDEADLK;
    394393
    395                 cfaPthread* p = lookup(_thread);
     394                cfaPthread * p = lookup(_thread);
    396395
    397396                // thread not finished ?
     
    411410        void pthread_exit(void * status) libcfa_public __THROW {
    412411                pthread_t pid = pthread_self();
    413                 cfaPthread* _thread = (cfaPthread*)pid;
     412                cfaPthread * _thread = (cfaPthread *)pid;
    414413                _thread->joinval = status;  // set return value
    415414                _thread->isTerminated = 1;  // set terminated flag
    416                 cancel_stack((pthread_exit_exp) {&exp_vt});
     415                cancel_stack((pthread_exit_exp){&exp_vt});
    417416        }   //pthread_exit_
    418417
     
    425424        //######################### Mutex #########################
    426425
    427         int pthread_mutex_init(pthread_mutex_t *_mutex, const pthread_mutexattr_t *attr) libcfa_public __THROW {
     426        int pthread_mutex_init(pthread_mutex_t *_mutex, const pthread_mutexattr_t * attr) libcfa_public __THROW {
    428427                check_nonnull(_mutex);
    429428                init(_mutex);
     
    434433        int pthread_mutex_destroy(pthread_mutex_t *_mutex) libcfa_public __THROW {
    435434                check_nonnull(_mutex);
    436                 simple_owner_lock* _lock = get(_mutex);
     435                simple_owner_lock * _lock = get(_mutex);
    437436                if (_lock->owner != NULL) {
    438437                        return EBUSY;
     
    445444                check_nonnull(_mutex);
    446445                mutex_check(_mutex);
    447                 simple_owner_lock* _lock = get(_mutex);
     446                simple_owner_lock * _lock = get(_mutex);
    448447                lock(*_lock);
    449448                return 0;
     
    452451        int pthread_mutex_unlock(pthread_mutex_t *_mutex) libcfa_public __THROW {
    453452                check_nonnull(_mutex);
    454                 simple_owner_lock* _lock = get(_mutex);
     453                simple_owner_lock * _lock = get(_mutex);
    455454                if (_lock->owner != active_thread()) {
    456455                        return EPERM;
     
    462461        int pthread_mutex_trylock(pthread_mutex_t *_mutex) libcfa_public __THROW {
    463462                check_nonnull(_mutex);
    464                 simple_owner_lock* _lock = get(_mutex);
     463                simple_owner_lock * _lock = get(_mutex);
    465464                if (_lock->owner != active_thread() && _lock->owner != NULL) {
    466465                        return EBUSY;
     
    473472
    474473        /* conditional variable routines */
    475         int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) libcfa_public __THROW {
     474        int pthread_cond_init(pthread_cond_t * cond, const pthread_condattr_t * attr) libcfa_public __THROW {
    476475                check_nonnull(cond);
    477476                init(cond);
     
    479478        }  //pthread_cond_init
    480479
    481         int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *_mutex) libcfa_public __THROW {
     480        int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t *_mutex) libcfa_public __THROW {
    482481                check_nonnull(_mutex);
    483482                check_nonnull(cond);
     
    493492        } // pthread_cond_timedwait
    494493
    495         int pthread_cond_signal(pthread_cond_t *cond) libcfa_public __THROW {
     494        int pthread_cond_signal(pthread_cond_t * cond) libcfa_public __THROW {
    496495                check_nonnull(cond);
    497496                return notify_one(*get(cond));
    498497        } // pthread_cond_signal
    499498
    500         int pthread_cond_broadcast(pthread_cond_t *cond) libcfa_public __THROW {
     499        int pthread_cond_broadcast(pthread_cond_t * cond) libcfa_public __THROW {
    501500                check_nonnull(cond);
    502501                return notify_all(*get(cond));
    503502        } // pthread_cond_broadcast
    504503
    505         int pthread_cond_destroy(pthread_cond_t *cond) libcfa_public __THROW {
     504        int pthread_cond_destroy(pthread_cond_t * cond) libcfa_public __THROW {
    506505                check_nonnull(cond);
    507506                destroy(cond);
     
    513512        //######################### Local storage #########################
    514513
    515         int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) libcfa_public __THROW {
     514        int pthread_once(pthread_once_t * once_control, void (* init_routine)(void)) libcfa_public __THROW {
    516515                static_assert(sizeof(pthread_once_t) >= sizeof(int),"sizeof(pthread_once_t) < sizeof(int)");
    517516                check_nonnull(once_control);
     
    526525        } // pthread_once
    527526
    528         int pthread_key_create( pthread_key_t *key, void (*destructor)( void * ) ) libcfa_public __THROW {
     527        int pthread_key_create( pthread_key_t * key, void (* destructor)( void * ) ) libcfa_public __THROW {
    529528                lock(key_lock);
    530529                for ( int i = 0; i < PTHREAD_KEYS_MAX; i += 1 ) {
     
    561560        }   // pthread_key_delete
    562561
    563         int pthread_setspecific( pthread_key_t key, const void *value ) libcfa_public __THROW {
     562        int pthread_setspecific( pthread_key_t key, const void * value ) libcfa_public __THROW {
    564563                // get current thread
    565                 cfaPthread* t = lookup(pthread_self());
     564                cfaPthread * t = lookup(pthread_self());
    566565                // if current thread's pthreadData is NULL; initialize it
    567                 pthread_values* values;
     566                pthread_values * values;
    568567                if (t->pthreadData == NULL) {
    569568                        values = anew( PTHREAD_KEYS_MAX);
    570569                        t->pthreadData = values;
    571                         for ( int i = 0;i < PTHREAD_KEYS_MAX; i++) {
     570                        for ( int i = 0;i < PTHREAD_KEYS_MAX; i++ ) {
    572571                                t->pthreadData[i].in_use = false;
    573572                        }   // for
     
    592591        } //pthread_setspecific
    593592
    594         void* pthread_getspecific(pthread_key_t key) libcfa_public __THROW {
     593        void * pthread_getspecific(pthread_key_t key) libcfa_public __THROW {
    595594                if (key >= PTHREAD_KEYS_MAX || ! cfa_pthread_keys[key].in_use) return NULL;
    596595
    597596                // get current thread
    598                 cfaPthread* t = lookup(pthread_self());
     597                cfaPthread * t = lookup(pthread_self());
    599598                if (t->pthreadData == NULL) return NULL;
    600599                lock(key_lock);
     
    604603                        return NULL;
    605604                } // if
    606                 void *value = entry.value;
     605                void * value = entry.value;
    607606                unlock(key_lock);
    608607
     
    905904        } // pthread_cancel
    906905
    907         int pthread_setcancelstate( int state, int *oldstate ) libcfa_public __THROW {
     906        int pthread_setcancelstate( int state, int * oldstate ) libcfa_public __THROW {
    908907                abort("pthread_setcancelstate not implemented");
    909908                return 0;
    910909        } // pthread_setcancelstate
    911910
    912         int pthread_setcanceltype( int type, int *oldtype ) libcfa_public __THROW {
     911        int pthread_setcanceltype( int type, int * oldtype ) libcfa_public __THROW {
    913912                abort("pthread_setcanceltype not implemented");
    914913                return 0;
     
    917916
    918917#pragma GCC diagnostic pop
    919 
Note: See TracChangeset for help on using the changeset viewer.