home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / TESTS / SRC / TASKTEST.MOD < prev    next >
Text File  |  1996-08-28  |  4KB  |  125 lines

  1. MODULE TaskTest;
  2.  
  3. (************************************************************************)
  4. (*                                                                      *)
  5. (*                      Test program TaskTest                           *)
  6. (*      Creates several threads, prints some messages from each.        *)
  7. (*                                                                      *)
  8. (*      Purpose: to help in developing and testing the OS/2 version     *)
  9. (*                      of module TaskControl                           *)
  10. (*                                                                      *)
  11. (*      Author:         P. Moylan                                       *)
  12. (*      Last edited:    28 August 1996                                  *)
  13. (*      Status:         Working                                         *)
  14. (*                                                                      *)
  15. (************************************************************************)
  16.  
  17. FROM Testbed IMPORT
  18.     (* proc *)  Checkpoint;
  19.  
  20. FROM TaskControl IMPORT
  21.     (* proc *)  CreateTask;
  22.  
  23. FROM Semaphores IMPORT
  24.     (* type *)  Semaphore,
  25.     (* proc *)  CreateSemaphore, Wait, Signal;
  26.  
  27. FROM Timer IMPORT
  28.     (* proc *)  Sleep;
  29.  
  30. FROM STextIO IMPORT
  31.     (* proc *)  WriteString, WriteLn;
  32.  
  33. FROM SWholeIO IMPORT
  34.     (* proc *)  WriteCard;
  35.  
  36. (************************************************************************)
  37.  
  38. CONST NumberOfChildThreads = 3;
  39.  
  40. VAR ChildDone: Semaphore;
  41.     TaskNum: RECORD
  42.                  access: Semaphore;
  43.                  next: CARDINAL;
  44.              END (*RECORD*);
  45.  
  46. (************************************************************************)
  47. (*                         CODE FOR ONE THREAD                          *)
  48. (************************************************************************)
  49.  
  50. PROCEDURE ThreadCode;
  51.  
  52.     (* One copy of this procedure is run for each child task. *)
  53.  
  54.     VAR MyID: CARDINAL;  j: CARDINAL;
  55.  
  56.     BEGIN
  57.         (* Work out which task we are. *)
  58.  
  59.         WITH TaskNum DO
  60.              Wait (access);
  61.              MyID := next;
  62.              INC (next);
  63.              Signal (access);
  64.         END (*WITH*);
  65.         WriteString ("Task ");  WriteCard (MyID, 2);
  66.         WriteString (" starting");  WriteLn;
  67.  
  68.         (* Do something to prove that we're running. *)
  69.  
  70.         FOR j := 1 TO 4 DO
  71.             WriteString ("Message ");  WriteCard (j,2);
  72.             WriteString ("/4 from task ");
  73.             WriteCard (MyID, 2);
  74.             WriteLn;
  75.             Sleep (2000);
  76.         END (*FOR*);
  77.  
  78.         (* Tell main task that we're done. *)
  79.  
  80.         Signal (ChildDone);
  81.  
  82.     END ThreadCode;
  83.  
  84. (****************************************************************)
  85. (*        PROCEDURE TO CREATE AND RUN THE CHILD THREADS         *)
  86. (****************************************************************)
  87.  
  88. PROCEDURE RunTheTest;
  89.  
  90.     (* On entry, the "count" part of the ThreadInfo array is already    *)
  91.     (* set up.  This procedure creates the necessary semaphores, tells  *)
  92.     (* the child threads to start, and then waits until they have       *)
  93.     (* finished.                                                        *)
  94.  
  95.     VAR j: CARDINAL;
  96.  
  97.     BEGIN
  98.         WITH TaskNum DO
  99.             CreateSemaphore (access, 1);  next := 1;
  100.         END (*WITH*);
  101.         FOR j := 1 TO NumberOfChildThreads DO
  102.             (*Checkpoint ("RunTheTest, about to call CreateTask");*)
  103.             CreateSemaphore (ChildDone, 0);
  104.             CreateTask (ThreadCode, 2, "child");
  105.             (*Checkpoint ("RunTheTest, after return from CreateTask");*)
  106.         END (*FOR*);
  107.         FOR j := 1 TO NumberOfChildThreads DO
  108.             Wait (ChildDone);
  109.         END (*FOR*);
  110.         WriteString ("Done!");  WriteLn;
  111.  
  112.     END RunTheTest;
  113.  
  114. (****************************************************************)
  115. (*                          MAIN PROGRAM                        *)
  116. (****************************************************************)
  117.  
  118. BEGIN
  119.     WriteLn;
  120.     WriteString ("Simple test of creating child threads.");
  121.     WriteLn;
  122.     RunTheTest;
  123. END TaskTest.
  124.  
  125.