libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

fibers.c (2575B)


      1 //
      2 
      3 #include "../release/libsheepy.h"
      4 
      5 #define internal static
      6 
      7 #include <stdio.h>
      8 
      9 void fiberA(int thisSlot);
     10 void fiberB(int thisSlot);
     11 #ifndef unitTest
     12 #endif
     13 int MAIN(int ARGC, char** ARGV);
     14 
     15 int argc; char **argv;
     16 
     17 // make a ring with a buffer of 30 int elements
     18 #define chanMax   30
     19 ringMake(chanT, int, chanMax);
     20 
     21 // context for fibers of type fiberA
     22 typedef struct {int slot; int a; chanT *c;} AArgs;
     23 
     24 // context for fibers of type fiberA
     25 typedef struct {int slot; int a; int b; chanT *c;} BArgs;
     26 
     27 // declaration of fibers type fiberA
     28 void fiberA(int thisSlot) {
     29   // static to save local variable
     30   static int r = 0;;
     31   static AArgs *ctx = NULL;
     32   int slot;
     33 
     34   // get context for this fiber
     35   ctx = fiberCtx(thisSlot);
     36 
     37   puts("a1");
     38   r++;
     39   ctx->a++;
     40 
     41   // send data in ring
     42   puts("ring send 10");
     43   ringSend(ctx->c, 10);
     44 
     45   // switch to scheduler
     46   yield(slot, ctx->slot);
     47   ctx = fiberCtx(slot);
     48 
     49   puts("a2");
     50 
     51   logVar(r, "d");
     52   logVar(slot, "d");
     53   logVar(ctx->a, "d");
     54 
     55   ctx->a++;
     56 
     57   // switch to scheduler
     58   yield(slot, ctx->slot);
     59   ctx = fiberCtx(slot);
     60 
     61   puts("a3");
     62   logVar(r, "d");
     63   logVar(slot, "d");
     64   logVar(ctx->a, "d");
     65 }
     66 
     67 static AArgs Ca;
     68 
     69 // declaration of fibers type fiberA
     70 void fiberB(int thisSlot) {
     71   // static to save local variable
     72   static int r = 0;;
     73   static BArgs *ctx = NULL;
     74   int slot;;
     75 
     76   // get context for this fiber
     77   ctx = fiberCtx(thisSlot);
     78 
     79   puts("b1");
     80   r+=2;;
     81   ctx->a++;
     82   ctx->b++;
     83 
     84   // back to scheduler
     85   yield(slot, ctx->slot);
     86   ctx = fiberCtx(slot);
     87 
     88   puts("b2");
     89 
     90   logVar(r, "d");
     91   logVar(slot, "d");
     92 
     93   // receive data from the ring
     94   puts("ring receive in B");
     95   ringRecv(ctx->c, r)
     96   printf(" %d\n", r);
     97 
     98   puts(__func__);
     99 
    100   // start a fiber type fiberA on slot 3
    101   Ca.a    = 0;
    102   Ca.slot = 3;
    103   fiberAdd(&Ca, 3, fiberA);
    104 
    105   yield(slot, ctx->slot);
    106   ctx = fiberCtx(slot);
    107 
    108   puts("b3");
    109   logVar(slot, "d");
    110   logVar(ctx->a, "d");
    111   logVar(ctx->b, "d");
    112 
    113   // end of fiberB
    114 }
    115 
    116 #ifndef unitTest
    117 // Remove main when running the unit tests
    118 #define MAIN   main
    119 #endif
    120 int MAIN(int ARGC, char** ARGV) {
    121   int d UNUSED;
    122 
    123   argc = ARGC; argv = ARGV;;//
    124 
    125   AArgs Aa;
    126   BArgs Ba;
    127   // the ring between A and B
    128   chanT c;
    129   ringInit(&c, chanMax);
    130 
    131   initLibsheepy(argv[0]);
    132 
    133   // start a fiberA on slot 1
    134   puts(__func__);
    135   Aa.a = 0;
    136   Aa.c = &c;;
    137   fiberAdd(&Aa, 1, fiberA);
    138 
    139   AArgs *a = &Aa;
    140   Ca = *a;;
    141   Ca.c = &c;;
    142   printf("Ca %d\n", Ca.slot);
    143 
    144   // start a fiberB on slot 2
    145   puts(__func__);
    146   Ba.a = 1;
    147   Ba.b = 2;
    148   Ba.c = &c;;
    149   fiberAdd(&Ba, 2, fiberB);
    150 
    151   // run the fibers
    152   scheduler();
    153 }
    154