- File:
-
- 1 edited
-
libcfa/src/concurrency/clib/cfathread.cfa (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/clib/cfathread.cfa
rd27b6be rf03e11d 17 17 #include "locks.hfa" 18 18 #include "kernel.hfa" 19 #include "stats.hfa"20 19 #include "thread.hfa" 21 20 #include "time.hfa" 22 21 23 22 #include "cfathread.h" 24 25 extern void ?{}(processor &, const char[], cluster &, $thread *);26 extern "C" {27 extern void __cfactx_invoke_thread(void (*main)(void *), void * this);28 }29 30 //================================================================================31 // Thread run y the C Interface32 23 33 24 struct cfathread_object { … … 74 65 } 75 66 76 //================================================================================ 77 // Special Init Thread responsible for the initialization or processors 78 struct __cfainit { 79 $thread self; 80 void (*init)( void * ); 81 void * arg; 82 }; 83 void main(__cfainit & this); 84 void ^?{}(__cfainit & mutex this); 85 86 static inline $thread * get_thread( __cfainit & this ) { return &this.self; } 87 88 typedef ThreadCancelled(__cfainit) __cfainit_exception; 89 typedef ThreadCancelled_vtable(__cfainit) __cfainit_vtable; 90 91 void defaultResumptionHandler(ThreadCancelled(__cfainit) & except) { 92 abort | "The init thread was cancelled"; 93 } 94 95 __cfainit_vtable ___cfainit_vtable_instance; 96 97 __cfainit_vtable const & get_exception_vtable(__cfainit_exception *) { 98 return ___cfainit_vtable_instance; 99 } 100 101 static void ?{}( __cfainit & this, void (*init)( void * ), void * arg ) { 102 this.init = init; 103 this.arg = arg; 104 ((thread&)this){"Processir Init"}; 105 106 // Don't use __thrd_start! just prep the context manually 107 $thread * this_thrd = get_thread(this); 108 void (*main_p)(__cfainit &) = main; 109 110 disable_interrupts(); 111 __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread); 112 113 this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP]; 114 /* paranoid */ verify( this_thrd->context.SP ); 115 116 this_thrd->state = Ready; 117 enable_interrupts( __cfaabi_dbg_ctx ); 118 } 119 120 void ^?{}(__cfainit & mutex this) { 121 ^(this.self){}; 122 } 123 124 void main( __cfainit & this ) { 125 __attribute__((unused)) void * const thrd_obj = (void*)&this; 126 __attribute__((unused)) void * const thrd_hdl = (void*)active_thread(); 127 /* paranoid */ verify( thrd_obj == thrd_hdl ); 128 129 this.init( this.arg ); 130 } 131 132 //================================================================================ 133 // Main Api 67 processor * procs = 0p; 68 int proc_cnt = 1; 69 134 70 extern "C" { 135 71 int cfathread_cluster_create(cfathread_cluster_t * cl) __attribute__((nonnull(1))) { … … 142 78 } 143 79 144 int cfathread_cluster_print_stats( cfathread_cluster_t cl ) {145 #if !defined(__CFA_NO_STATISTICS__)146 print_stats_at_exit( *cl, CFA_STATS_READY_Q | CFA_STATS_IO );147 print_stats_now( *cl, CFA_STATS_READY_Q | CFA_STATS_IO );148 #endif149 return 0;150 }151 152 80 int cfathread_cluster_add_worker(cfathread_cluster_t cl, pthread_t* tid, void (*init_routine) (void *), void * arg) { 153 __cfainit * it = 0p; 154 if(init_routine) { 155 it = alloc(); 156 (*it){init_routine, arg}; 157 } 81 // processor * proc = new("C-processor", *cl, init_routine, arg); 158 82 processor * proc = alloc(); 159 (*proc){ "C-processor", *cl, get_thread(*it) }; 160 161 // Wait for the init thread to return before continuing 162 if(it) { 163 ^(*it){}; 164 free(it); 165 } 166 83 (*proc){ "C-processor", *cl, init_routine, arg }; 167 84 if(tid) *tid = proc->kernel_thread; 168 85 return 0; … … 245 162 int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict) __attribute__((nonnull (1))) { *mut = new(); return 0; } 246 163 int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { delete( *mut ); return 0; } 247 int cfathread_mutex_lock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { lock( (*mut)->impl ); return 0; } 248 int cfathread_mutex_unlock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { unlock( (*mut)->impl ); return 0; } 249 int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { 250 bool ret = try_lock( (*mut)->impl ); 251 if( ret ) return 0; 252 else return EBUSY; 253 } 164 int cfathread_mutex_lock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { lock ( (*mut)->impl ); return 0; } 165 int cfathread_mutex_trylock(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { try_lock( (*mut)->impl ); return 0; } 166 int cfathread_mutex_unlock (cfathread_mutex_t *mut) __attribute__((nonnull (1))) { unlock ( (*mut)->impl ); return 0; } 254 167 255 168 //--------------------
Note:
See TracChangeset
for help on using the changeset viewer.