// Create the coroutines defined in an array of ``s_coroutine_create_t`` by initializing each's ``pscostate``. When finished, return back to the routine which called it. void createCoroutines( // .. _pscocreate: // // An array of routines to create. const s_coroutineCreate_t* const pscocreate, // .. _sz_pscocreateIndex: // // The index of the first uncreated element in ``pscocreate``. size_t sz_pscocreateIndex, // .. _sz_pscocreateElements: // // The number of elements in ``pscocreate``. size_t sz_pscocreateElements) { // Keep track of the coroutine at the top of the stack. static s_coroutineState_t* pscostate_top = NULL; // Provide convenient access to the coroutine state we're creating. s_coroutineState_t* pscostate_create; // A way to get the address of the top of the stack. uint8_t u8_tos; // ``createCoroutines`` must only be called from the TOS. ASSERT(pscostate_current == pscostate_top); // Weakly check ``pscocreate`` validity. ASSERT(pscocreate != NULL); pscostate_create = pscocreate[sz_pscocreateIndex].pscostate; // The function which called this routine must not define a ``pf_coroutineMain``, since this function will (eventually) return to it. if (sz_pscocreateIndex == 0) { ASSERT(pscocreate[0].pf_coroutineMain == NULL); } else { ASSERT(pscocreate[sz_pscocreateIndex].pf_coroutineMain != NULL); } // Save the current processor state. pscostate_create->pu8_stackBase = (uint8_t*) &u8_tos; if (!setjmp(pscostate_create->jmpBuf)) { // If there are more coroutines to create above us, allocate stack for the current coroutine. if (sz_pscocreateIndex + 1 < sz_pscocreateElements) { allocStack(pscocreate, sz_pscocreateIndex, sz_pscocreateElements); ASSERT(0); } // We've created the last co-routine. Set ``pscostate_current`` if it's not valid. if (pscostate_current == NULL) { pscostate_current = pscocreate[0].pscostate; } // Jump back to the coroutine which invoked ``createCoroutines``. longjmp(pscocreate[0].pscostate->jmpBuf, 1); } // We're here because of a ``longjmp``. If this is index 0, then return to pick up where we left off; otherwise, run the co-routine's function. if (sz_pscocreateIndex != 0) { pscocreate[sz_pscocreateIndex].pf_coroutineMain(); // Coroutines should never return. ASSERT(0); } }
Fiber *allocFiber(){ if( !fibers ) init(); Fiber *fiber=freeFibers; if( !fiber ) return nullptr; if( !fiber->stack ) fiber->stack=allocStack(); freeFibers=fiber->succ; fiber->id+=MAX_FIBERS; return fiber; }
// Allocate space on the stack void allocStack( // See pscocreate_. const s_coroutineCreate_t* const pscocreate, // See sz_pscocreateIndex_. size_t sz_pscocreateIndex, // See sz_pscocreateElements_. size_t sz_pscocreateElements) { uint8_t u8_tos; if (&u8_tos - pscocreate[sz_pscocreateIndex].pscostate->pu8_stackBase < pscocreate[sz_pscocreateIndex].sz_stackMin) { allocStack(pscocreate, sz_pscocreateIndex, sz_pscocreateElements); } // We've allocated enough stack. Record this for the current coroutine. pscocreate[sz_pscocreateIndex].pscostate->pu8_stackLimit = &u8_tos; // TODO: Fill the stack with a test pattern to monitor stack usage. // Create the next coroutine. createCoroutines(pscocreate, sz_pscocreateIndex + 1, sz_pscocreateElements); }
int main(void) { int line =0; unsigned int *s; int wCol=5; clearscreen(line); writeln("Initializing OS"); writeln("Setting up OS descriptor..."); gdt_install(); pmWriteScr(wCol,32," done"); wCol++; pmWriteln(wCol,32); /*while(1);*/ //program3 starts here clearscreen(0); writeln("Running Ten processes"); front = rear = 0; next_pcb = 0; nextstack = 0; s=allocStack(); // Allocating stack createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p1); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p2); /* s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p3); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p4); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p5); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p6); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p7); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p8); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p9); s=allocStack(); createProcess((unsigned int)16,(unsigned int) 24,(unsigned int) (s+1024),(unsigned int ) 8,(unsigned int) p10); */ current = dequeue(); go(current); while(1); return 0; } /*Main ends Here */