diff --git a/src/libsrc/fiber.pla b/src/libsrc/fiber.pla index a4312b4..c9d3573 100644 --- a/src/libsrc/fiber.pla +++ b/src/libsrc/fiber.pla @@ -116,22 +116,22 @@ export def fbrInit(numPool) fbrPool = heapalloc(fbrMax * 512 + 256) if fbrPool // - // Each fiber gets 256 bytes of stack and 256 bytes - // for frame (local) data - except fiber 0 uses original frame data - // + // Each fiber gets 256 bytes of stack and 256 bytes + // for frame (local) data - except fiber 0 uses original frame data + // pool = fbrPool + 256 - for i = fbrMax downto 1 - if i < numPool - fbrState[i] = FIBER_FREE - fbrVMState[i] = pool - pool = pool + 512 - fin - next - // - // Set fiber 0 to the RUNning fiber - // - fbrState = FIBER_RUN - fbrVMState = fbrPool + for i = fbrMax downto 1 + if i < numPool + fbrState[i] = FIBER_FREE + fbrVMState[i] = pool + pool = pool + 512 + fin + next + // + // Set fiber 0 to the RUNning fiber + // + fbrState = FIBER_RUN + fbrVMState = fbrPool else return -1 fin @@ -147,18 +147,18 @@ export def fbrStop(fid) // if fid // - // Remove fiber from RUN list and tag as free - // + // Remove fiber from RUN list and tag as free + // fbrState[fid] = FIBER_FREE i = 0 while fbrNext[i] <> fid - i = fbrNext[i] - loop - fbrNext[i] = fbrNext[fid] - if fid == fbrRunning - fbrRunning = fbrNext[fbrRunning] - return fbrLoad(fbrVMState[fbrRunning]) - fin + i = fbrNext[i] + loop + fbrNext[i] = fbrNext[fid] + if fid == fbrRunning + fbrRunning = fbrNext[fbrRunning] + return fbrLoad(fbrVMState[fbrRunning]) + fin fin end // @@ -179,40 +179,40 @@ export def fbrStart(defaddr, param) for i = fbrMax downto 1 if fbrState[i] == FIBER_FREE - // - // Allocate fiber from pool - // - fbrState[i] = FIBER_RUN - vmstate = fbrVMState[i] - vmstate=>ifp = vmstate + 512 + // + // Allocate fiber from pool + // + fbrState[i] = FIBER_RUN + vmstate = fbrVMState[i] + vmstate=>ifp = vmstate + 512 vmstate=>pp = vmstate + 512 - // - // Set fiber parameters to fiber ID and passed-in value - // + // + // Set fiber parameters to fiber ID and passed-in value + // vmstate->esp = $0E - vmstate->estklo.$0F = i - vmstate->estkhi.$0F = 0 - vmstate->estklo.$0E = param.0 // param lo byte - vmstate->estkhi.$0E = param.1 // param hi byte - // - // Initialize stack to point to fiber def and fbrExit - // This allows a fiber to return and it will fall into fbrExit - // + vmstate->estklo.$0F = i + vmstate->estkhi.$0F = 0 + vmstate->estklo.$0E = param.0 // param lo byte + vmstate->estkhi.$0E = param.1 // param hi byte + // + // Initialize stack to point to fiber def and fbrExit + // This allows a fiber to return and it will fall into fbrExit + // vmstate->hwsp = $FB vmstate=>$FE = @fbrExit - 1 vmstate=>$FC = defaddr - 1 - // - // Link into RUN list - // - fbrNext[i] = fbrNext[fbrRunning] - fbrNext[fbrRunning] = i - // - // Return fiber ID (index) - // - return i - fin - next - return -1 + // + // Link into RUN list + // + fbrNext[i] = fbrNext[fbrRunning] + fbrNext[fbrRunning] = i + // + // Return fiber ID (index) + // + return i + fin + next + return -1 end // // Round-robin schedule RUNning fibers @@ -226,7 +226,7 @@ export def fbrYield if fbrNext[fbrRunning] <> fbrRunning prev = fbrRunning fbrRunning = fbrNext[fbrRunning] - return fbrSwap(fbrVMState[prev], fbrVMState[fbrRunning]) + return fbrSwap(fbrVMState[prev], fbrVMState[fbrRunning]) fin end // @@ -240,17 +240,17 @@ export def fbrHalt // if fbrRunning // - // Remove fiber from RUN list - // + // Remove fiber from RUN list + // i = 0 while fbrNext[i] <> fbrRunning - i = fbrNext[i] - loop - fbrState[fbrRunning] = FIBER_HALT - fbrNext[i] = fbrNext[fbrRunning] - i = fbrRunning - fbrRunning = fbrNext[fbrRunning] - return fbrSwap(fbrVMState[i], fbrVMState[fbrRunning]) + i = fbrNext[i] + loop + fbrState[fbrRunning] = FIBER_HALT + fbrNext[i] = fbrNext[fbrRunning] + i = fbrRunning + fbrRunning = fbrNext[fbrRunning] + return fbrSwap(fbrVMState[i], fbrVMState[fbrRunning]) fin end // @@ -259,11 +259,11 @@ end export def fbrResume(fid) if fbrState[fid] == FIBER_HALT // - // Insert HALTed fiber back into RUN list - // - fbrState[fid] = FIBER_RUN - fbrNext[fid] = fbrNext[fbrRunning] - fbrNext[fbrRunning] = fid + // Insert HALTed fiber back into RUN list + // + fbrState[fid] = FIBER_RUN + fbrNext[fid] = fbrNext[fbrRunning] + fbrNext[fbrRunning] = fid fin end @@ -284,7 +284,7 @@ def fbrTest(fid, param) for i = 1 to param puth(fid); putc($0D) - fbrYield + fbrYield next end