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

  1. /*
  2.     --- Version 2.0 89-12-14 12:50 ---
  3.  
  4.    TSKFLG.C - CTask - Flag handling routines.
  5.  
  6.    Public Domain Software written by
  7.       Thomas Wagner
  8.       Patschkauer Weg 31
  9.       D-1000 Berlin 33
  10.       West Germany
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. #include "tsk.h"
  16. #include "tsklocal.h"
  17.  
  18.  
  19. /*
  20.    create_flag - initialises flag.
  21. */
  22.  
  23. flagptr far create_flag (flagptr flg
  24. #if (TSK_NAMEPAR)
  25.                         ,byteptr name
  26. #endif
  27.                         )
  28. {
  29. #if (TSK_DYNAMIC)
  30.    if (flg == NULL)
  31.       {
  32.       if ((flg = tsk_alloc (sizeof (flag))) == NULL)
  33.          return NULL;
  34.       flg->flags = F_TEMP;
  35.       }
  36.    else
  37.       flg->flags = 0;
  38. #endif
  39.  
  40.    tsk_init_qhead (&flg->wait_set);
  41.    tsk_init_qhead (&flg->wait_clear);
  42.    flg->state = 0;
  43.  
  44. #if (TSK_NAMED)
  45.    tsk_add_name (&flg->name, name, TYP_FLAG, flg);
  46. #endif
  47.  
  48.    return flg;
  49. }
  50.  
  51. /*
  52.    delete_flag - kills all processes waiting for flag
  53. */
  54.  
  55. void far delete_flag (flagptr flg)
  56. {
  57.    CRITICAL;
  58.  
  59.    C_ENTER;
  60.  
  61.    tsk_kill_queue (&(flg->wait_set));
  62.    tsk_kill_queue (&(flg->wait_clear));
  63.    flg->state = 0;
  64.    C_LEAVE;
  65.  
  66. #if (TSK_NAMED)
  67.    tsk_del_name (&flg->name);
  68. #endif
  69.  
  70. #if (TSK_DYNAMIC)
  71.    if (flg->flags & F_TEMP)
  72.       tsk_free (flg);
  73. #endif
  74. }
  75.  
  76.  
  77. /*
  78.    wait_flag_set  - Wait until flag is != 0. If flag is != 0 on
  79.                     entry, the task continues to run.
  80. */
  81.  
  82. int far wait_flag_set (flagptr flg, dword timeout)
  83. {
  84.    CRITICAL;
  85.  
  86.    C_ENTER;
  87.    if (flg->state)
  88.       {
  89.       C_LEAVE;
  90.       return 0;
  91.       }
  92.    GLOBDATA current_task->retptr = NULL;
  93.    tsk_wait (&flg->wait_set, timeout);
  94.    return (int)GLOBDATA current_task->retptr;
  95. }
  96.  
  97.  
  98. /*
  99.    wait_flag_clear - Wait until flag is == 0. If flag is == 0 on
  100.                      entry, the task continues to run.
  101. */
  102.  
  103. int far wait_flag_clear (flagptr flg, dword timeout)
  104. {
  105.    CRITICAL;
  106.  
  107.    C_ENTER;
  108.    if (!flg->state)
  109.       {
  110.       C_LEAVE;
  111.       return 0;
  112.       }
  113.  
  114.    GLOBDATA current_task->retptr = NULL;
  115.    tsk_wait (&flg->wait_clear, timeout);
  116.    return (int)GLOBDATA current_task->retptr;
  117. }
  118.  
  119.  
  120. /*
  121.    set_flag - Set flag to 1. If there are tasks waiting for the
  122.               set state, all tasks in the queue are made eligible.
  123. */
  124.  
  125. void far set_flag (flagptr flg)
  126. {
  127.    CRITICAL;
  128.  
  129.    C_ENTER;
  130.    flg->state = 1;
  131.    tsk_runable_all (&flg->wait_set);
  132.    C_LEAVE;
  133. }
  134.  
  135.  
  136. /*
  137.    clear_flag - Set flag to 0. If there are tasks waiting for the
  138.                 clear state, all tasks in the queue are made eligible.
  139. */
  140.  
  141. void far clear_flag (flagptr flg)
  142. {
  143.    CRITICAL;
  144.  
  145.    C_ENTER;
  146.    flg->state = 0;
  147.    tsk_runable_all (&flg->wait_clear);
  148.    C_LEAVE;
  149. }
  150.  
  151.  
  152. /*
  153.    clear_flag_wait_set - Set flag to 0, then wait for set state.
  154. */
  155.  
  156. int far clear_flag_wait_set (flagptr flg, dword timeout)
  157. {
  158.    clear_flag (flg);
  159.    return wait_flag_set (flg, timeout);
  160. }
  161.  
  162.  
  163. /*
  164.    check_flag - return current flag state.
  165. */
  166.  
  167. int far check_flag (flagptr flg)
  168. {
  169.    return flg->state;
  170. }
  171.  
  172.  
  173.  
  174.