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