home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTASK11.ZIP / TSKRSC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-01  |  3.0 KB  |  158 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. resourceptr far create_resource (resourceptr rsc
  21. #if (TSK_NAMEPAR)
  22.                                  ,byteptr name
  23. #endif
  24.                                  )
  25. {
  26. #if (TSK_DYNAMIC)
  27.    if (rsc == NULL)
  28.       {
  29.       if ((rsc = tsk_alloc (sizeof (resource))) == NULL)
  30.          return NULL;
  31.       rsc->flags = F_TEMP;
  32.       }
  33.    else
  34.       rsc->flags = 0;
  35. #endif
  36.  
  37.    rsc->waiting = NULL;
  38.    rsc->state = 1;
  39.    rsc->owner = NULL;
  40.  
  41. #if (TSK_NAMED)
  42.    tsk_add_name (&rsc->name, name, TYP_RESOURCE, rsc);
  43. #endif
  44.  
  45.    return rsc;
  46. }
  47.  
  48. /*
  49.    delete_resource - Kill all tasks waiting for the resource.
  50. */
  51.  
  52. void far delete_resource (resourceptr rsc)
  53. {
  54.    CRITICAL;
  55.  
  56.    C_ENTER;
  57.    tsk_kill_queue (&(rsc->waiting));
  58.    rsc->owner = NULL;
  59.    rsc->state = 0;
  60.    C_LEAVE;
  61.  
  62. #if (TSK_NAMED)
  63.    tsk_del_name (&rsc->name);
  64. #endif
  65.  
  66. #if (TSK_DYNAMIC)
  67.    if (rsc->flags & F_TEMP)
  68.       tsk_free (rsc);
  69. #endif
  70. }
  71.  
  72.  
  73. /*
  74.    request_resource - Wait until resource is available. If the resource
  75.                       is free on entry, it is assigned to the task, and
  76.                       the task continues to run.
  77.                       If the task requesting the resource already owns it,
  78.                       this routine returns 0.
  79. */
  80.  
  81. int far request_resource (resourceptr rsc, dword timeout)
  82. {
  83.    CRITICAL;
  84.  
  85.    C_ENTER;
  86.    if (rsc->state || rsc->owner == tsk_current)
  87.       {
  88.       rsc->state = 0;
  89.       rsc->owner = tsk_current;
  90.       C_LEAVE;
  91.       return 0;
  92.       }
  93.    tsk_current->retptr = NULL;
  94.    tsk_wait (&rsc->waiting, timeout);
  95.    return (int)tsk_current->retptr;
  96. }
  97.  
  98.  
  99. /*
  100.    c_request_resource - If the resource is free on entry, it is assigned 
  101.                         to the task, otherwise an error status is returned.
  102. */
  103.  
  104. int far c_request_resource (resourceptr rsc)
  105. {
  106.    int rv;
  107.    CRITICAL;
  108.  
  109.    C_ENTER;
  110.    if (rv = rsc->state)
  111.       {
  112.       rsc->state = 0;
  113.       rsc->owner = tsk_current;
  114.       }
  115.    C_LEAVE;
  116.    return (rv) ? 0 : -1;
  117. }
  118.  
  119.  
  120. /*
  121.    release_resource - Release the resource. If there are tasks waiting for
  122.                       the resource, it is assigned to the first waiting
  123.                       task, and the task is made eligible.
  124. */
  125.  
  126. void far release_resource (resourceptr rsc)
  127. {
  128.    tcbptr curr;
  129.    CRITICAL;
  130.  
  131.    if (rsc->owner != tsk_current)
  132.       return;
  133.  
  134.    C_ENTER;
  135.    if ((curr = rsc->waiting) == NULL)
  136.       {
  137.       rsc->state = 1;
  138.       rsc->owner = NULL;
  139.       C_LEAVE;
  140.       return;
  141.       }
  142.  
  143.    rsc->waiting = tsk_runable (curr);
  144.    rsc->owner = curr;
  145.    C_LEAVE;
  146. }
  147.  
  148.  
  149. /*
  150.    check_resource - returns 1 if the resource is available.
  151. */
  152.  
  153. int far check_resource (resourceptr rsc)
  154. {
  155.    return rsc->state;
  156. }
  157.  
  158.