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