| rev |
line source |
|
Me@0
|
1 /*
|
|
Me@0
|
2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
|
|
Me@0
|
3 *
|
|
Me@0
|
4 * Licensed under BSD
|
|
Me@0
|
5 */
|
|
Me@0
|
6
|
|
Me@0
|
7 #include <stdio.h>
|
|
Me@0
|
8 #include <stdlib.h>
|
|
Me@0
|
9 #include <malloc.h>
|
|
Me@0
|
10
|
|
Me@0
|
11 #include "VMS.h"
|
|
Me@0
|
12 #include "Queue_impl/BlockingQueue.h"
|
|
Me@0
|
13
|
|
Me@0
|
14
|
|
Me@22
|
15 //===========================================================================
|
|
Me@22
|
16 void
|
|
Me@22
|
17 shutdownFn( void *dummy, VirtProcr *dummy2 );
|
|
Me@22
|
18
|
|
Me@22
|
19 void
|
|
Me@22
|
20 create_sched_slots( MasterEnv *masterEnv );
|
|
Me@22
|
21
|
|
Me@22
|
22 //===========================================================================
|
|
Me@22
|
23
|
|
Me@0
|
24 /*Setup has two phases:
|
|
Me@0
|
25 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts
|
|
Me@8
|
26 * the master virt procr into the work-queue, ready for first "call"
|
|
Me@8
|
27 * 2) Semantic layer then does its own init, which creates the seed virt
|
|
Me@8
|
28 * procr inside the semantic layer, ready to schedule it when
|
|
Me@0
|
29 * asked by the first run of the masterLoop.
|
|
Me@0
|
30 *
|
|
Me@0
|
31 *This part is bit weird because VMS really wants to be "always there", and
|
|
Me@0
|
32 * have applications attach and detach.. for now, this VMS is part of
|
|
Me@0
|
33 * the app, so the VMS system starts up as part of running the app.
|
|
Me@0
|
34 *
|
|
Me@8
|
35 *The semantic layer is isolated from the VMS internals by making the
|
|
Me@8
|
36 * semantic layer do setup to a state that it's ready with its
|
|
Me@8
|
37 * initial virt procrs, ready to schedule them to slots when the masterLoop
|
|
Me@0
|
38 * asks. Without this pattern, the semantic layer's setup would
|
|
Me@8
|
39 * have to modify slots directly to assign the initial virt-procrs, and put
|
|
Me@0
|
40 * them into the workQ itself, breaking the isolation completely.
|
|
Me@0
|
41 *
|
|
Me@0
|
42 *
|
|
Me@8
|
43 *The semantic layer creates the initial virt procr(s), and adds its
|
|
Me@8
|
44 * own environment to masterEnv, and fills in the pointers to
|
|
Me@0
|
45 * the requestHandler and slaveScheduler plug-in functions
|
|
Me@8
|
46 */
|
|
Me@8
|
47
|
|
Me@8
|
48 /*This allocates VMS data structures, populates the master VMSProc,
|
|
Me@0
|
49 * and master environment, and returns the master environment to the semantic
|
|
Me@0
|
50 * layer.
|
|
Me@0
|
51 */
|
|
Me@8
|
52 void
|
|
Me@8
|
53 VMS__init()
|
|
Me@1
|
54 { MasterEnv *masterEnv;
|
|
Me@12
|
55 CASQueueStruc *workQ;
|
|
Me@1
|
56
|
|
Me@0
|
57 //Make the central work-queue
|
|
Me@12
|
58 _VMSWorkQ = makeCASQ();
|
|
Me@1
|
59 workQ = _VMSWorkQ;
|
|
Me@0
|
60
|
|
Me@1
|
61 _VMSMasterEnv = malloc( sizeof(MasterEnv) );
|
|
Me@1
|
62 masterEnv = _VMSMasterEnv;
|
|
Me@0
|
63
|
|
Me@8
|
64 //create the master virtual processor
|
|
Me@8
|
65 masterEnv->masterVirtPr = VMS__create_procr( &masterLoop, masterEnv );
|
|
Me@0
|
66
|
|
Me@1
|
67 create_sched_slots( masterEnv );
|
|
Me@0
|
68
|
|
Me@8
|
69 //Set slot 0 to be the master virt procr & set flags just in case
|
|
Me@8
|
70 masterEnv->schedSlots[0]->needsProcrAssigned = FALSE; //says don't touch
|
|
Me@8
|
71 masterEnv->schedSlots[0]->workIsDone = FALSE; //says don't touch
|
|
Me@1
|
72 masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr;
|
|
Me@22
|
73 masterEnv->masterVirtPr->schedSlot = masterEnv->schedSlots[0];
|
|
Me@24
|
74 masterEnv->stillRunning = FALSE;
|
|
Me@22
|
75
|
|
Me@1
|
76 //First core loop to start up gets this, which will schedule seed Pr
|
|
Me@1
|
77 //TODO: debug: check address of masterVirtPr
|
|
Me@22
|
78 writeCASQ( masterEnv->masterVirtPr, workQ );
|
|
Me@12
|
79
|
|
Me@12
|
80 numProcrsCreated = 1;
|
|
Me@24
|
81
|
|
Me@24
|
82 //========================================================================
|
|
Me@24
|
83 // Create the Threads
|
|
Me@25
|
84 int coreIdx, retCode;
|
|
Me@25
|
85 #define thdAttrs NULL
|
|
Me@24
|
86
|
|
Me@25
|
87 _VMSMasterEnv->setupComplete = 0;
|
|
Me@25
|
88 _VMSMasterEnv->suspend_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
Me@25
|
89 _VMSMasterEnv->suspend_cond = PTHREAD_COND_INITIALIZER;
|
|
Me@25
|
90
|
|
Me@25
|
91 //Need the threads to be created suspended, and wait for a signal
|
|
Me@25
|
92 // before proceeding -- gives time after creating to initialize other
|
|
Me@25
|
93 // stuff before the coreLoops set off.
|
|
Me@25
|
94
|
|
Me@24
|
95 //Make params given to the win threads that animate the core loops
|
|
Me@24
|
96 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@24
|
97 { coreLoopThdParams[coreIdx] = malloc( sizeof(ThdParams) );
|
|
Me@24
|
98 coreLoopThdParams[coreIdx]->coreNum = coreIdx;
|
|
Me@24
|
99
|
|
Me@25
|
100 retCode =
|
|
Me@25
|
101 pthread_create( &(coreLoopThdHandles[coreIdx]),
|
|
Me@25
|
102 thdAttrs,
|
|
Me@25
|
103 &coreLoop,
|
|
Me@25
|
104 (void *)(coreLoopThdParams[coreIdx]) );
|
|
Me@25
|
105 if(!retCode){printf("ERROR creating thread: %d\n", retCode); exit();}
|
|
Me@24
|
106 }
|
|
Me@24
|
107
|
|
Me@25
|
108
|
|
Me@0
|
109 }
|
|
Me@0
|
110
|
|
Me@0
|
111 void
|
|
Me@1
|
112 create_sched_slots( MasterEnv *masterEnv )
|
|
Me@8
|
113 { SchedSlot **schedSlots, **filledSlots;
|
|
Me@0
|
114 int i;
|
|
Me@0
|
115
|
|
Me@8
|
116 schedSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
|
|
Me@8
|
117 filledSlots = malloc( NUM_SCHED_SLOTS * sizeof(SchedSlot *) );
|
|
Me@8
|
118 masterEnv->schedSlots = schedSlots;
|
|
Me@8
|
119 masterEnv->filledSlots = filledSlots;
|
|
Me@8
|
120
|
|
Me@1
|
121 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
|
|
Me@0
|
122 {
|
|
Me@8
|
123 schedSlots[i] = malloc( sizeof(SchedSlot) );
|
|
Me@8
|
124
|
|
Me@1
|
125 //Set state to mean "handling requests done, slot needs filling"
|
|
Me@8
|
126 schedSlots[i]->workIsDone = FALSE;
|
|
Me@8
|
127 schedSlots[i]->needsProcrAssigned = TRUE;
|
|
Me@0
|
128 }
|
|
Me@0
|
129 }
|
|
Me@0
|
130
|
|
Me@8
|
131
|
|
Me@0
|
132 /*Semantic layer calls this when it want the system to start running..
|
|
Me@0
|
133 *
|
|
Me@24
|
134 *This starts the core loops running then waits for them to exit.
|
|
Me@0
|
135 */
|
|
Me@12
|
136 void
|
|
Me@24
|
137 VMS__start_the_work_then_wait_until_done()
|
|
Me@12
|
138 { int coreIdx;
|
|
Me@24
|
139 //Start the core loops running
|
|
Me@24
|
140 //===========================================================================
|
|
Me@25
|
141 TSCount startCount, endCount;
|
|
Me@24
|
142 unsigned long long count = 0, freq = 0;
|
|
Me@25
|
143 double runTime;
|
|
Me@0
|
144
|
|
Me@25
|
145 startCount = getTSCount();
|
|
Me@25
|
146
|
|
Me@25
|
147 //tell the core loop threads that setup is complete
|
|
Me@25
|
148 //get lock, to lock out any threads still starting up -- they'll see
|
|
Me@25
|
149 // that setupComplete is true before entering while loop, and so never
|
|
Me@25
|
150 // wait on the condition
|
|
Me@25
|
151 pthread_mutex_lock( _VMSMasterEnv->suspend_mutex );
|
|
Me@25
|
152 _VMSMasterEnv->setupComplete = 1;
|
|
Me@25
|
153 pthread_mutex_unlock( _VMSMasterEnv->suspend_mutex );
|
|
Me@25
|
154 pthread_cond_broadcast( _VMSMasterEnv->suspend_cond );
|
|
Me@25
|
155
|
|
Me@25
|
156
|
|
Me@24
|
157 //wait for all to complete
|
|
Me@8
|
158 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@8
|
159 {
|
|
Me@25
|
160 pthread_join( coreLoopThdHandles[coreIdx], NULL );
|
|
Me@24
|
161 }
|
|
Me@25
|
162
|
|
Me@24
|
163 //NOTE: do not clean up VMS env here -- semantic layer has to have
|
|
Me@24
|
164 // a chance to clean up its environment first, then do a call to free
|
|
Me@24
|
165 // the Master env and rest of VMS locations
|
|
Me@24
|
166
|
|
Me@24
|
167
|
|
Me@25
|
168 endCount = getTSCount();
|
|
Me@25
|
169 count = endCount - startCount;
|
|
Me@24
|
170
|
|
Me@25
|
171 runTime = (double)count / (double)TSCOUNT_FREQ;
|
|
Me@25
|
172
|
|
Me@25
|
173 printf("\n Time startup to shutdown: %f\n", runTime); fflush( stdin );
|
|
Me@8
|
174 }
|
|
Me@0
|
175
|
|
Me@0
|
176
|
|
Me@0
|
177
|
|
Me@8
|
178 /*Create stack, then create __cdecl structure on it and put initialData and
|
|
Me@8
|
179 * pointer to the new structure instance into the parameter positions on
|
|
Me@8
|
180 * the stack
|
|
Me@8
|
181 *Then put function pointer into nextInstrPt -- the stack is setup in std
|
|
Me@8
|
182 * call structure, so jumping to function ptr is same as a GCC generated
|
|
Me@8
|
183 * function call
|
|
Me@8
|
184 *No need to save registers on old stack frame, because there's no old
|
|
Me@8
|
185 * animator state to return to --
|
|
Me@8
|
186 *
|
|
Me@8
|
187 */
|
|
Me@8
|
188 VirtProcr *
|
|
Me@8
|
189 VMS__create_procr( VirtProcrFnPtr fnPtr, void *initialData )
|
|
Me@8
|
190 { VirtProcr *newPr;
|
|
Me@8
|
191 char *stackLocs, *stackPtr;
|
|
Me@8
|
192
|
|
Me@8
|
193 newPr = malloc( sizeof(VirtProcr) );
|
|
Me@12
|
194 newPr->procrID = numProcrsCreated++;
|
|
Me@8
|
195 newPr->nextInstrPt = fnPtr;
|
|
Me@8
|
196 newPr->initialData = initialData;
|
|
Me@8
|
197
|
|
Me@14
|
198 //fnPtr takes two params -- void *initData & void *animProcr
|
|
Me@8
|
199 //alloc stack locations, make stackPtr be the highest addr minus room
|
|
Me@14
|
200 // for 2 params + return addr. Return addr (NULL) is in loc pointed to
|
|
Me@14
|
201 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above
|
|
Me@22
|
202 stackLocs = malloc( VIRT_PROCR_STACK_SIZE );
|
|
Me@22
|
203 newPr->startOfStack = stackLocs;
|
|
Me@22
|
204 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 );
|
|
Me@8
|
205 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp
|
|
Me@22
|
206 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer
|
|
Me@14
|
207 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left
|
|
Me@8
|
208 newPr->stackPtr = stackPtr; //core loop will switch to this, then
|
|
Me@8
|
209 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr
|
|
Me@8
|
210
|
|
Me@8
|
211 return newPr;
|
|
Me@8
|
212 }
|
|
Me@8
|
213
|
|
Me@8
|
214
|
|
Me@0
|
215 /*there is a label inside this function -- save the addr of this label in
|
|
Me@0
|
216 * the callingPr struc, as the pick-up point from which to start the next
|
|
Me@0
|
217 * work-unit for that procr. If turns out have to save registers, then
|
|
Me@0
|
218 * save them in the procr struc too. Then do assembly jump to the CoreLoop's
|
|
Me@0
|
219 * "done with work-unit" label. The procr struc is in the request in the
|
|
Me@0
|
220 * slave that animated the just-ended work-unit, so all the state is saved
|
|
Me@0
|
221 * there, and will get passed along, inside the request handler, to the
|
|
Me@0
|
222 * next work-unit for that procr.
|
|
Me@0
|
223 */
|
|
Me@8
|
224 void
|
|
Me@22
|
225 VMS__suspend_procr( VirtProcr *callingPr )
|
|
Me@14
|
226 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr;
|
|
Me@14
|
227 void *coreLoopFramePtr;
|
|
Me@0
|
228
|
|
Me@14
|
229 //The request to master will cause this suspended virt procr to get
|
|
Me@14
|
230 // scheduled again at some future point -- to resume, core loop jumps
|
|
Me@14
|
231 // to the resume point (below), which causes restore of saved regs and
|
|
Me@14
|
232 // "return" from this call.
|
|
Me@1
|
233 callingPr->nextInstrPt = &&ResumePt;
|
|
Me@1
|
234
|
|
Me@1
|
235 //return ownership of the virt procr and sched slot to Master virt pr
|
|
Me@1
|
236 callingPr->schedSlot->workIsDone = TRUE;
|
|
Me@14
|
237 // coreIdx = callingPr->coreAnimatedBy;
|
|
Me@1
|
238
|
|
Me@18
|
239 stackPtrAddr = &(callingPr->stackPtr);
|
|
Me@18
|
240 framePtrAddr = &(callingPr->framePtr);
|
|
Me@14
|
241
|
|
Me@14
|
242 jmpPt = callingPr->coreLoopStartPt;
|
|
Me@14
|
243 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only
|
|
Me@18
|
244 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety
|
|
Me@1
|
245
|
|
Me@14
|
246 //Save the virt procr's stack and frame ptrs, restore coreloop's frame
|
|
Me@14
|
247 // ptr, then jump back to "start" of core loop
|
|
Me@22
|
248 //Note, GCC compiles to assembly that saves esp and ebp in the stack
|
|
Me@22
|
249 // frame -- so have to explicitly do assembly that saves to memory
|
|
Me@18
|
250 asm volatile("movl %0, %%eax; \
|
|
Me@18
|
251 movl %%esp, (%%eax); \
|
|
Me@18
|
252 movl %1, %%eax; \
|
|
Me@18
|
253 movl %%ebp, (%%eax); \
|
|
Me@18
|
254 movl %2, %%eax; \
|
|
Me@18
|
255 movl %3, %%esp; \
|
|
Me@18
|
256 movl %4, %%ebp; \
|
|
Me@18
|
257 jmp %%eax " \
|
|
Me@18
|
258 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr) \
|
|
Me@18
|
259 /* inputs */ : "g" (jmpPt), "g"(coreLoopStackPtr), "g"(coreLoopFramePtr)\
|
|
Me@18
|
260 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi","%esi" \
|
|
Me@12
|
261 ); //list everything as clobbered to force GCC to save all
|
|
Me@12
|
262 // live vars that are in regs on stack before this
|
|
Me@12
|
263 // assembly, so that stack pointer is correct, before jmp
|
|
Me@1
|
264
|
|
Me@1
|
265 ResumePt:
|
|
Me@0
|
266 return;
|
|
Me@0
|
267 }
|
|
Me@0
|
268
|
|
Me@22
|
269
|
|
Me@22
|
270
|
|
Me@22
|
271 /*This is equivalent to "jump back to core loop" -- it's mainly only used
|
|
Me@22
|
272 * just after adding dissipate request to a processor -- so the semantic
|
|
Me@22
|
273 * layer is the only place it will be seen and/or used.
|
|
Me@22
|
274 *
|
|
Me@22
|
275 *It does almost the same thing as suspend, except don't need to save the
|
|
Me@22
|
276 * stack nor set the nextInstrPt
|
|
Me@22
|
277 *
|
|
Me@22
|
278 *As of June 30, 2010 just implementing as a call to suspend -- just sugar
|
|
Me@22
|
279 */
|
|
Me@8
|
280 void
|
|
Me@22
|
281 VMS__return_from_fn( VirtProcr *animatingPr )
|
|
Me@8
|
282 {
|
|
Me@22
|
283 VMS__suspend_procr( animatingPr );
|
|
Me@1
|
284 }
|
|
Me@1
|
285
|
|
Me@22
|
286
|
|
Me@22
|
287 /*Not sure yet the form going to put "dissipate" in, so this is the third
|
|
Me@22
|
288 * possibility -- the semantic layer can just make a macro that looks like
|
|
Me@22
|
289 * a call to its name, then expands to a call to this.
|
|
Me@8
|
290 *
|
|
Me@22
|
291 *As of June 30, 2010 this looks like the top choice..
|
|
Me@8
|
292 *
|
|
Me@22
|
293 *This adds a request to dissipate, then suspends the processor so that the
|
|
Me@22
|
294 * request handler will receive the request. The request handler is what
|
|
Me@22
|
295 * does the work of freeing memory and removing the processor from the
|
|
Me@22
|
296 * semantic environment's data structures.
|
|
Me@22
|
297 *The request handler also is what figures out when to shutdown the VMS
|
|
Me@22
|
298 * system -- which causes all the core loop threads to die, and returns from
|
|
Me@22
|
299 * the call that started up VMS to perform the work.
|
|
Me@22
|
300 *
|
|
Me@22
|
301 *This form is a bit misleading to understand if one is trying to figure out
|
|
Me@22
|
302 * how VMS works -- it looks like a normal function call, but inside it
|
|
Me@22
|
303 * sends a request to the request handler and suspends the processor, which
|
|
Me@22
|
304 * jumps out of the VMS__dissipate_procr function, and out of all nestings
|
|
Me@22
|
305 * above it, transferring the work of dissipating to the request handler,
|
|
Me@22
|
306 * which then does the actual work -- causing the processor that animated
|
|
Me@22
|
307 * the call of this function to disappear and the "hanging" state of this
|
|
Me@22
|
308 * function to just poof into thin air -- the virtual processor's trace
|
|
Me@22
|
309 * never returns from this call, but instead the virtual processor's trace
|
|
Me@22
|
310 * gets suspended in this call and all the virt processor's state disap-
|
|
Me@22
|
311 * pears -- making that suspend the last thing in the virt procr's trace.
|
|
Me@8
|
312 */
|
|
Me@8
|
313 void
|
|
Me@22
|
314 VMS__dissipate_procr( VirtProcr *procrToDissipate )
|
|
Me@22
|
315 { VMSReqst *req;
|
|
Me@22
|
316
|
|
Me@22
|
317 req = malloc( sizeof(VMSReqst) );
|
|
Me@22
|
318 // req->virtProcrFrom = callingPr;
|
|
Me@22
|
319 req->reqType = dissipate;
|
|
Me@22
|
320 req->nextReqst = procrToDissipate->requests;
|
|
Me@22
|
321 procrToDissipate->requests = req;
|
|
Me@22
|
322
|
|
Me@22
|
323 VMS__suspend_procr( procrToDissipate );
|
|
Me@22
|
324 }
|
|
Me@22
|
325
|
|
Me@22
|
326
|
|
Me@22
|
327 /*This inserts the semantic-layer's request data into standard VMS carrier
|
|
Me@22
|
328 */
|
|
Me@22
|
329 inline void
|
|
Me@24
|
330 VMS__add_sem_request( void *semReqData, VirtProcr *callingPr )
|
|
Me@22
|
331 { VMSReqst *req;
|
|
Me@22
|
332
|
|
Me@22
|
333 req = malloc( sizeof(VMSReqst) );
|
|
Me@22
|
334 // req->virtProcrFrom = callingPr;
|
|
Me@22
|
335 req->reqType = semantic;
|
|
Me@22
|
336 req->semReqData = semReqData;
|
|
Me@22
|
337 req->nextReqst = callingPr->requests;
|
|
Me@22
|
338 callingPr->requests = req;
|
|
Me@22
|
339 }
|
|
Me@22
|
340
|
|
Me@22
|
341
|
|
Me@22
|
342
|
|
Me@22
|
343 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
|
|
Me@22
|
344 // of a request -- IE call with both a virt procr and a fn-ptr to request
|
|
Me@22
|
345 // freer (or maybe put request freer as a field in virt procr?)
|
|
Me@22
|
346 void
|
|
Me@22
|
347 VMS__remove_and_free_top_request( VirtProcr *procrWithReq )
|
|
Me@22
|
348 { VMSReqst *req;
|
|
Me@22
|
349
|
|
Me@22
|
350 req = procrWithReq->requests;
|
|
Me@22
|
351 procrWithReq->requests = procrWithReq->requests->nextReqst;
|
|
Me@22
|
352 free( req );
|
|
Me@22
|
353 }
|
|
Me@22
|
354
|
|
Me@24
|
355
|
|
Me@24
|
356 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion
|
|
Me@24
|
357 // of a request -- IE call with both a virt procr and a fn-ptr to request
|
|
Me@24
|
358 // freer (also maybe put sem request freer as a field in virt procr?)
|
|
Me@22
|
359 void
|
|
Me@24
|
360 VMS__free_request( VMSReqst *req )
|
|
Me@24
|
361 {
|
|
Me@24
|
362 free( req );
|
|
Me@24
|
363 }
|
|
Me@24
|
364
|
|
Me@24
|
365 VMSReqst *
|
|
Me@24
|
366 VMS__take_top_request_from( VirtProcr *procrWithReq )
|
|
Me@24
|
367 { VMSReqst *req;
|
|
Me@24
|
368
|
|
Me@24
|
369 req = procrWithReq->requests;
|
|
Me@24
|
370 if( req == NULL ) return req;
|
|
Me@24
|
371
|
|
Me@24
|
372 procrWithReq->requests = procrWithReq->requests->nextReqst;
|
|
Me@24
|
373 return req;
|
|
Me@24
|
374 }
|
|
Me@24
|
375
|
|
Me@24
|
376 inline int
|
|
Me@24
|
377 VMS__isSemanticReqst( VMSReqst *req )
|
|
Me@22
|
378 {
|
|
Me@24
|
379 return ( req->reqType == semantic );
|
|
Me@24
|
380 }
|
|
Me@22
|
381
|
|
Me@24
|
382
|
|
Me@24
|
383 inline void *
|
|
Me@24
|
384 VMS__take_sem_reqst_from( VMSReqst *req )
|
|
Me@24
|
385 {
|
|
Me@24
|
386 return req->semReqData;
|
|
Me@24
|
387 }
|
|
Me@24
|
388
|
|
Me@24
|
389 inline int
|
|
Me@24
|
390 VMS__isDissipateReqst( VMSReqst *req )
|
|
Me@24
|
391 {
|
|
Me@24
|
392 return ( req->reqType == dissipate );
|
|
Me@24
|
393 }
|
|
Me@24
|
394
|
|
Me@24
|
395 inline int
|
|
Me@24
|
396 VMS__isCreateReqst( VMSReqst *req )
|
|
Me@24
|
397 {
|
|
Me@24
|
398 return ( req->reqType == regCreated );
|
|
Me@24
|
399 }
|
|
Me@24
|
400
|
|
Me@24
|
401 void
|
|
Me@24
|
402 VMS__send_register_new_procr_request(VirtProcr *newPr, VirtProcr *reqstingPr)
|
|
Me@24
|
403 { VMSReqst *req;
|
|
Me@24
|
404
|
|
Me@24
|
405 req = malloc( sizeof(VMSReqst) );
|
|
Me@24
|
406 req->reqType = regCreated;
|
|
Me@24
|
407 req->semReqData = newPr;
|
|
Me@24
|
408 req->nextReqst = reqstingPr->requests;
|
|
Me@24
|
409 reqstingPr->requests = req;
|
|
Me@24
|
410
|
|
Me@24
|
411 VMS__suspend_procr( reqstingPr );
|
|
Me@22
|
412 }
|
|
Me@22
|
413
|
|
Me@22
|
414
|
|
Me@22
|
415 /*The semantic layer figures out when the work is done ( perhaps by a call
|
|
Me@22
|
416 * in the application to "work all done", or perhaps all the virtual
|
|
Me@22
|
417 * processors have dissipated.. a.s.o. )
|
|
Me@22
|
418 *
|
|
Me@22
|
419 *The semantic layer is responsible for making sure all work has fully
|
|
Me@22
|
420 * completed before using this to shutdown the VMS system.
|
|
Me@22
|
421 *
|
|
Me@22
|
422 *After the semantic layer has determined it wants to shut down, the
|
|
Me@22
|
423 * next time the Master Loop calls the scheduler plug-in, the scheduler
|
|
Me@22
|
424 * then calls this function and returns the virtual processor it gets back.
|
|
Me@22
|
425 *
|
|
Me@22
|
426 *When the shut-down processor runs, it first frees all locations malloc'd to
|
|
Me@22
|
427 * the VMS system (that wasn't
|
|
Me@22
|
428 * specified as return-locations). Then it creates one core-loop shut-down
|
|
Me@22
|
429 * processor for each core loop and puts them all into the workQ. When a
|
|
Me@22
|
430 * core loop animates a core loop shut-down processor, it causes exit-thread
|
|
Me@22
|
431 * to run, and when all core loop threads have exited, then the "wait for
|
|
Me@22
|
432 * work to finish" in the main thread is woken, and the function-call that
|
|
Me@22
|
433 * started all the work returns.
|
|
Me@22
|
434 *
|
|
Me@22
|
435 *The function animated by this processor performs the shut-down work.
|
|
Me@22
|
436 */
|
|
Me@22
|
437 VirtProcr *
|
|
Me@22
|
438 VMS__create_the_shutdown_procr()
|
|
Me@22
|
439 {
|
|
Me@22
|
440 return VMS__create_procr( &shutdownFn, NULL );
|
|
Me@22
|
441 }
|
|
Me@22
|
442
|
|
Me@22
|
443
|
|
Me@24
|
444 /*This must be called by the request handler plugin -- it cannot be called
|
|
Me@24
|
445 * from the semantic library "dissipate processor" function -- instead, the
|
|
Me@24
|
446 * semantic layer has to generate a request for the plug-in to call this
|
|
Me@24
|
447 * function.
|
|
Me@24
|
448 *The reason is that this frees the virtual processor's stack -- which is
|
|
Me@24
|
449 * still in use inside semantic library calls!
|
|
Me@24
|
450 *
|
|
Me@24
|
451 *This frees or recycles all the state owned by and comprising the VMS
|
|
Me@24
|
452 * portion of the animating virtual procr. The request handler must first
|
|
Me@24
|
453 * free any semantic data created for the processor that didn't use the
|
|
Me@24
|
454 * VMS_malloc mechanism. Then it calls this, which first asks the malloc
|
|
Me@24
|
455 * system to disown any state that did use VMS_malloc, and then frees the
|
|
Me@24
|
456 * statck and the processor-struct itself.
|
|
Me@24
|
457 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd
|
|
Me@24
|
458 * state, then that state gets freed (or sent to recycling) as a side-effect
|
|
Me@24
|
459 * of dis-owning it.
|
|
Me@24
|
460 */
|
|
Me@24
|
461 void
|
|
Me@24
|
462 VMS__free_procr_locs( VirtProcr *animatingPr )
|
|
Me@24
|
463 {
|
|
Me@24
|
464 //dis-own all locations owned by this processor, causing to be freed
|
|
Me@24
|
465 // any locations that it is (was) sole owner of
|
|
Me@24
|
466 //TODO: implement VMS__malloc system, including "give up ownership"
|
|
Me@24
|
467
|
|
Me@24
|
468 //The dissipate request might still be attached, so remove and free it
|
|
Me@24
|
469 VMS__remove_and_free_top_request( animatingPr );
|
|
Me@24
|
470 free( animatingPr->startOfStack );
|
|
Me@24
|
471
|
|
Me@24
|
472 //NOTE: initialData was given to the processor, so should either have
|
|
Me@24
|
473 // been alloc'd with VMS__malloc, or freed by the level above animPr.
|
|
Me@24
|
474 //So, all that's left to free here is the stack and the VirtProcr struc
|
|
Me@24
|
475 // itself
|
|
Me@24
|
476 free( animatingPr->startOfStack );
|
|
Me@24
|
477 free( animatingPr );
|
|
Me@24
|
478 }
|
|
Me@24
|
479
|
|
Me@24
|
480
|
|
Me@24
|
481
|
|
Me@22
|
482 /*This is the function run by the special "shut-down" processor
|
|
Me@22
|
483 *
|
|
Me@22
|
484 *The _VMSMasterEnv is needed by this shut down function, so the "wait"
|
|
Me@22
|
485 * function run in the main loop has to free it, and the thread-related
|
|
Me@22
|
486 * locations (coreLoopThdParams a.s.o.).
|
|
Me@22
|
487 *However, the semantic environment and all data malloc'd to VMS can be
|
|
Me@22
|
488 * freed here.
|
|
Me@22
|
489 *
|
|
Me@22
|
490 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the
|
|
Me@22
|
491 * locations it needs -- they will be automatically freed by the standard
|
|
Me@22
|
492 * "free all owned locations"
|
|
Me@22
|
493 *
|
|
Me@22
|
494 *Free any locations malloc'd to the VMS system (that weren't
|
|
Me@22
|
495 * specified as return-locations).
|
|
Me@22
|
496 *Then create one core-loop shut-down processor for each core loop and puts
|
|
Me@22
|
497 * them all into the workQ.
|
|
Me@22
|
498 */
|
|
Me@22
|
499 void
|
|
Me@22
|
500 shutdownFn( void *dummy, VirtProcr *animatingPr )
|
|
Me@8
|
501 { int coreIdx;
|
|
Me@14
|
502 VirtProcr *shutDownPr;
|
|
Me@22
|
503 CASQueueStruc *workQ = _VMSWorkQ;
|
|
Me@22
|
504
|
|
Me@22
|
505 //free all the locations owned within the VMS system
|
|
Me@22
|
506 //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt
|
|
Me@22
|
507
|
|
Me@22
|
508 //make the core loop shut-down processors and put them into the workQ
|
|
Me@8
|
509 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ )
|
|
Me@8
|
510 {
|
|
Me@14
|
511 shutDownPr = VMS__create_procr( NULL, NULL );
|
|
Me@14
|
512 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt;
|
|
Me@22
|
513 writeCASQ( shutDownPr, workQ );
|
|
Me@8
|
514 }
|
|
Me@22
|
515
|
|
Me@22
|
516 //This is an issue: the animating processor of this function may not
|
|
Me@22
|
517 // get its request handled before all the cores have shutdown.
|
|
Me@22
|
518 //TODO: after all the threads stop, clean out the MasterEnv, the
|
|
Me@22
|
519 // SemanticEnv, and the workQ before returning.
|
|
Me@24
|
520 VMS__dissipate_procr( animatingPr ); //will never come back from this
|
|
Me@12
|
521 }
|
|
Me@12
|
522
|
|
Me@12
|
523
|
|
Me@24
|
524 /*This has to free anything allocated during VMS_init, and any other alloc'd
|
|
Me@24
|
525 * locations that might be left over.
|
|
Me@24
|
526 */
|
|
Me@24
|
527 void
|
|
Me@24
|
528 VMS__shutdown()
|
|
Me@24
|
529 { int i;
|
|
Me@24
|
530
|
|
Me@24
|
531 free( _VMSWorkQ );
|
|
Me@24
|
532 free( _VMSMasterEnv->filledSlots );
|
|
Me@24
|
533 for( i = 0; i < NUM_SCHED_SLOTS; i++ )
|
|
Me@24
|
534 {
|
|
Me@24
|
535 free( _VMSMasterEnv->schedSlots[i] );
|
|
Me@24
|
536 }
|
|
Me@24
|
537
|
|
Me@24
|
538 free( _VMSMasterEnv->schedSlots);
|
|
Me@24
|
539 VMS__free_procr_locs( _VMSMasterEnv->masterVirtPr );
|
|
Me@24
|
540
|
|
Me@24
|
541 free( _VMSMasterEnv );
|
|
Me@24
|
542 }
|
|
Me@24
|
543
|
|
Me@24
|
544
|
|
Me@24
|
545 //===========================================================================
|
|
Me@12
|
546
|
|
Me@12
|
547 inline TSCount getTSCount()
|
|
Me@12
|
548 { unsigned int low, high;
|
|
Me@12
|
549 TSCount out;
|
|
Me@12
|
550
|
|
Me@12
|
551 saveTimeStampCountInto( low, high );
|
|
Me@12
|
552 out = high;
|
|
Me@12
|
553 out = (out << 32) + low;
|
|
Me@12
|
554 return out;
|
|
Me@12
|
555 }
|
|
Me@12
|
556
|