Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/thierry_delisle_PhD/code/readQ_example/thrdlib/thread.cpp

    r72b1800 r4e39f51  
    1 #if !defined(WITH_PTHREADS) && !defined(WITH_LIBFIBRE) && !defined(WITH_CFORALL)
    2 #error must define WITH_PTHREADS, WITH_LIBFIBRE or WITH_CFORALL
    3 #endif
     1#include "thread.hpp"
    42
    5 #ifdef WITH_PTHREADS
    6 #include "pthread.hpp"
    7 #endif
    8 #ifdef WITH_LIBFIBRE
    9 #include "fibre.hpp"
    10 #endif
    11 #ifdef WITH_CFORALL
    12 #include "cforall.hpp"
    13 #endif
     3#include <cstdarg>                                                                              // va_start, va_end
     4#include <cstdio>
     5#include <cstring>                                                                              // strlen
     6extern "C" {
     7        #include <unistd.h>                                                                             // _exit, getpid
     8        #include <signal.h>
     9        #include <dlfcn.h>                                                                              // dlopen, dlsym
     10        #include <execinfo.h>                                                                   // backtrace, messages
     11}
    1412
    15 namespace thrdlib {
    16         //--------------------
    17         // Basic thread support
    18         void * create( void (*main)( void * ) ) { return (thread_t)thrdlib_create(  (void (*)( thread_t )) main ); }
    19         void join  ( void * handle ) { thrdlib_join  ((thread_t)handle); }
    20         void park  ( void * handle ) { thrdlib_park  ((thread_t)handle); }
    21         void unpark( void * handle ) { thrdlib_unpark((thread_t)handle); }
    22         void yield( void )  { thrdlib_yield(); }
     13#include <iostream>
     14#include <string>
    2315
    24         //--------------------
    25         // Basic kernel features
    26         void init( int procs ) { thrdlib_init(procs); }
    27         void clean( void ) { thrdlib_clean(); }
    28 };
     16using thrdlib::thread_t;
     17
     18thread_t (*thrdlib::create)( void (*main)( thread_t ) ) = nullptr;
     19void (*thrdlib::join)( thread_t handle ) = nullptr;
     20void (*thrdlib::park)( thread_t handle ) = nullptr;
     21void (*thrdlib::unpark)( thread_t handle ) = nullptr;
     22void (*thrdlib::yield)( void ) = nullptr;
     23void (*lib_clean)(void) = nullptr;
     24
     25typedef void (*fptr_t)();
     26static fptr_t open_symbol( void * library, const char * symbol, bool required ) {
     27        void * ptr = dlsym( library, symbol );
     28
     29        const char * error = dlerror();
     30        if ( required && error ) {
     31                std::cerr << "Fetching symbol '" << symbol << "' failed with error '" << error << "'\n";
     32                std::abort();
     33        }
     34
     35        return (fptr_t)ptr;
     36}
     37
     38//--------------------
     39// Basic kernel features
     40void thrdlib::init( const char * name, int procs ) {
     41        std::string file = __FILE__;
     42        std::size_t found = file.find_last_of("/");
     43        std::string libname = file.substr(0,found+1) + name + ".so";
     44
     45        std::cout << "Use framework " << name << "(" << libname << ")\n";
     46
     47        void * library = dlopen( libname.c_str(), RTLD_NOW );
     48        if ( const char * error = dlerror() ) {
     49                std::cerr << "Could not open library '" << libname << "' from name '" << name <<"'\n";
     50                std::cerr << "Error was : '" << error << "'\n";
     51                std::abort();
     52        }
     53
     54        void (*lib_init)( int ) = (void (*)( int ))open_symbol( library, "thrdlib_init", false );
     55        lib_clean = open_symbol( library, "thrdlib_clean" , false );
     56
     57        thrdlib::create = (typeof(thrdlib::create))open_symbol( library, "thrdlib_create", true  );
     58        thrdlib::join   = (typeof(thrdlib::join  ))open_symbol( library, "thrdlib_join"  , true  );
     59        thrdlib::park   = (typeof(thrdlib::park  ))open_symbol( library, "thrdlib_park"  , true  );
     60        thrdlib::unpark = (typeof(thrdlib::unpark))open_symbol( library, "thrdlib_unpark", true  );
     61        thrdlib::yield  = (typeof(thrdlib::yield ))open_symbol( library, "thrdlib_yield" , true  );
     62
     63        lib_init( procs );
     64}
     65
     66void thrdlib::clean( void ) {
     67        if(lib_clean) lib_clean();
     68}
Note: See TracChangeset for help on using the changeset viewer.