- Timestamp:
- Dec 15, 2020, 12:51:15 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 2dd0689, 54eb1bb3, 72a3aff
- Parents:
- 90ecade
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/thierry_delisle_PhD/code/readQ_example/thrdlib/thread.cpp
r90ecade r72b1800 1 #include "thread.hpp" 1 #if !defined(WITH_PTHREADS) && !defined(WITH_LIBFIBRE) && !defined(WITH_CFORALL) 2 #error must define WITH_PTHREADS, WITH_LIBFIBRE or WITH_CFORALL 3 #endif 2 4 3 #i nclude <cstdarg> // va_start, va_end4 #include <cstdio>5 # include <cstring> // strlen6 extern "C" { 7 #include <unistd.h> // _exit, getpid 8 #include <signal.h> 9 #include <dlfcn.h> // dlopen, dlsym 10 #include <execinfo.h> // backtrace, messages 11 } 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 12 14 13 #include <iostream> 14 #include <string> 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(); } 15 23 16 using thrdlib::thread_t; 17 18 thread_t (*thrdlib::create)( void (*main)( thread_t ) ) = nullptr; 19 void (*thrdlib::join)( thread_t handle ) = nullptr; 20 void (*thrdlib::park)( thread_t handle ) = nullptr; 21 void (*thrdlib::unpark)( thread_t handle ) = nullptr; 22 void (*thrdlib::yield)( void ) = nullptr; 23 void (*lib_clean)(void) = nullptr; 24 25 typedef void (*fptr_t)(); 26 static 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 40 void 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 66 void thrdlib::clean( void ) { 67 if(lib_clean) lib_clean(); 68 } 24 //-------------------- 25 // Basic kernel features 26 void init( int procs ) { thrdlib_init(procs); } 27 void clean( void ) { thrdlib_clean(); } 28 };
Note: See TracChangeset
for help on using the changeset viewer.