Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
comparison VMS.c @ 22:1dbc7f6e3e67
Full VMS test -- works
| author | Me |
|---|---|
| date | Wed, 30 Jun 2010 13:10:59 -0700 |
| parents | 734c665500e4 |
| children | 2b161e1a50ee |
comparison
equal
deleted
inserted
replaced
| 5:e76b9b55566c | 6:641e4ced0df8 |
|---|---|
| 9 #include <malloc.h> | 9 #include <malloc.h> |
| 10 | 10 |
| 11 #include "VMS.h" | 11 #include "VMS.h" |
| 12 #include "Queue_impl/BlockingQueue.h" | 12 #include "Queue_impl/BlockingQueue.h" |
| 13 | 13 |
| 14 | |
| 15 //=========================================================================== | |
| 16 void | |
| 17 shutdownFn( void *dummy, VirtProcr *dummy2 ); | |
| 18 | |
| 19 void | |
| 20 create_sched_slots( MasterEnv *masterEnv ); | |
| 21 | |
| 22 //=========================================================================== | |
| 14 | 23 |
| 15 /*Setup has two phases: | 24 /*Setup has two phases: |
| 16 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts | 25 * 1) Semantic layer first calls init_VMS, which creates masterEnv, and puts |
| 17 * the master virt procr into the work-queue, ready for first "call" | 26 * the master virt procr into the work-queue, ready for first "call" |
| 18 * 2) Semantic layer then does its own init, which creates the seed virt | 27 * 2) Semantic layer then does its own init, which creates the seed virt |
| 34 *The semantic layer creates the initial virt procr(s), and adds its | 43 *The semantic layer creates the initial virt procr(s), and adds its |
| 35 * own environment to masterEnv, and fills in the pointers to | 44 * own environment to masterEnv, and fills in the pointers to |
| 36 * the requestHandler and slaveScheduler plug-in functions | 45 * the requestHandler and slaveScheduler plug-in functions |
| 37 */ | 46 */ |
| 38 | 47 |
| 39 void | |
| 40 create_sched_slots( MasterEnv *masterEnv ); | |
| 41 | |
| 42 | |
| 43 /*This allocates VMS data structures, populates the master VMSProc, | 48 /*This allocates VMS data structures, populates the master VMSProc, |
| 44 * and master environment, and returns the master environment to the semantic | 49 * and master environment, and returns the master environment to the semantic |
| 45 * layer. | 50 * layer. |
| 46 */ | 51 */ |
| 47 void | 52 void |
| 63 | 68 |
| 64 //Set slot 0 to be the master virt procr & set flags just in case | 69 //Set slot 0 to be the master virt procr & set flags just in case |
| 65 masterEnv->schedSlots[0]->needsProcrAssigned = FALSE; //says don't touch | 70 masterEnv->schedSlots[0]->needsProcrAssigned = FALSE; //says don't touch |
| 66 masterEnv->schedSlots[0]->workIsDone = FALSE; //says don't touch | 71 masterEnv->schedSlots[0]->workIsDone = FALSE; //says don't touch |
| 67 masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr; | 72 masterEnv->schedSlots[0]->procrAssignedToSlot = masterEnv->masterVirtPr; |
| 68 | 73 masterEnv->masterVirtPr->schedSlot = masterEnv->schedSlots[0]; |
| 74 | |
| 69 //First core loop to start up gets this, which will schedule seed Pr | 75 //First core loop to start up gets this, which will schedule seed Pr |
| 70 //TODO: debug: check address of masterVirtPr | 76 //TODO: debug: check address of masterVirtPr |
| 71 //TODO: commented out for debugging -- put it back in!! | 77 writeCASQ( masterEnv->masterVirtPr, workQ ); |
| 72 // writeCASQ( masterEnv->masterVirtPr, workQ ); | |
| 73 | 78 |
| 74 numProcrsCreated = 1; | 79 numProcrsCreated = 1; |
| 75 } | 80 } |
| 76 | 81 |
| 77 | 82 |
| 148 | 153 |
| 149 //fnPtr takes two params -- void *initData & void *animProcr | 154 //fnPtr takes two params -- void *initData & void *animProcr |
| 150 //alloc stack locations, make stackPtr be the highest addr minus room | 155 //alloc stack locations, make stackPtr be the highest addr minus room |
| 151 // for 2 params + return addr. Return addr (NULL) is in loc pointed to | 156 // for 2 params + return addr. Return addr (NULL) is in loc pointed to |
| 152 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above | 157 // by stackPtr, initData at stackPtr + 4 bytes, animatingPr just above |
| 153 stackLocs = malloc( 0x100000 ); //1 meg stack -- default Win thread's size | 158 stackLocs = malloc( VIRT_PROCR_STACK_SIZE ); |
| 154 stackPtr = ( (char *)stackLocs + 0x100000 - 0x10 ); | 159 newPr->startOfStack = stackLocs; |
| 160 stackPtr = ( (char *)stackLocs + VIRT_PROCR_STACK_SIZE - 0x10 ); | |
| 155 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp | 161 //setup __cdecl on stack -- coreloop will switch to stackPtr before jmp |
| 156 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer | 162 *( (int *)stackPtr + 2 ) = (int) newPr; //rightmost param -- 32bit pointer |
| 157 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left | 163 *( (int *)stackPtr + 1 ) = (int) initialData; //next param to left |
| 158 newPr->stackPtr = stackPtr; //core loop will switch to this, then | 164 newPr->stackPtr = stackPtr; //core loop will switch to this, then |
| 159 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr | 165 newPr->framePtr = stackPtr; //suspend loop will save new stack & frame ptr |
| 160 | 166 |
| 161 return newPr; | 167 return newPr; |
| 162 } | 168 } |
| 163 | 169 |
| 164 | |
| 165 /*This inserts the semantic-layer's data into the standard VMS carrier | |
| 166 */ | |
| 167 inline void | |
| 168 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr ) | |
| 169 { SlaveReqst *req; | |
| 170 | |
| 171 req = malloc( sizeof(SlaveReqst) ); | |
| 172 req->slaveFrom = callingPr; | |
| 173 req->semReqData = semReqData; | |
| 174 req->nextRequest = callingPr->requests; | |
| 175 callingPr->requests = req; | |
| 176 } | |
| 177 | 170 |
| 178 /*there is a label inside this function -- save the addr of this label in | 171 /*there is a label inside this function -- save the addr of this label in |
| 179 * the callingPr struc, as the pick-up point from which to start the next | 172 * the callingPr struc, as the pick-up point from which to start the next |
| 180 * work-unit for that procr. If turns out have to save registers, then | 173 * work-unit for that procr. If turns out have to save registers, then |
| 181 * save them in the procr struc too. Then do assembly jump to the CoreLoop's | 174 * save them in the procr struc too. Then do assembly jump to the CoreLoop's |
| 183 * slave that animated the just-ended work-unit, so all the state is saved | 176 * slave that animated the just-ended work-unit, so all the state is saved |
| 184 * there, and will get passed along, inside the request handler, to the | 177 * there, and will get passed along, inside the request handler, to the |
| 185 * next work-unit for that procr. | 178 * next work-unit for that procr. |
| 186 */ | 179 */ |
| 187 void | 180 void |
| 188 VMS__suspend_processor( VirtProcr *callingPr ) | 181 VMS__suspend_procr( VirtProcr *callingPr ) |
| 189 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr; | 182 { void *jmpPt, *stackPtrAddr, *framePtrAddr, *coreLoopStackPtr; |
| 190 void *coreLoopFramePtr; | 183 void *coreLoopFramePtr; |
| 191 int coreIdx; | |
| 192 | 184 |
| 193 //The request to master will cause this suspended virt procr to get | 185 //The request to master will cause this suspended virt procr to get |
| 194 // scheduled again at some future point -- to resume, core loop jumps | 186 // scheduled again at some future point -- to resume, core loop jumps |
| 195 // to the resume point (below), which causes restore of saved regs and | 187 // to the resume point (below), which causes restore of saved regs and |
| 196 // "return" from this call. | 188 // "return" from this call. |
| 207 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only | 199 coreLoopFramePtr = callingPr->coreLoopFramePtr;//need this only |
| 208 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety | 200 coreLoopStackPtr = callingPr->coreLoopStackPtr;//shouldn't need -- safety |
| 209 | 201 |
| 210 //Save the virt procr's stack and frame ptrs, restore coreloop's frame | 202 //Save the virt procr's stack and frame ptrs, restore coreloop's frame |
| 211 // ptr, then jump back to "start" of core loop | 203 // ptr, then jump back to "start" of core loop |
| 204 //Note, GCC compiles to assembly that saves esp and ebp in the stack | |
| 205 // frame -- so have to explicitly do assembly that saves to memory | |
| 212 asm volatile("movl %0, %%eax; \ | 206 asm volatile("movl %0, %%eax; \ |
| 213 movl %%esp, (%%eax); \ | 207 movl %%esp, (%%eax); \ |
| 214 movl %1, %%eax; \ | 208 movl %1, %%eax; \ |
| 215 movl %%ebp, (%%eax); \ | 209 movl %%ebp, (%%eax); \ |
| 216 movl %2, %%eax; \ | 210 movl %2, %%eax; \ |
| 226 | 220 |
| 227 ResumePt: | 221 ResumePt: |
| 228 return; | 222 return; |
| 229 } | 223 } |
| 230 | 224 |
| 231 void | 225 |
| 232 VMS__dissipate_animating_processor( VirtProcr *animatingPr ) | 226 |
| 227 /*This is equivalent to "jump back to core loop" -- it's mainly only used | |
| 228 * just after adding dissipate request to a processor -- so the semantic | |
| 229 * layer is the only place it will be seen and/or used. | |
| 230 * | |
| 231 *It does almost the same thing as suspend, except don't need to save the | |
| 232 * stack nor set the nextInstrPt | |
| 233 * | |
| 234 *As of June 30, 2010 just implementing as a call to suspend -- just sugar | |
| 235 */ | |
| 236 void | |
| 237 VMS__return_from_fn( VirtProcr *animatingPr ) | |
| 233 { | 238 { |
| 234 | 239 VMS__suspend_procr( animatingPr ); |
| 235 } | 240 } |
| 236 | 241 |
| 237 /*This runs in main thread -- so can only signal to the core loop to shut | 242 |
| 238 * itself down -- | 243 /*Not sure yet the form going to put "dissipate" in, so this is the third |
| 239 * | 244 * possibility -- the semantic layer can just make a macro that looks like |
| 240 *Want the master to decide when to shut down -- when semantic layer tells it | 245 * a call to its name, then expands to a call to this. |
| 241 * to -- say, when all the application-virtual processors have dissipated. | 246 * |
| 242 * | 247 *As of June 30, 2010 this looks like the top choice.. |
| 243 *Maybe return a special code from scheduling plug-in.. master checks and | 248 * |
| 244 * when sees, it shuts down the core loops -- does this by scheduling a | 249 *This adds a request to dissipate, then suspends the processor so that the |
| 245 * special virt processor whose next instr pt is the core-end label. | 250 * request handler will receive the request. The request handler is what |
| 246 */ | 251 * does the work of freeing memory and removing the processor from the |
| 247 void | 252 * semantic environment's data structures. |
| 248 VMS__shutdown() | 253 *The request handler also is what figures out when to shutdown the VMS |
| 254 * system -- which causes all the core loop threads to die, and returns from | |
| 255 * the call that started up VMS to perform the work. | |
| 256 * | |
| 257 *This form is a bit misleading to understand if one is trying to figure out | |
| 258 * how VMS works -- it looks like a normal function call, but inside it | |
| 259 * sends a request to the request handler and suspends the processor, which | |
| 260 * jumps out of the VMS__dissipate_procr function, and out of all nestings | |
| 261 * above it, transferring the work of dissipating to the request handler, | |
| 262 * which then does the actual work -- causing the processor that animated | |
| 263 * the call of this function to disappear and the "hanging" state of this | |
| 264 * function to just poof into thin air -- the virtual processor's trace | |
| 265 * never returns from this call, but instead the virtual processor's trace | |
| 266 * gets suspended in this call and all the virt processor's state disap- | |
| 267 * pears -- making that suspend the last thing in the virt procr's trace. | |
| 268 */ | |
| 269 void | |
| 270 VMS__dissipate_procr( VirtProcr *procrToDissipate ) | |
| 271 { VMSReqst *req; | |
| 272 | |
| 273 req = malloc( sizeof(VMSReqst) ); | |
| 274 // req->virtProcrFrom = callingPr; | |
| 275 req->reqType = dissipate; | |
| 276 req->nextReqst = procrToDissipate->requests; | |
| 277 procrToDissipate->requests = req; | |
| 278 | |
| 279 VMS__suspend_procr( procrToDissipate ); | |
| 280 } | |
| 281 | |
| 282 | |
| 283 /*This inserts the semantic-layer's request data into standard VMS carrier | |
| 284 */ | |
| 285 inline void | |
| 286 VMS__send_sem_request( void *semReqData, VirtProcr *callingPr ) | |
| 287 { VMSReqst *req; | |
| 288 | |
| 289 req = malloc( sizeof(VMSReqst) ); | |
| 290 // req->virtProcrFrom = callingPr; | |
| 291 req->reqType = semantic; | |
| 292 req->semReqData = semReqData; | |
| 293 req->nextReqst = callingPr->requests; | |
| 294 callingPr->requests = req; | |
| 295 } | |
| 296 | |
| 297 | |
| 298 /*This creates a request of type "dissipate" -- which will cause the virt | |
| 299 * processor's state and owned locations to be freed | |
| 300 */ | |
| 301 inline void | |
| 302 VMS__send_dissipate_request( VirtProcr *procrToDissipate ) | |
| 303 { VMSReqst *req; | |
| 304 | |
| 305 req = malloc( sizeof(VMSReqst) ); | |
| 306 // req->virtProcrFrom = callingPr; | |
| 307 req->reqType = dissipate; | |
| 308 req->nextReqst = procrToDissipate->requests; | |
| 309 procrToDissipate->requests = req; | |
| 310 } | |
| 311 | |
| 312 | |
| 313 //TODO: add a semantic-layer supplied "freer" for the semantic-data portion | |
| 314 // of a request -- IE call with both a virt procr and a fn-ptr to request | |
| 315 // freer (or maybe put request freer as a field in virt procr?) | |
| 316 void | |
| 317 VMS__remove_and_free_top_request( VirtProcr *procrWithReq ) | |
| 318 { VMSReqst *req; | |
| 319 | |
| 320 req = procrWithReq->requests; | |
| 321 procrWithReq->requests = procrWithReq->requests->nextReqst; | |
| 322 free( req ); | |
| 323 } | |
| 324 | |
| 325 /*This must be called by the request handler plugin -- it cannot be called | |
| 326 * from the semantic library "dissipate processor" function -- instead, the | |
| 327 * semantic layer has to generate a request for the plug-in to call this | |
| 328 * function. | |
| 329 *The reason is that this frees the virtual processor's stack -- which is | |
| 330 * still in use inside semantic library calls! | |
| 331 * | |
| 332 *This frees or recycles all the state owned by and comprising the animating | |
| 333 * virtual procr. It frees any state that was malloc'd by the VMS system | |
| 334 * itself, and asks the VMS system to dis-own any VMS__malloc'd locations. | |
| 335 *If the dissipated processor is the sole (remaining) owner of VMS__malloc'd | |
| 336 * state, then that state gets freed (or sent to recycling) as a side-effect | |
| 337 * of dis-owning it. | |
| 338 */ | |
| 339 void | |
| 340 VMS__free_procr_locs( VirtProcr *animatingPr ) | |
| 341 { | |
| 342 //dis-own all locations owned by this processor, causing to be freed | |
| 343 // any locations that it is (was) sole owner of | |
| 344 //TODO: implement VMS__malloc system, including "give up ownership" | |
| 345 | |
| 346 VMS__remove_and_free_top_request( animatingPr ); | |
| 347 free( animatingPr->startOfStack ); | |
| 348 | |
| 349 //NOTE: animatingPr->semanticData should either have been allocated | |
| 350 // with VMS__malloc, or else freed in the request handler plug-in. | |
| 351 //NOTE: initialData was given to the processor, so should either have | |
| 352 // been alloc'd with VMS__malloc, or freed by the level above animPr. | |
| 353 //So, all that's left to free here is the VirtProcr struc itself | |
| 354 free( animatingPr ); | |
| 355 } | |
| 356 | |
| 357 | |
| 358 /*The semantic layer figures out when the work is done ( perhaps by a call | |
| 359 * in the application to "work all done", or perhaps all the virtual | |
| 360 * processors have dissipated.. a.s.o. ) | |
| 361 * | |
| 362 *The semantic layer is responsible for making sure all work has fully | |
| 363 * completed before using this to shutdown the VMS system. | |
| 364 * | |
| 365 *After the semantic layer has determined it wants to shut down, the | |
| 366 * next time the Master Loop calls the scheduler plug-in, the scheduler | |
| 367 * then calls this function and returns the virtual processor it gets back. | |
| 368 * | |
| 369 *When the shut-down processor runs, it first frees all locations malloc'd to | |
| 370 * the VMS system (that wasn't | |
| 371 * specified as return-locations). Then it creates one core-loop shut-down | |
| 372 * processor for each core loop and puts them all into the workQ. When a | |
| 373 * core loop animates a core loop shut-down processor, it causes exit-thread | |
| 374 * to run, and when all core loop threads have exited, then the "wait for | |
| 375 * work to finish" in the main thread is woken, and the function-call that | |
| 376 * started all the work returns. | |
| 377 * | |
| 378 *The function animated by this processor performs the shut-down work. | |
| 379 */ | |
| 380 VirtProcr * | |
| 381 VMS__create_the_shutdown_procr() | |
| 382 { | |
| 383 return VMS__create_procr( &shutdownFn, NULL ); | |
| 384 } | |
| 385 | |
| 386 | |
| 387 /*This is the function run by the special "shut-down" processor | |
| 388 * | |
| 389 *The _VMSMasterEnv is needed by this shut down function, so the "wait" | |
| 390 * function run in the main loop has to free it, and the thread-related | |
| 391 * locations (coreLoopThdParams a.s.o.). | |
| 392 *However, the semantic environment and all data malloc'd to VMS can be | |
| 393 * freed here. | |
| 394 * | |
| 395 *NOTE: the semantic plug-in is expected to use VMS__malloc to get all the | |
| 396 * locations it needs -- they will be automatically freed by the standard | |
| 397 * "free all owned locations" | |
| 398 * | |
| 399 *Free any locations malloc'd to the VMS system (that weren't | |
| 400 * specified as return-locations). | |
| 401 *Then create one core-loop shut-down processor for each core loop and puts | |
| 402 * them all into the workQ. | |
| 403 */ | |
| 404 void | |
| 405 shutdownFn( void *dummy, VirtProcr *animatingPr ) | |
| 249 { int coreIdx; | 406 { int coreIdx; |
| 250 VirtProcr *shutDownPr; | 407 VirtProcr *shutDownPr; |
| 251 | 408 CASQueueStruc *workQ = _VMSWorkQ; |
| 252 //TODO: restore the "orig" stack pointer and frame ptr saved in VMS__start | 409 |
| 253 //create a "special" virtual processor, one for each core loop that has | 410 //free all the locations owned within the VMS system |
| 254 // the "loop end" point as its "next instr" point -- when the core loop | 411 //TODO: write VMS__malloc and free.. -- take the DKU malloc as starting pt |
| 255 // jumps to animate the virt procr, the jump lands it at its own | 412 |
| 256 // shut-down code. | 413 //make the core loop shut-down processors and put them into the workQ |
| 257 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ ) | 414 for( coreIdx=0; coreIdx < NUM_CORES; coreIdx++ ) |
| 258 { | 415 { |
| 259 shutDownPr = VMS__create_procr( NULL, NULL ); | 416 shutDownPr = VMS__create_procr( NULL, NULL ); |
| 260 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt; | 417 shutDownPr->nextInstrPt = _VMSMasterEnv->coreLoopShutDownPt; |
| 418 writeCASQ( shutDownPr, workQ ); | |
| 261 } | 419 } |
| 420 | |
| 421 //This is an issue: the animating processor of this function may not | |
| 422 // get its request handled before all the cores have shutdown. | |
| 423 //TODO: after all the threads stop, clean out the MasterEnv, the | |
| 424 // SemanticEnv, and the workQ before returning. | |
| 425 VMS__send_dissipate_request( animatingPr ); | |
| 426 VMS__suspend_procr( animatingPr ); //will never come back from this | |
| 262 } | 427 } |
| 263 | 428 |
| 264 | 429 |
| 265 | 430 |
| 266 inline TSCount getTSCount() | 431 inline TSCount getTSCount() |
