Changeset a7d696f for libcfa/src


Ignore:
Timestamp:
Aug 3, 2022, 6:32:06 PM (2 years ago)
Author:
z277zhu <z277zhu@…>
Branches:
ADT, ast-experimental, master, pthread-emulation
Children:
20be782
Parents:
80d16f8
git-author:
z277zhu <z277zhu@…> (08/03/22 18:24:16)
git-committer:
z277zhu <z277zhu@…> (08/03/22 18:32:06)
Message:

added pthread symbol interpose

Signed-off-by: z277zhu <z277zhu@…>

Location:
libcfa/src
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/Makefile.am

    r80d16f8 ra7d696f  
    145145        concurrency/stats.cfa \
    146146        concurrency/stats.hfa \
    147         concurrency/stats.hfa
     147        concurrency/stats.hfa \
     148        concurrency/pthread.cfa
    148149
    149150else
  • libcfa/src/bits/defs.hfa

    r80d16f8 ra7d696f  
    2121#include <stdint.h>
    2222#include <assert.h>
     23#include <pthread.h>
    2324
    2425#define likely(x)   __builtin_expect(!!(x), 1)
     
    4546#endif
    4647void __cabi_abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
     48int real_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
     49int real_pthread_join(pthread_t _thread, void **retval);
     50pthread_t real_pthread_self(void);
     51int real_pthread_mutex_init(pthread_mutex_t *_mutex, const pthread_mutexattr_t *attr);
     52int real_pthread_mutex_destroy(pthread_mutex_t *_mutex);
     53int real_pthread_mutex_lock(pthread_mutex_t *_mutex);
     54int real_pthread_mutex_unlock(pthread_mutex_t *_mutex);
     55int real_pthread_mutex_trylock(pthread_mutex_t *_mutex);
     56int real_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
     57int real_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *_mutex);
     58int real_pthread_cond_signal(pthread_cond_t *cond);
     59int real_pthread_cond_broadcast(pthread_cond_t *cond);
     60int real_pthread_cond_destroy(pthread_cond_t *cond);
     61int real_pthread_attr_init(pthread_attr_t *attr);
     62int real_pthread_attr_destroy(pthread_attr_t *attr);
     63int real_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize );
     64int real_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize );
    4765#ifdef __cforall
    4866}
  • libcfa/src/concurrency/clib/cfathread.cfa

    r80d16f8 ra7d696f  
    172172
    173173                pthread_attr_t attr;
    174                 if (int ret = pthread_attr_init(&attr); 0 != ret) {
     174                if (int ret = real_pthread_attr_init(&attr); 0 != ret) {
    175175                        abort | "failed to create master epoll thread attr: " | ret | strerror(ret);
    176176                }
    177177
    178                 if (int ret = pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) {
     178                if (int ret = real_pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) {
    179179                        abort | "failed to create master epoll thread: " | ret | strerror(ret);
    180180                }
  • libcfa/src/concurrency/kernel/startup.cfa

    r80d16f8 ra7d696f  
    218218                ( this.runner ){};
    219219                init( this, "Main Processor", *mainCluster, 0p );
    220                 kernel_thread = pthread_self();
     220                kernel_thread = real_pthread_self();
    221221
    222222                runner{ &this };
     
    769769        pthread_attr_t attr;
    770770
    771         check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     771        check( real_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    772772
    773773        size_t stacksize = max( PTHREAD_STACK_MIN, DEFAULT_STACK_SIZE );
     
    796796        #endif
    797797
    798         check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
    799         check( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     798        check( real_pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     799        check( real_pthread_create( pthread, &attr, start, arg ), "pthread_create" );
    800800        return stack;
    801801}
    802802
    803803void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
    804         int err = pthread_join( pthread, retval );
     804        int err = real_pthread_join( pthread, retval );
    805805        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
    806806
     
    808808                pthread_attr_t attr;
    809809
    810                 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     810                check( real_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    811811
    812812                size_t stacksize;
    813813                // default stack size, normally defined by shell limit
    814                 check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     814                check( real_pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
    815815                assert( stacksize >= PTHREAD_STACK_MIN );
    816816                stacksize += __page_size;
  • libcfa/src/interpose.cfa

    r80d16f8 ra7d696f  
    4646
    4747        static void * library;
     48        static void * pthread_library;
    4849        if ( ! library ) {
    4950                #if defined( RTLD_NEXT )
     
    5859                #endif
    5960        } // if
     61        if ( ! pthread_library ) {
     62                #if defined( RTLD_NEXT )
     63                        pthread_library = RTLD_NEXT;
     64                #else
     65                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
     66                        pthread_library = dlopen( "libpthread.so", RTLD_LAZY );
     67                        error = dlerror();
     68                        if ( error ) {
     69                                abort( "interpose_symbol : failed to open libpthread, %s\n", error );
     70                        }
     71                #endif
     72        } // if
    6073
    6174        union { generic_fptr_t fptr; void * ptr; } originalFunc;
     
    7285
    7386        error = dlerror();
    74         if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
     87        if ( error ) {
     88                originalFunc.ptr = dlsym( pthread_library, symbol );
     89                error = dlerror();
     90                if (error){
     91                        abort( "interpose_symbol : internal error, %s\n", error );
     92                }       // if
     93        }       // if
    7594
    7695        return originalFunc.fptr;
     
    92111        void (* exit)( int ) __attribute__(( __noreturn__ ));
    93112        void (* abort)( void ) __attribute__(( __noreturn__ ));
     113        typeof(pthread_create) pthread_create;
     114        typeof(pthread_join) pthread_join;
     115        typeof(pthread_self) pthread_self;
     116        typeof(pthread_cond_init) pthread_cond_init;
     117        typeof(pthread_cond_destroy) pthread_cond_destroy;
     118        typeof(pthread_cond_signal) pthread_cond_signal;
     119        typeof(pthread_cond_broadcast) pthread_cond_broadcast;
     120        typeof(pthread_cond_wait) pthread_cond_wait;
     121        typeof(pthread_mutex_init) pthread_mutex_init;
     122        typeof(pthread_mutex_lock) pthread_mutex_lock;
     123        typeof(pthread_mutex_trylock) pthread_mutex_trylock;
     124        typeof(pthread_mutex_unlock) pthread_mutex_unlock;
     125        typeof(pthread_mutex_destroy) pthread_mutex_destroy;
     126        typeof(pthread_attr_init ) pthread_attr_init;
     127        typeof(pthread_attr_setstack ) pthread_attr_setstack;
     128        typeof(pthread_attr_destroy) pthread_attr_destroy;
     129        typeof(pthread_attr_getstacksize) pthread_attr_getstacksize;
    94130} __cabi_libc;
    95131
     
    107143                INTERPOSE_LIBC( abort, version );
    108144                INTERPOSE_LIBC( exit , version );
     145                INTERPOSE_LIBC( pthread_create , version );
     146                INTERPOSE_LIBC( pthread_join , version );
     147                INTERPOSE_LIBC( pthread_self , version );
     148                INTERPOSE_LIBC( pthread_mutex_init , version );
     149                INTERPOSE_LIBC( pthread_mutex_lock , version );
     150                INTERPOSE_LIBC( pthread_mutex_unlock , version );
     151                INTERPOSE_LIBC( pthread_mutex_destroy , version );
     152                INTERPOSE_LIBC( pthread_cond_init , version );
     153                INTERPOSE_LIBC( pthread_cond_destroy , version );
     154                INTERPOSE_LIBC( pthread_cond_signal , version );
     155                INTERPOSE_LIBC( pthread_cond_broadcast , version );
     156                INTERPOSE_LIBC( pthread_cond_wait , version );
     157                INTERPOSE_LIBC( pthread_attr_init , version );
     158                INTERPOSE_LIBC( pthread_attr_destroy , version );
     159                INTERPOSE_LIBC( pthread_attr_setstack , version );
     160                INTERPOSE_LIBC( pthread_attr_getstacksize , version );
    109161#pragma GCC diagnostic pop
    110162
     
    168220                __cabi_libc.exit( status );
    169221        }
     222
     223        libcfa_public int real_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){
     224                return __cabi_libc.pthread_create(_thread, attr, start_routine, arg);
     225        }
     226
     227        libcfa_public int real_pthread_join(pthread_t _thread, void **retval){
     228                return __cabi_libc.pthread_join(_thread, retval);
     229        }
     230
     231        libcfa_public pthread_t real_pthread_self(void){
     232                return __cabi_libc.pthread_self();
     233        }
     234        /* mutex Default attr is PTHREAD_MUTEX_RECURSIVE*/
     235        libcfa_public int real_pthread_mutex_init(pthread_mutex_t *_mutex, const pthread_mutexattr_t *attr){
     236                return __cabi_libc.pthread_mutex_init(_mutex, attr);
     237        }
     238        libcfa_public int real_pthread_mutex_destroy(pthread_mutex_t *_mutex){
     239                return __cabi_libc.pthread_mutex_destroy(_mutex);
     240        }
     241        libcfa_public int real_pthread_mutex_lock(pthread_mutex_t *_mutex){
     242                return __cabi_libc.pthread_mutex_lock(_mutex);
     243        }
     244        libcfa_public int real_pthread_mutex_unlock(pthread_mutex_t *_mutex){
     245                return  __cabi_libc.pthread_mutex_unlock(_mutex);
     246        }
     247        libcfa_public int real_pthread_mutex_trylock(pthread_mutex_t *_mutex){
     248                return __cabi_libc.pthread_mutex_trylock(_mutex);
     249        }
     250        libcfa_public int real_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr){
     251                return __cabi_libc.pthread_cond_init(cond, attr);
     252        }
     253        libcfa_public int real_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *_mutex){
     254                return __cabi_libc.pthread_cond_wait(cond, _mutex);
     255        }
     256        libcfa_public int real_pthread_cond_signal(pthread_cond_t *cond){
     257                return __cabi_libc.pthread_cond_signal(cond);
     258        }
     259        libcfa_public int real_pthread_cond_broadcast(pthread_cond_t *cond){
     260                return __cabi_libc.pthread_cond_broadcast(cond);
     261        }
     262        libcfa_public int real_pthread_cond_destroy(pthread_cond_t *cond){
     263                return __cabi_libc.pthread_cond_destroy(cond);
     264        }
     265        libcfa_public int real_pthread_attr_init(pthread_attr_t *attr){
     266                return __cabi_libc.pthread_attr_init(attr);
     267        }
     268        libcfa_public int real_pthread_attr_destroy(pthread_attr_t *attr){
     269                return __cabi_libc.pthread_attr_destroy(attr);
     270        }
     271        libcfa_public int real_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ){
     272                return __cabi_libc.pthread_attr_setstack(attr, stackaddr, stacksize);
     273        }
     274        libcfa_public int pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ){
     275                return __cabi_libc.pthread_attr_getstacksize(attr, stacksize);
     276        }
     277
    170278}
    171279
Note: See TracChangeset for help on using the changeset viewer.