Changeset ba0e1bc


Ignore:
Timestamp:
Nov 6, 2023, 2:20:09 PM (7 months ago)
Author:
caparson <caparson@…>
Branches:
master
Children:
0d41b2e
Parents:
3d9d017
Message:

Added supporting library routines for cofor impl

Location:
libcfa/src/concurrency
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/cofor.cfa

    r3d9d017 rba0e1bc  
    44// cofor ( uC++ COFOR )
    55
    6 thread cofor_runner {
     6thread cofor_task {
    77        ssize_t low, high;
    88        __cofor_body_t loop_body;
    99};
    1010
    11 static void ?{}( cofor_runner & this, ssize_t low, ssize_t high, __cofor_body_t loop_body ) {
     11static void ?{}( cofor_task & this, ssize_t low, ssize_t high, __cofor_body_t loop_body ) {
    1212        this.low = low;
    1313        this.high = high;
     
    1515}
    1616
    17 void main( cofor_runner & this ) with( this ) {
     17void main( cofor_task & this ) with( this ) {
    1818        for ( ssize_t i = low; i < high; i++ )
    1919                loop_body(i);
     
    2929        ssize_t i = 0;
    3030        ssize_t stride_iter = low;
    31         cofor_runner * runners[ threads ];
     31        cofor_task * runners[ threads ];
    3232        for ( i; threads ) {
    3333                runners[i] = alloc();
  • libcfa/src/concurrency/cofor.hfa

    r3d9d017 rba0e1bc  
    11#include <thread.hfa>
     2#include <locks.hfa>
     3#include <list.hfa>
    24
    35//////////////////////////////////////////////////////////////////////////////////////////
     
    1416                __Cofor__( low, high, __CFA_loopLambda__ ); \
    1517        }
     18
     19struct runner_node {
     20    void * value;
     21    inline dlink(runner_node);
     22};
     23P9_EMBEDDED( runner_node, dlink(runner_node) )
     24
     25thread cofor_runner {
     26        go_mutex mutex_lock;              // MX lock
     27    dlist( runner_node ) items;
     28    void (*func)(void *);
     29    volatile bool done;
     30};
     31
     32void ?{}( cofor_runner & this ) { this.done = false; }
     33
     34void main( cofor_runner & this ) with(this) {
     35    while ( !done || !items`isEmpty ) {
     36                lock( mutex_lock );
     37        runner_node * node = &try_pop_front( items );
     38                unlock( mutex_lock );
     39        func( node->value );
     40        free( node->value );
     41        free( node );
     42    }
     43}
     44
     45void start_runners( cofor_runner * thds, unsigned nprocs, void (*func)(void *) ) {
     46        for ( i; nprocs ) {
     47                thds[i].func = func;
     48        }
     49}
     50
     51void end_runners( cofor_runner * thds, unsigned nprocs ) {
     52        for ( i; nprocs ) {
     53                thds[i].done = true;
     54        }
     55}
     56
     57void send_work( cofor_runner * thds, unsigned nprocs, unsigned & curr_proc, void * value ) {
     58        runner_node * node = malloc();
     59        (*node){};
     60        node->value = value;
     61        lock( thds[curr_proc].mutex_lock );
     62        insert_last( thds[curr_proc].items, *node );
     63        unlock( thds[curr_proc].mutex_lock );
     64        curr_proc = ( curr_proc + 1 ) % nprocs;
     65}
    1666
    1767//////////////////////////////////////////////////////////////////////////////////////////
     
    4292    delete( this.runner );
    4393}
    44 
Note: See TracChangeset for help on using the changeset viewer.