Changeset 93d2219 for libcfa/src/concurrency
- Timestamp:
- Oct 28, 2022, 3:12:16 PM (3 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- fa2e183
- Parents:
- e874605 (diff), 22a0e87 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- libcfa/src/concurrency
- Files:
-
- 1 added
- 12 edited
-
clib/cfathread.cfa (modified) (1 diff)
-
invoke.h (modified) (1 diff)
-
io.cfa (modified) (1 diff)
-
io/setup.cfa (modified) (1 diff)
-
kernel.cfa (modified) (2 diffs)
-
kernel.hfa (modified) (3 diffs)
-
kernel/private.hfa (modified) (3 diffs)
-
kernel/startup.cfa (modified) (12 diffs)
-
locks.hfa (modified) (2 diffs)
-
preemption.cfa (modified) (9 diffs)
-
pthread.cfa (added)
-
thread.cfa (modified) (2 diffs)
-
thread.hfa (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/clib/cfathread.cfa
re874605 r93d2219 172 172 173 173 pthread_attr_t attr; 174 if (int ret = pthread_attr_init(&attr); 0 != ret) {174 if (int ret = __cfaabi_pthread_attr_init(&attr); 0 != ret) { 175 175 abort | "failed to create master epoll thread attr: " | ret | strerror(ret); 176 176 } 177 177 178 if (int ret = pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) {178 if (int ret = __cfaabi_pthread_create(&master_poller, &attr, master_epoll, 0p); 0 != ret) { 179 179 abort | "failed to create master epoll thread: " | ret | strerror(ret); 180 180 } -
libcfa/src/concurrency/invoke.h
re874605 r93d2219 214 214 215 215 #if defined( __CFA_WITH_VERIFY__ ) 216 struct processor * volatile executing; 216 217 void * canary; 217 218 #endif -
libcfa/src/concurrency/io.cfa
re874605 r93d2219 610 610 if( we ) { 611 611 sigval_t value = { PREEMPT_IO }; 612 pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value);612 __cfaabi_pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value); 613 613 } 614 614 -
libcfa/src/concurrency/io/setup.cfa
re874605 r93d2219 344 344 // iopoll.run = false; 345 345 // sigval val = { 1 }; 346 // pthread_sigqueue( iopoll.thrd, SIGUSR1, val );346 // __cfaabi_pthread_sigqueue( iopoll.thrd, SIGUSR1, val ); 347 347 348 348 // // Make sure all this is done -
libcfa/src/concurrency/kernel.cfa
re874605 r93d2219 321 321 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor 322 322 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor 323 /* paranoid */ verify( __atomic_exchange_n( &thrd_dst->executing, this, __ATOMIC_SEQ_CST) == 0p ); 323 324 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); 324 325 … … 332 333 333 334 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); 335 /* paranoid */ verify( __atomic_exchange_n( &thrd_dst->executing, 0p, __ATOMIC_SEQ_CST) == this ); 334 336 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); 335 337 /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->corctx_flag, "ERROR : Destination thread$ %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); 338 /* paranoid */ verify( thrd_dst->state != Halted ); 336 339 /* paranoid */ verify( thrd_dst->context.SP ); 337 /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );338 340 /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); 339 341 /* paranoid */ verify( ! __preemption_enabled() ); -
libcfa/src/concurrency/kernel.hfa
re874605 r93d2219 160 160 // P9_EMBEDDED( processor, dlink(processor) ) 161 161 static inline tytagref( dlink(processor), dlink(processor) ) ?`inner( processor & this ) { 162 dlink(processor) & b = this.link;163 tytagref( dlink(processor), dlink(processor) ) result = { b };164 return result;162 dlink(processor) & b = this.link; 163 tytagref( dlink(processor), dlink(processor) ) result = { b }; 164 return result; 165 165 } 166 166 … … 269 269 io_context_params params; 270 270 } io; 271 272 struct { 273 struct processor ** procs; 274 unsigned cnt; 275 } managed; 271 276 272 277 #if !defined(__CFA_NO_STATISTICS__) … … 298 303 static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; } 299 304 305 // set the number of internal processors 306 // these processors are in addition to any explicitly declared processors 307 unsigned set_concurrency( cluster & this, unsigned new_count ); 308 300 309 #if !defined(__CFA_NO_STATISTICS__) 301 310 void print_stats_now( cluster & this, int flags ); -
libcfa/src/concurrency/kernel/private.hfa
re874605 r93d2219 20 20 #endif 21 21 22 #include <signal.h> 23 22 24 #include "kernel.hfa" 23 25 #include "thread.hfa" … … 48 50 #endif 49 51 #endif 50 51 52 // #define READYQ_USE_LINEAR_AVG 52 53 #define READYQ_USE_LOGDBL_AVG … … 62 63 #error must pick a scheme for averaging 63 64 #endif 65 66 extern "C" { 67 __attribute__((visibility("protected"))) int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 68 __attribute__((visibility("protected"))) int __cfaabi_pthread_join(pthread_t _thread, void **retval); 69 __attribute__((visibility("protected"))) pthread_t __cfaabi_pthread_self(void); 70 __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_init(pthread_attr_t *attr); 71 __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr); 72 __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize ); 73 __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize ); 74 __attribute__((visibility("protected"))) int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value); 75 __attribute__((visibility("protected"))) int __cfaabi_pthread_sigmask( int how, const sigset_t *set, sigset_t *oset); 76 } 64 77 65 78 //----------------------------------------------------------------------------- -
libcfa/src/concurrency/kernel/startup.cfa
re874605 r93d2219 16 16 #define __cforall_thread__ 17 17 #define _GNU_SOURCE 18 19 // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__ 18 20 19 21 // C Includes … … 222 224 ( this.runner ){}; 223 225 init( this, "Main Processor", *mainCluster, 0p ); 224 kernel_thread = pthread_self();226 kernel_thread = __cfaabi_pthread_self(); 225 227 226 228 runner{ &this }; … … 283 285 } 284 286 287 extern "C"{ 288 void pthread_delete_kernel_threads_(); 289 } 290 291 285 292 static void __kernel_shutdown(void) { 286 293 if(!cfa_main_returned) return; 294 295 //delete kernel threads for pthread_concurrency 296 pthread_delete_kernel_threads_(); 297 287 298 /* paranoid */ verify( __preemption_enabled() ); 288 299 disable_interrupts(); … … 327 338 328 339 /* paranoid */ verify( this.do_terminate == true ); 329 __cfa abi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);340 __cfadbg_print_safe(runtime_core, "Kernel : destroyed main processor context %p\n", &runner); 330 341 } 331 342 … … 388 399 (proc->runner){ proc, &info }; 389 400 390 __cfa abi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);401 __cfadbg_print_safe(runtime_core, "Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage); 391 402 392 403 //Set global state … … 520 531 random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl(); 521 532 #if defined( __CFA_WITH_VERIFY__ ) 533 executing = 0p; 522 534 canary = 0x0D15EA5E0D15EA5Ep; 523 535 #endif … … 652 664 io.params = io_params; 653 665 666 managed.procs = 0p; 667 managed.cnt = 0; 668 654 669 doregister(this); 655 670 … … 667 682 668 683 void ^?{}(cluster & this) libcfa_public { 684 set_concurrency( this, 0 ); 685 669 686 destroy(this.io.arbiter); 670 687 … … 777 794 pthread_attr_t attr; 778 795 779 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute796 check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute 780 797 781 798 size_t stacksize = max( PTHREAD_STACK_MIN, DEFAULT_STACK_SIZE ); … … 804 821 #endif 805 822 806 check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );807 check( pthread_create( pthread, &attr, start, arg ), "pthread_create" );823 check( __cfaabi_pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); 824 check( __cfaabi_pthread_create( pthread, &attr, start, arg ), "pthread_create" ); 808 825 return stack; 809 826 } 810 827 811 828 void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) { 812 int err = pthread_join( pthread, retval );829 int err = __cfaabi_pthread_join( pthread, retval ); 813 830 if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err)); 814 831 … … 816 833 pthread_attr_t attr; 817 834 818 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute835 check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute 819 836 820 837 size_t stacksize; 821 838 // default stack size, normally defined by shell limit 822 check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );839 check( __cfaabi_pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); 823 840 assert( stacksize >= PTHREAD_STACK_MIN ); 824 841 stacksize += __page_size; … … 838 855 } 839 856 857 unsigned set_concurrency( cluster & this, unsigned new ) libcfa_public { 858 unsigned old = this.managed.cnt; 859 860 __cfadbg_print_safe(runtime_core, "Kernel : resizing cluster from %u to %u\n", old, (unsigned)new); 861 862 // Delete all the old unneeded procs 863 if(old > new) for(i; (unsigned)new ~ old) { 864 __cfadbg_print_safe(runtime_core, "Kernel : destroying %u\n", i); 865 delete( this.managed.procs[i] ); 866 } 867 868 // Allocate new array (uses realloc and memcpies the data) 869 this.managed.procs = alloc( new, this.managed.procs`realloc ); 870 this.managed.cnt = new; 871 872 // Create the desired new procs 873 if(old < new) for(i; old ~ new) { 874 __cfadbg_print_safe(runtime_core, "Kernel : constructing %u\n", i); 875 (*(this.managed.procs[i] = alloc())){ this }; 876 } 877 878 // return the old count 879 return old; 880 } 881 840 882 #if defined(__CFA_WITH_VERIFY__) 841 883 static bool verify_fwd_bck_rng(void) { -
libcfa/src/concurrency/locks.hfa
re874605 r93d2219 21 21 22 22 #include "bits/weakso_locks.hfa" 23 #include "containers/ queueLockFree.hfa"23 #include "containers/lockfree.hfa" 24 24 #include "containers/list.hfa" 25 25 … … 423 423 } 424 424 425 static inline size_t on_wait(simple_owner_lock & this) with(this) { 425 static inline size_t on_wait(simple_owner_lock & this) with(this) { 426 426 lock( lock __cfaabi_dbg_ctx2 ); 427 427 /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); -
libcfa/src/concurrency/preemption.cfa
re874605 r93d2219 352 352 sigset_t oldset; 353 353 int ret; 354 ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary354 ret = __cfaabi_pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary 355 355 if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); } 356 356 … … 385 385 sigaddset( &mask, sig ); 386 386 387 if ( pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) {387 if ( __cfaabi_pthread_sigmask( SIG_UNBLOCK, &mask, 0p ) == -1 ) { 388 388 abort( "internal error, pthread_sigmask" ); 389 389 } … … 396 396 sigaddset( &mask, sig ); 397 397 398 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {398 if ( __cfaabi_pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) { 399 399 abort( "internal error, pthread_sigmask" ); 400 400 } … … 404 404 static void preempt( processor * this ) { 405 405 sigval_t value = { PREEMPT_NORMAL }; 406 pthread_sigqueue( this->kernel_thread, SIGUSR1, value );406 __cfaabi_pthread_sigqueue( this->kernel_thread, SIGUSR1, value ); 407 407 } 408 408 … … 415 415 sigset_t oldset; 416 416 int ret; 417 ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary417 ret = __cfaabi_pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary 418 418 if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); } 419 419 … … 434 434 sigset_t oldset; 435 435 int ret; 436 ret = pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary436 ret = __cfaabi_pthread_sigmask(0, ( const sigset_t * ) 0p, &oldset); // workaround trac#208: cast should be unnecessary 437 437 if(ret != 0) { abort("ERROR sigprocmask returned %d", ret); } 438 438 … … 505 505 sigval val; 506 506 val.sival_int = 0; 507 pthread_sigqueue( alarm_thread, SIGALRM, val );507 __cfaabi_pthread_sigqueue( alarm_thread, SIGALRM, val ); 508 508 509 509 // Wait for the preemption thread to finish … … 579 579 static_assert( sizeof( sigset_t ) == sizeof( cxt->uc_sigmask ), "Expected cxt->uc_sigmask to be of sigset_t" ); 580 580 #endif 581 if ( pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) {581 if ( __cfaabi_pthread_sigmask( SIG_SETMASK, (sigset_t *)&(cxt->uc_sigmask), 0p ) == -1 ) { 582 582 abort( "internal error, sigprocmask" ); 583 583 } … … 607 607 sigset_t mask; 608 608 sigfillset(&mask); 609 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {609 if ( __cfaabi_pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) { 610 610 abort( "internal error, pthread_sigmask" ); 611 611 } -
libcfa/src/concurrency/thread.cfa
re874605 r93d2219 50 50 random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl(); 51 51 #if defined( __CFA_WITH_VERIFY__ ) 52 executing = 0p; 52 53 canary = 0x0D15EA5E0D15EA5Ep; 53 54 #endif … … 177 178 178 179 //----------------------------------------------------------------------------- 180 bool migrate( thread$ * thrd, struct cluster & cl ) { 181 182 monitor$ * tmon = get_monitor(thrd); 183 monitor$ * __monitors[] = { tmon }; 184 monitor_guard_t __guard = { __monitors, 1 }; 185 186 187 { 188 // if nothing needs to be done, return false 189 if( thrd->curr_cluster == &cl ) return false; 190 191 // are we migrating ourself? 192 const bool local = thrd == active_thread(); 193 194 /* paranoid */ verify( !local || &cl != active_cluster() ); 195 /* paranoid */ verify( !local || thrd->curr_cluster == active_cluster() ); 196 /* paranoid */ verify( !local || thrd->curr_cluster == active_processor()->cltr ); 197 /* paranoid */ verify( local || tmon->signal_stack.top->owner->waiting_thread == thrd ); 198 /* paranoid */ verify( local || tmon->signal_stack.top ); 199 200 // make sure we aren't interrupted while doing this 201 // not as important if we aren't local 202 disable_interrupts(); 203 204 // actually move the thread 205 unregister( thrd->curr_cluster, *thrd ); 206 thrd->curr_cluster = &cl; 207 doregister( thrd->curr_cluster, *thrd ); 208 209 // restore interrupts 210 enable_interrupts(); 211 212 // if this is the local thread, we are still running on the old cluster 213 if(local) yield(); 214 215 /* paranoid */ verify( !local || &cl == active_cluster() ); 216 /* paranoid */ verify( !local || thrd->curr_cluster == active_cluster() ); 217 /* paranoid */ verify( !local || thrd->curr_cluster == active_processor()->cltr ); 218 /* paranoid */ verify( local || tmon->signal_stack.top ); 219 /* paranoid */ verify( local || tmon->signal_stack.top->owner->waiting_thread == thrd ); 220 221 return true; 222 } 223 } 224 225 //----------------------------------------------------------------------------- 179 226 #define GENERATOR LCG 180 227 -
libcfa/src/concurrency/thread.hfa
re874605 r93d2219 132 132 133 133 //---------- 134 // misc 135 bool migrate( thread$ * thrd, struct cluster & cl ); 136 137 forall( T & | is_thread(T) ) 138 static inline bool migrate( T & mutex thrd, struct cluster & cl ) { return migrate( &(thread&)thrd, cl ); } 139 140 141 //---------- 134 142 // prng 135 143 static inline {
Note:
See TracChangeset
for help on using the changeset viewer.