home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TSKCNT.C < prev    next >
C/C++ Source or Header  |  1988-03-01  |  3KB  |  141 lines

  1. /*
  2.    TSKCNT.C - CTask - Counter handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "tsk.h"
  14. #include "tsklocal.h"
  15.  
  16. /*
  17.    create_counter - initialises counter.
  18. */
  19.  
  20. void far create_counter (counterptr cnt)
  21. {
  22.    cnt->wait_set = cnt->wait_clear = NULL;
  23.    cnt->state = 0;
  24. }
  25.  
  26.  
  27. /*
  28.    delete_counter - kills all processes waiting for counter
  29. */
  30.  
  31. void far delete_counter (counterptr cnt)
  32. {
  33.    CRITICAL;
  34.  
  35.    C_ENTER;
  36.    tsk_kill_queue (&(cnt->wait_set));
  37.    tsk_kill_queue (&(cnt->wait_clear));
  38.    cnt->state = 0L;
  39.    C_LEAVE;
  40. }
  41.  
  42.  
  43. /*
  44.    clear_counter  - Sets counter to zero. All tasks waiting for
  45.                     Counter zero are made eligible.
  46. */
  47.  
  48. void far clear_counter (counterptr cnt)
  49. {
  50.    CRITICAL;
  51.  
  52.    C_ENTER;
  53.    cnt->state = 0L;
  54.    while (cnt->wait_clear != NULL)
  55.       cnt->wait_clear = tsk_runable (cnt->wait_clear);
  56.    C_LEAVE;
  57. }
  58.  
  59. /*
  60.    wait_counter_set  - Wait until counter is != 0. If counter is != 0 on
  61.                        entry, the counter is decremented and the task
  62.                        continues to run. If the counter is decremented to 
  63.                        zero, tasks waiting for zero are made eligible.
  64. */
  65.  
  66. int far wait_counter_set (counterptr cnt, dword timeout)
  67. {
  68.    CRITICAL;
  69.  
  70.    C_ENTER;
  71.    if (cnt->state)
  72.       {
  73.       if (!--cnt->state)
  74.          while (cnt->wait_clear != NULL)
  75.             cnt->wait_clear = tsk_runable (cnt->wait_clear);
  76.       C_LEAVE;
  77.       return 0;
  78.       }
  79.  
  80.    tsk_current->retptr = NULL;
  81.    tsk_wait (&cnt->wait_set, timeout);
  82.    return (int)tsk_current->retptr;
  83. }
  84.  
  85. /*
  86.    wait_counter_clear - Wait until counter is == 0. If counter is == 0 on
  87.                        entry, the task continues to run.
  88. */
  89.  
  90. int far wait_counter_clear (counterptr cnt, dword timeout)
  91. {
  92.    CRITICAL;
  93.  
  94.    C_ENTER;
  95.    if (!cnt->state)
  96.       {
  97.       C_LEAVE;
  98.       return 0;
  99.       }
  100.  
  101.    tsk_current->retptr = NULL;
  102.    tsk_wait (&cnt->wait_clear, timeout);
  103.    return (int)tsk_current->retptr;
  104. }
  105.  
  106.  
  107. /*
  108.    inc_counter - Increment counter. If there are tasks waiting for the
  109.                  set state, the first task in the queue is made eligible.
  110. */
  111.  
  112. void far inc_counter (counterptr cnt)
  113. {
  114.    tcbptr curr;
  115.  
  116.    CRITICAL;
  117.  
  118.    C_ENTER;
  119.    if ((curr = cnt->wait_set) == NULL)
  120.       {
  121.       cnt->state++;
  122.       C_LEAVE;
  123.       return;
  124.       }
  125.    cnt->wait_set = tsk_runable (curr);
  126.    C_LEAVE;
  127. }
  128.  
  129.  
  130. /*
  131.    check_counter - return current counter state.
  132. */
  133.  
  134. dword far check_counter (counterptr cnt)
  135. {
  136.    return cnt->state;
  137. }
  138.  
  139.  
  140.  
  141.