add a phtread implementation of semaphores for general runtime use
[IRC.git] / Robust / src / Runtime / mlp_runtime.c
1 #include "runtime.h"
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <assert.h>
6
7 #include "mlp_runtime.h"
8 #include "Queue.h"
9
10
11 #define FALSE 0
12 #define TRUE  1
13
14
15 // the root sese is accessible globally so
16 // buildcode can generate references to it
17 SESErecord* rootsese;
18
19
20 // the issuedQ, in this simple version, spits
21 // out SESErecord's in the order they were issued
22 static struct Queue* issuedQ;
23
24
25 // the class_age2instance maps an SESE class id and
26 // age value to a particular SESErecord instance
27 static SESErecord** class_age2instance;
28
29
30 // each core/pthread should have a current SESE
31 static SESErecord* current;
32
33
34 SESErecord* mlpCreateSESErecord( int         classID,
35                                  int         instanceID,
36                                  SESErecord* parent,
37                                  int         numVars,
38                                  void*       paramStruct
39                                ) {
40
41   SESErecord* newrec = malloc( sizeof( SESErecord ) );
42
43   newrec->classID          = classID;
44   //newrec->instanceID       = instanceID;
45   //newrec->childInstanceIDs = 0;
46   newrec->parent           = parent;
47   newrec->childrenList     = createQueue();
48   newrec->vars             = (SESEvar*) malloc( sizeof( SESEvar ) *
49                                                 numVars
50                                               );
51   newrec->paramStruct      = paramStruct;
52   newrec->forwardList      = createQueue();
53   newrec->doneExecuting    = FALSE;
54   newrec->startedExecuting = FALSE;
55
56   newrec->startCondVar     = (pthread_cond_t*)  malloc( sizeof( pthread_cond_t  ) );
57   newrec->startCondVarLock = (pthread_mutex_t*) malloc( sizeof( pthread_mutex_t ) );
58   newrec->forwardListLock  = (pthread_mutex_t*) malloc( sizeof( pthread_mutex_t ) );
59
60   pthread_cond_init ( newrec->startCondVar,     NULL );
61   pthread_mutex_init( newrec->startCondVarLock, NULL );
62   pthread_mutex_init( newrec->forwardListLock,  NULL );
63
64   return newrec;
65 }
66
67
68 void mlpDestroySESErecord( SESErecord* sese ) {
69
70   pthread_cond_destroy ( sese->startCondVar     );
71   pthread_mutex_destroy( sese->startCondVarLock );
72   pthread_mutex_destroy( sese->forwardListLock  );
73
74   free     ( sese->startCondVar     );
75   free     ( sese->startCondVarLock );
76   free     ( sese->forwardListLock  );
77
78   freeQueue( sese->forwardList      );
79   freeQueue( sese->childrenList     );
80   free     ( sese->vars             );
81   free     ( sese                   );
82 }
83
84
85 void mlpInit( int totalNumSESEs, int maxSESEage ) {  
86
87   issuedQ = createQueue();
88
89   class_age2instance = (SESErecord**) malloc( sizeof( SESErecord* ) *
90                                               maxSESEage            *
91                                               totalNumSESEs
92                                             );
93   //current = rootsese;
94   current = NULL;
95 }
96
97
98 SESErecord* mlpGetCurrent() {
99   return current;
100 }
101
102
103 void mlpIssue( SESErecord* sese ) {
104   addNewItem( issuedQ, (void*) sese );
105 }
106
107
108 SESErecord* mlpSchedule() {
109   assert( !isEmpty( issuedQ ) );
110   return (SESErecord*) getItem( issuedQ );  
111 }
112
113
114 void mlpStall( SESErecord* sese ) {
115   
116 }
117
118
119 void mlpNotifyExit( SESErecord* sese ) {
120   
121 }