home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TSKRSC.C < prev    next >
Text File  |  1988-03-01  |  2KB  |  128 lines

  1. /*
  2.    TSKRSC.C - CTask - Resource 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 resource - initialise resource.
  18. */
  19.  
  20. void far create_resource (resourceptr rsc)
  21. {
  22.    rsc->waiting = NULL;
  23.    rsc->state = 1;
  24.    rsc->owner = NULL;
  25. }
  26.  
  27. /*
  28.    delete_resource - Kill all tasks waiting for the resource.
  29. */
  30.  
  31. void far delete_resource (resourceptr rsc)
  32. {
  33.    CRITICAL;
  34.  
  35.    C_ENTER;
  36.    tsk_kill_queue (&(rsc->waiting));
  37.    rsc->owner = NULL;
  38.    rsc->state = 0;
  39.    C_LEAVE;
  40. }
  41.  
  42.  
  43. /*
  44.    request_resource - Wait until resource is available. If the resource
  45.                       is free on entry, it is assigned to the task, and
  46.                       the task continues to run.
  47.                       If the task requesting the resource already owns it,
  48.                       this routine returns 0.
  49. */
  50.  
  51. int far request_resource (resourceptr rsc, dword timeout)
  52. {
  53.    CRITICAL;
  54.  
  55.    C_ENTER;
  56.    if (rsc->state || rsc->owner == tsk_current)
  57.       {
  58.       rsc->state = 0;
  59.       rsc->owner = tsk_current;
  60.       C_LEAVE;
  61.       return 0;
  62.       }
  63.    tsk_current->retptr = NULL;
  64.    tsk_wait (&rsc->waiting, timeout);
  65.    return (int)tsk_current->retptr;
  66. }
  67.  
  68.  
  69. /*
  70.    c_request_resource - If the resource is free on entry, it is assigned 
  71.                         to the task, otherwise an error status is returned.
  72. */
  73.  
  74. int far c_request_resource (resourceptr rsc)
  75. {
  76.    int rv;
  77.    CRITICAL;
  78.  
  79.    C_ENTER;
  80.    if (rv = rsc->state)
  81.       {
  82.       rsc->state = 0;
  83.       rsc->owner = tsk_current;
  84.       }
  85.    C_LEAVE;
  86.    return (rv) ? 0 : -1;
  87. }
  88.  
  89.  
  90. /*
  91.    release_resource - Release the resource. If there are tasks waiting for
  92.                       the resource, it is assigned to the first waiting
  93.                       task, and the task is made eligible.
  94. */
  95.  
  96. void far release_resource (resourceptr rsc)
  97. {
  98.    tcbptr curr;
  99.    CRITICAL;
  100.  
  101.    if (rsc->owner != tsk_current)
  102.       return;
  103.  
  104.    C_ENTER;
  105.    if ((curr = rsc->waiting) == NULL)
  106.       {
  107.       rsc->state = 1;
  108.       rsc->owner = NULL;
  109.       C_LEAVE;
  110.       return;
  111.       }
  112.  
  113.    rsc->waiting = tsk_runable (curr);
  114.    rsc->owner = curr;
  115.    C_LEAVE;
  116. }
  117.  
  118.  
  119. /*
  120.    check_resource - returns 1 if the resource is available.
  121. */
  122.  
  123. int far check_resource (resourceptr rsc)
  124. {
  125.    return rsc->state;
  126. }
  127.  
  128.