Ignore:
Timestamp:
Feb 16, 2023, 4:37:25 PM (18 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
fbdfcd8
Parents:
1c80f20
git-author:
Peter A. Buhr <pabuhr@…> (02/16/23 16:34:37)
git-committer:
Peter A. Buhr <pabuhr@…> (02/16/23 16:37:25)
Message:

make _GNU_SOURCE default, change IO to use SOCKADDR_ARG and CONST_SOCKADDR_ARG, move sys/socket.h to first include because of anonymous naming problem

File:
1 edited

Legend:

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

    r1c80f20 rf5f2768  
    1515
    1616#define __cforall_thread__
    17 #define _GNU_SOURCE
    1817
    1918#include <signal.h>
     
    3534struct pthread_values{
    3635        inline Seqable;
    37         void* value;
     36        void * value;
    3837        bool in_use;
    3938};
     
    5554};
    5655
    57 static void ?{}(pthread_keys& k){
     56static void ?{}(pthread_keys& k) {
    5857        k.threads{};
    5958}
     
    6261static pthread_keys cfa_pthread_keys_storage[PTHREAD_KEYS_MAX] __attribute__((aligned (16)));
    6362
    64 static void init_pthread_storage(){
    65         for (int i = 0; i < PTHREAD_KEYS_MAX; i++){
     63static void init_pthread_storage() {
     64        for ( int i = 0; i < PTHREAD_KEYS_MAX; i++) {
    6665                cfa_pthread_keys_storage[i]{};
    6766        }
     
    9695
    9796/* condvar helper routines */
    98 static void init(pthread_cond_t* pcond){
     97static void init(pthread_cond_t * pcond) {
    9998        static_assert(sizeof(pthread_cond_t) >= sizeof(cfa2pthr_cond_var_t),"sizeof(pthread_t) < sizeof(cfa2pthr_cond_var_t)");
    100         cfa2pthr_cond_var_t* _cond = (cfa2pthr_cond_var_t*)pcond;
     99        cfa2pthr_cond_var_t * _cond = (cfa2pthr_cond_var_t*)pcond;
    101100        ?{}(*_cond);
    102101}
    103102
    104 static cfa2pthr_cond_var_t* get(pthread_cond_t* pcond){
     103static cfa2pthr_cond_var_t * get(pthread_cond_t * pcond) {
    105104        static_assert(sizeof(pthread_cond_t) >= sizeof(cfa2pthr_cond_var_t),"sizeof(pthread_t) < sizeof(cfa2pthr_cond_var_t)");
    106105        return (cfa2pthr_cond_var_t*)pcond;
    107106}
    108107
    109 static void destroy(pthread_cond_t* cond){
     108static void destroy(pthread_cond_t* cond) {
    110109        static_assert(sizeof(pthread_cond_t) >= sizeof(cfa2pthr_cond_var_t),"sizeof(pthread_t) < sizeof(cfa2pthr_cond_var_t)");
    111110        ^?{}(*get(cond));
     
    116115
    117116/* mutex helper routines */
    118 static void mutex_check(pthread_mutex_t* t){
     117static void mutex_check(pthread_mutex_t* t) {
    119118        // Use double check to improve performance.
    120119        // Check is safe on x86; volatile prevents compiler reordering
     
    136135
    137136
    138 static void init(pthread_mutex_t* plock){
     137static void init(pthread_mutex_t* plock) {
    139138        static_assert(sizeof(pthread_mutex_t) >= sizeof(simple_owner_lock),"sizeof(pthread_mutex_t) < sizeof(simple_owner_lock)");
    140139        simple_owner_lock* _lock = (simple_owner_lock*)plock;
     
    142141}
    143142
    144 static simple_owner_lock* get(pthread_mutex_t* plock){
     143static simple_owner_lock* get(pthread_mutex_t* plock) {
    145144        static_assert(sizeof(pthread_mutex_t) >= sizeof(simple_owner_lock),"sizeof(pthread_mutex_t) < sizeof(simple_owner_lock)");
    146145        return (simple_owner_lock*)plock;
    147146}
    148147
    149 static void destroy(pthread_mutex_t* plock){
     148static void destroy(pthread_mutex_t* plock) {
    150149        static_assert(sizeof(pthread_mutex_t) >= sizeof(simple_owner_lock),"sizeof(pthread_mutex_t) < sizeof(simple_owner_lock)");
    151150        ^?{}(*get(plock));
     
    173172};
    174173
    175 static cfaPthread_attr_t* get(const pthread_attr_t* attr){
     174static cfaPthread_attr_t* get(const pthread_attr_t* attr) {
    176175        static_assert(sizeof(pthread_attr_t) >= sizeof(cfaPthread_attr_t),"sizeof(pthread_attr_t) < sizeof(cfaPthread_attr_t)");
    177176        return (cfaPthread_attr_t*)attr;
     
    207206/* thread part routines */
    208207//  cfaPthread entry point
    209 void main(cfaPthread& _thread) with(_thread){
    210         joinval =  start_routine(start_arg);
     208void main( cfaPthread & _thread ) with( _thread ) {
     209        joinval = start_routine( start_arg );
    211210        isTerminated = true;
    212211}
    213212
    214 static cfaPthread *lookup( pthread_t p ){
     213static cfaPthread * lookup( pthread_t p ) {
    215214        static_assert(sizeof(pthread_t) >= sizeof(cfaPthread*),"sizeof(pthread_t) < sizeof(cfaPthread*)");
    216215        return (cfaPthread*)p;
     
    221220        pthread_keys* key;
    222221        bool destcalled = true;
    223         if (values != NULL){
     222        if (values != NULL) {
    224223                for ( int attempts = 0; attempts < PTHREAD_DESTRUCTOR_ITERATIONS && destcalled ; attempts += 1 ) {
    225224                        destcalled = false;
    226225                        lock(key_lock);
    227                         for (int i = 0; i < PTHREAD_KEYS_MAX; i++){
     226                        for ( int i = 0; i < PTHREAD_KEYS_MAX; i++) {
    228227                                // for each valid key
    229                                 if ( values[i].in_use){
     228                                if ( values[i].in_use) {
    230229                                        value = &values[i];
    231230                                        key = &cfa_pthread_keys[i];
     
    234233                                        // if  a  key  value  has  a  non-NULL  destructor pointer,  and  the  thread  has  a  non-NULL  value associated with that key,
    235234                                        // the value of the key is set to NULL, and then the function pointed to is called with the previously associated value as its sole argument.
    236                                         if (value->value != NULL && key->destructor != NULL){
     235                                        if (value->value != NULL && key->destructor != NULL) {
    237236                                                unlock(key_lock);
    238237                                                key->destructor(value->value); // run destructor
     
    249248}
    250249
    251 static void ^?{}(cfaPthread & mutex t){
     250static void ^?{}(cfaPthread & mutex t) {
    252251        // delete pthread local storage
    253252        pthread_values * values = t.pthreadData;
     
    260259        // set up user thread stackSize
    261260        cfaPthread_attr_t * attr = get(_attr);
    262         ((thread&)t){ attr ? attr->stacksize: DEFAULT_STACK_SIZE };
     261        ((thread&)t) { attr ? attr->stacksize: DEFAULT_STACK_SIZE };
    263262
    264263        // initialize _thread & cfaPthread id
     
    365364        int pthread_create(pthread_t * _thread, const pthread_attr_t * attr, void *(*start_routine)(void *), void * arg) libcfa_public __THROW {
    366365                cfaPthread *t = alloc();
    367                 (*t){_thread, attr, start_routine, arg};
     366                (*t) {_thread, attr, start_routine, arg};
    368367                return 0;
    369368        }
     
    415414                _thread->joinval = status;  // set return value
    416415                _thread->isTerminated = 1;  // set terminated flag
    417                 cancel_stack((pthread_exit_exp){&exp_vt});
     416                cancel_stack((pthread_exit_exp) {&exp_vt});
    418417        }   //pthread_exit_
    419418
     
    436435                check_nonnull(_mutex);
    437436                simple_owner_lock* _lock = get(_mutex);
    438                 if (_lock->owner != NULL){
     437                if (_lock->owner != NULL) {
    439438                        return EBUSY;
    440439                }
     
    454453                check_nonnull(_mutex);
    455454                simple_owner_lock* _lock = get(_mutex);
    456                 if (_lock->owner != active_thread()){
     455                if (_lock->owner != active_thread()) {
    457456                        return EPERM;
    458457                } // current thread does not hold the mutex
     
    464463                check_nonnull(_mutex);
    465464                simple_owner_lock* _lock = get(_mutex);
    466                 if (_lock->owner != active_thread() && _lock->owner != NULL){
     465                if (_lock->owner != active_thread() && _lock->owner != NULL) {
    467466                        return EBUSY;
    468467                }   // if mutex is owned
     
    567566                // if current thread's pthreadData is NULL; initialize it
    568567                pthread_values* values;
    569                 if (t->pthreadData == NULL){
     568                if (t->pthreadData == NULL) {
    570569                        values = anew( PTHREAD_KEYS_MAX);
    571570                        t->pthreadData = values;
    572                         for (int i = 0;i < PTHREAD_KEYS_MAX; i++){
     571                        for ( int i = 0;i < PTHREAD_KEYS_MAX; i++) {
    573572                                t->pthreadData[i].in_use = false;
    574573                        }   // for
     
    875874        //######################### Parallelism #########################
    876875
    877         int pthread_setaffinity_np( pthread_t /* __th */, size_t /* __cpusetsize */, __const cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
    878                 abort( "pthread_setaffinity_np" );
    879         } // pthread_setaffinity_np
    880 
    881         int pthread_getaffinity_np( pthread_t /* __th */, size_t /* __cpusetsize */, cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
    882                 abort( "pthread_getaffinity_np" );
    883         } // pthread_getaffinity_np
    884 
    885         int pthread_attr_setaffinity_np( pthread_attr_t * /* __attr */, size_t /* __cpusetsize */, __const cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
    886                 abort( "pthread_attr_setaffinity_np" );
    887         } // pthread_attr_setaffinity_np
    888 
    889         int pthread_attr_getaffinity_np( __const pthread_attr_t * /* __attr */, size_t /* __cpusetsize */, cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
    890                 abort( "pthread_attr_getaffinity_np" );
    891         } // pthread_attr_getaffinity_np
     876        // int pthread_setaffinity_np( pthread_t /* __th */, size_t /* __cpusetsize */, __const cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
     877        //      abort( "pthread_setaffinity_np" );
     878        // } // pthread_setaffinity_np
     879
     880        // int pthread_getaffinity_np( pthread_t /* __th */, size_t /* __cpusetsize */, cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
     881        //      abort( "pthread_getaffinity_np" );
     882        // } // pthread_getaffinity_np
     883
     884        // int pthread_attr_setaffinity_np( pthread_attr_t * /* __attr */, size_t /* __cpusetsize */, __const cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
     885        //      abort( "pthread_attr_setaffinity_np" );
     886        // } // pthread_attr_setaffinity_np
     887
     888        // int pthread_attr_getaffinity_np( __const pthread_attr_t * /* __attr */, size_t /* __cpusetsize */, cpu_set_t * /* __cpuset */ ) libcfa_public __THROW {
     889        //      abort( "pthread_attr_getaffinity_np" );
     890        // } // pthread_attr_getaffinity_np
    892891
    893892        //######################### Cancellation #########################
Note: See TracChangeset for help on using the changeset viewer.