home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CTASK.ZIP / TEST.C < prev    next >
C/C++ Source or Header  |  1989-12-23  |  6KB  |  277 lines

  1.  
  2. /*
  3.    Test program for checking basic CTask functions.
  4.  
  5.    Keyboard input:
  6.  
  7.       e  Terminates program
  8.       d  Snapshot dump to STDOUT
  9.       h  Stop screen output
  10.       c  Resume screen output
  11.       b  Beep (Turbo C only)
  12.       m  Snapshot dump to mono screen
  13.       s  Snapshot dump to colour screen
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <time.h>
  22.  
  23. #include "tsk.h"
  24. #include "tsksup.h"
  25.  
  26. #define STACKSIZE 2048
  27.  
  28. unsigned int _stklen = 6 * STACKSIZE;  /* For Turbo C: Five tasks + main Task Stack */
  29.  
  30. typedef struct {
  31.                farptr xx;
  32.                char str [20];
  33.                } message;
  34.  
  35. tcb tcb1, tcb2, tcb3, tcb4, tcb5;
  36. mailbox box;
  37. message msg;
  38. flag halt;
  39. pipe pip;
  40. buffer buf;
  41.  
  42. byte pipbuf [10];
  43. word bufbuf [40];
  44.  
  45. int endrun;
  46.  
  47.  
  48. /*
  49.    Task1 sends a mail to Task3, and waits for a response in the buffer.
  50.    The response is then displayed.
  51.    Task1 will stop while the halt flag is set.
  52. */
  53.  
  54. void far task1 (void)
  55. {
  56.    char str [20];
  57.  
  58.    tprintf ("Task 1 started\n");
  59.    while (!endrun)
  60.       {
  61.       wait_flag_clear (&halt, 0L);
  62.  
  63.       t_delay (5L);
  64.       tputch ('1');
  65.       strcpy (msg.str, "From T1");
  66.       send_mail (&box, &msg);
  67.  
  68.       read_buffer (&buf, str, 20, 0L);
  69.       tprintf ("Task 1 read buf: <%s>\n", str);
  70.       }
  71. }
  72.  
  73. /*
  74.    Task2 reads the keyboard. If a character has been read, it is passed
  75.    to Task4 via a pipe. Entering 'h' will set the halt flag (stopping Task1),
  76.    entering 'c' will clear the halt flag.
  77.    'e' stops the program.
  78. */
  79.  
  80. void far task2 (void)
  81. {
  82.    int ch;
  83.  
  84.    tprintf ("Task 2 started\n");
  85.    while (!endrun)
  86.       {
  87.       ch = t_wait_key (36L);
  88.       if (ch < 0)
  89.          tputch (0x04);
  90.       else
  91.          {
  92.          switch (tolower (ch & 0xff))
  93.             {
  94.             case 'h':   set_flag (&halt);
  95.                         break;
  96.             case 'c':   clear_flag (&halt);
  97.                         break;
  98.             case 'e':   wake_task (NULL);
  99.                         break;
  100.    #if (TSK_NAMED)
  101.             case 'd':   snapshot (stdout);
  102.                         break;
  103.             case 'm':   screensnap ((char far *)0xb0000000L, 25);
  104.                         break;
  105.             case 's':   screensnap ((char far *)0xb8000000L, 60);
  106.                         break;
  107.    #endif
  108.    #if (TURBO)
  109.             case 'b':   sound (2000);
  110.                         t_delay (2);
  111.                         nosound ();
  112.                         break;
  113.    #endif
  114.             }
  115.  
  116.          if (!endrun)
  117.             write_pipe (&pip, (char)ch, 0L);
  118.          tputch ('2');
  119.          }
  120.       }
  121. }
  122.  
  123.  
  124. /*
  125.    Task3 waits for mail, then sends it back through a buffer.
  126. */
  127.  
  128. void far task3 (void)
  129. {
  130.    message far *m;
  131.  
  132.    tprintf ("Task 3 started\n");
  133.    while (!endrun)
  134.       {
  135.       m = wait_mail (&box, 0L);
  136.       tprintf ("Task 3 received <%Fs>\n", m->str);
  137.  
  138.       m->str [6] = '3';
  139.       write_buffer (&buf, m->str, 7, 0L);
  140.       }
  141. }
  142.  
  143.  
  144. /*
  145.    Task4 waits for a character in the pipe and displays it. To make
  146.    things livelier, it uses a timeout while waiting, and will display
  147.    faces when the timeout occurred before the character.
  148. */
  149.  
  150. void far task4 (void)
  151. {
  152.    int ch;
  153.  
  154.    tprintf ("Task 4 started\n");
  155.    while (!endrun)
  156.       {
  157.       ch = read_pipe (&pip, 10L);
  158.       if (ch < 0)
  159.          tputch (0x02);
  160.       else
  161.          tprintf ("Task 4 got <%c>\n", ch);
  162.       }
  163. }
  164.  
  165. /*
  166.    Task 5 checks the memory watch capability by waiting for the
  167.    BIOS keyboard status byte at 40:17 to contain 1 in the lower two
  168.    bits, signalling that both the left and right shift keys are pressed.
  169. */
  170.  
  171. void far task5 (void)
  172. {
  173.    tprintf ("Task 5 started\n");
  174.    while (!endrun)
  175.       {
  176.          wait_memory ((farptr)0x417L, 0x03, 0x03, TCMP_EQ);
  177.          tprintf ("Task 5: Both Shift keys pressed\n");
  178.          wait_memory ((farptr)0x417L, 0x03, 0x03, TCMP_NE);
  179.          tprintf ("Task 5: Shift keys released\n");
  180.       }
  181. }
  182.  
  183.  
  184. int main (void)
  185. {
  186.    char stack1 [STACKSIZE];
  187.    char stack2 [STACKSIZE];
  188.    char stack3 [STACKSIZE];
  189.    char stack4 [STACKSIZE];
  190.    char stack5 [STACKSIZE];
  191.  
  192. #if(TURBO)
  193.    directvideo = 0;
  194. #endif
  195.    endrun = 0;
  196.    install_tasker (0, 0, IFL_STD, "Test");
  197.  
  198.    create_mailbox (&box
  199. #if (TSK_NAMEPAR)
  200.                 ,"Mailbox"
  201. #endif
  202.                 );
  203.    create_flag (&halt
  204. #if (TSK_NAMEPAR)
  205.                 ,"Halt"
  206. #endif
  207.                 );
  208.    create_pipe (&pip, pipbuf, sizeof (pipbuf)
  209. #if (TSK_NAMEPAR)
  210.                 ,"Pipe"
  211. #endif
  212.                 );
  213.    create_buffer (&buf, bufbuf, sizeof (bufbuf)
  214. #if (TSK_NAMEPAR)
  215.                 ,"Buffer"
  216. #endif
  217.                 );
  218.  
  219.    init_conout ();
  220.    create_task (&tcb1, task1, stack1, STACKSIZE, PRI_STD, NULL
  221. #if (TSK_NAMEPAR)
  222.                 ,"TASK1"
  223. #endif
  224.                 );
  225.    create_task (&tcb2, task2, stack2, STACKSIZE, PRI_STD, NULL
  226. #if (TSK_NAMEPAR)
  227.                 ,"TASK2"
  228. #endif
  229.                 );
  230.    create_task (&tcb3, task3, stack3, STACKSIZE, PRI_STD, NULL
  231. #if (TSK_NAMEPAR)
  232.                 ,"TASK3"
  233. #endif
  234.                 );
  235.    create_task (&tcb4, task4, stack4, STACKSIZE, PRI_STD, NULL
  236. #if (TSK_NAMEPAR)
  237.                 ,"TASK4"
  238. #endif
  239.                 );
  240.    create_task (&tcb5, task5, stack5, STACKSIZE, PRI_STD, NULL
  241. #if (TSK_NAMEPAR)
  242.                 ,"TASK5"
  243. #endif
  244.                 );
  245.    start_task (&tcb1);
  246.    start_task (&tcb2);
  247.    start_task (&tcb3);
  248.    start_task (&tcb4);
  249.    start_task (&tcb5);
  250.    preempt_on ();
  251.  
  252.    t_delay (0L);
  253.  
  254.    endrun = 1;
  255.    tputs ("******** Main Task *********");
  256.  
  257.    kill_task (&tcb1);
  258.    kill_task (&tcb2);
  259.    kill_task (&tcb3);
  260.    kill_task (&tcb4);
  261.    kill_task (&tcb5);
  262.  
  263.    delete_mailbox (&box);
  264.    delete_pipe (&pip);
  265.    delete_buffer (&buf);
  266.    delete_flag (&halt);
  267.  
  268.    schedule ();
  269.  
  270.    end_conout ();
  271.  
  272.    remove_tasker ();
  273.    puts ("******** End Run *********");
  274.    return 0;
  275. }
  276.  
  277.