Ignore:
Timestamp:
Mar 23, 2021, 9:19:47 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
Children:
98d9ce9
Parents:
f9c3100 (diff), e825c9d (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
libcfa/src/concurrency/clib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/clib/cfathread.cfa

    rf9c3100 r1f55a75  
    1717#include "locks.hfa"
    1818#include "kernel.hfa"
     19#include "stats.hfa"
    1920#include "thread.hfa"
    2021#include "time.hfa"
    2122
    2223#include "cfathread.h"
     24
     25extern void ?{}(processor &, const char[], cluster &, $thread *);
     26extern "C" {
     27      extern void __cfactx_invoke_thread(void (*main)(void *), void * this);
     28}
     29
     30//================================================================================
     31// Thread run y the C Interface
    2332
    2433struct cfathread_object {
     
    6574}
    6675
    67 processor * procs = 0p;
    68 int proc_cnt = 1;
    69 
     76//================================================================================
     77// Special Init Thread responsible for the initialization or processors
     78struct __cfainit {
     79        $thread self;
     80        void (*init)( void * );
     81        void * arg;
     82};
     83void main(__cfainit & this);
     84void ^?{}(__cfainit & mutex this);
     85
     86static inline $thread * get_thread( __cfainit & this ) { return &this.self; }
     87
     88typedef ThreadCancelled(__cfainit) __cfainit_exception;
     89typedef ThreadCancelled_vtable(__cfainit) __cfainit_vtable;
     90
     91void 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
     101static 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
     120void ^?{}(__cfainit & mutex this) {
     121        ^(this.self){};
     122}
     123
     124void 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
    70134extern "C" {
    71135        int cfathread_cluster_create(cfathread_cluster_t * cl) __attribute__((nonnull(1))) {
     
    78142        }
    79143
     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                #endif
     149                return 0;
     150        }
     151
    80152        int cfathread_cluster_add_worker(cfathread_cluster_t cl, pthread_t* tid, void (*init_routine) (void *), void * arg) {
    81                 // processor * proc = new("C-processor", *cl, init_routine, arg);
     153                __cfainit * it = 0p;
     154                if(init_routine) {
     155                        it = alloc();
     156                        (*it){init_routine, arg};
     157                }
    82158                processor * proc = alloc();
    83                 (*proc){ "C-processor", *cl, init_routine, arg };
     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
    84167                if(tid) *tid = proc->kernel_thread;
    85168                return 0;
     
    162245        int cfathread_mutex_init(cfathread_mutex_t *restrict mut, const cfathread_mutexattr_t *restrict) __attribute__((nonnull (1))) { *mut = new(); return 0; }
    163246        int cfathread_mutex_destroy(cfathread_mutex_t *mut) __attribute__((nonnull (1))) { delete( *mut ); return 0; }
    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; }
     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        }
    167254
    168255        //--------------------
  • libcfa/src/concurrency/clib/cfathread.h

    rf9c3100 r1f55a75  
    1313// Update Count     :
    1414//
    15 
    16 #include "stddef.h"
    17 #include "invoke.h"
    1815
    1916#if defined(__cforall) || defined(__cplusplus)
     
    3229        int cfathread_cluster_create(cfathread_cluster_t * cluster);
    3330        cfathread_cluster_t cfathread_cluster_self(void);
     31        int cfathread_cluster_print_stats(cfathread_cluster_t cluster);
    3432        int cfathread_cluster_add_worker(cfathread_cluster_t cluster, pthread_t* tid, void (*init_routine) (void *), void * arg);
    3533        int cfathread_cluster_pause (cfathread_cluster_t cluster);
Note: See TracChangeset for help on using the changeset viewer.