home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacMETH 3.2.1 / Coroutines / Coroutines.DEF < prev    next >
Encoding:
Modula Definition  |  1994-04-08  |  1.7 KB  |  57 lines  |  [TEXT/MEDT]

  1. DEFINITION MODULE Coroutines;  (* J. Thoeny 4/94 *)
  2.  
  3.   TYPE
  4.     Coroutine;
  5.     Process;
  6.  
  7.   PROCEDURE NewCoroutine(p: PROC; n: LONGINT; VAR cID: Coroutine);
  8.     (* create new coroutine cID, using procedure p and a workspace with size n *)
  9.  
  10.   PROCEDURE Transfer(VAR fromID, toID: Coroutine);
  11.     (* transfer processor from coroutine fromID to coroutine toID *)
  12.  
  13.   PROCEDURE FreeCoroutineWorkspace(cID : Coroutine) : LONGINT;
  14.     (* returns the current free workspace of coroutine cID, i.e. free stack space *)
  15.  
  16.   PROCEDURE MyCoroutineID() : Coroutine;
  17.     (* Coroutine ID which uses the processor at the moment *)
  18.   
  19.   PROCEDURE NewProcess(p: PROC; n: LONGINT; VAR pID: Process);
  20.     (* create new preemptive scheduled process pID, using procedure p and a workspace
  21.        with size n.
  22.        Note that the process is started in suspended State
  23.     *)
  24.  
  25.   PROCEDURE ResumeProcess(pID: Process);
  26.   PROCEDURE SuspendProcess(pID: Process);
  27.   
  28.  
  29.   PROCEDURE BEGINCriticalSection();
  30.   PROCEDURE ENDCriticalSection();
  31.  
  32.     (* disallows/allows rescheduling,
  33.        this procedure-pair allows to perform critical tasks within a process,
  34.        such as manipulation of global variables.
  35.  
  36.        Example:
  37.          CONST
  38.            MAXIterations = 10000;
  39.          VAR
  40.            iterationCount : INTEGER; (* is set to 1 during initialisation *)
  41.          
  42.          PROCEDURE MyProcess();
  43.          BEGIN
  44.             WHILE iterationCount < MAXIterations DO
  45.               DoOneIteration();
  46.               BEGINCriticalSection();
  47.                 INC(iterationCount);
  48.               ENDCriticalSection()
  49.             END(*WHILE*);
  50.           END MyProcess;
  51.     *)
  52.  
  53.   PROCEDURE MyProcessID() : Process;
  54.     (* returns ID of the process currently using the processor*)
  55.  
  56. END Coroutines.
  57.