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

  1. /*
  2.     --- Version 2.0 89-12-14 13:16 ---
  3.  
  4.    TSKRSC.C - CTask - Resource 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.    create resource - initialise resource.
  20. */
  21.  
  22. resourceptr far create_resource (resourceptr rsc
  23. #if (TSK_NAMEPAR)
  24.                                  ,byteptr name
  25. #endif
  26.                                  )
  27. {
  28. #if (TSK_DYNAMIC)
  29.    if (rsc == NULL)
  30.       {
  31.       if ((rsc = tsk_alloc (sizeof (resource))) == NULL)
  32.          return NULL;
  33.       rsc->flags = F_TEMP;
  34.       }
  35.    else
  36.       rsc->flags = 0;
  37. #endif
  38.  
  39.    tsk_init_qhead (&rsc->waiting);
  40.    rsc->count = 0;
  41.    rsc->owner = NULL;
  42.  
  43. #if (TSK_NAMED)
  44.    tsk_add_name (&rsc->name, name, TYP_RESOURCE, rsc);
  45. #endif
  46.  
  47.    return rsc;
  48. }
  49.  
  50. /*
  51.    delete_resource - Kill all tasks waiting for the resource.
  52. */
  53.  
  54. void far delete_resource (resourceptr rsc)
  55. {
  56.    CRITICAL;
  57.  
  58.    C_ENTER;
  59.    tsk_kill_queue (&(rsc->waiting));
  60.    rsc->owner = NULL;
  61.    rsc->count = 1;
  62.    C_LEAVE;
  63.  
  64. #if (TSK_NAMED)
  65.    tsk_del_name (&rsc->name);
  66. #endif
  67.  
  68. #if (TSK_DYNAMIC)
  69.    if (rsc->flags & F_TEMP)
  70.       tsk_free (rsc);
  71. #endif
  72. }
  73.  
  74.  
  75. /*
  76.    request_resource - Wait until resource is available. If the resource
  77.                       is free on entry, it is assigned to the task, and
  78.                       the task continues to run.
  79.                       If the task requesting the resource already owns it,
  80.                       this routine returns 0.
  81. */
  82.  
  83. int far request_resource (resourceptr rsc, dword timeout)
  84. {
  85.    CRITICAL;
  86.  
  87.    C_ENTER;
  88.    if (!rsc->count || rsc->owner == GLOBDATA current_task)
  89.       {
  90.       rsc->count = 1;
  91.       rsc->owner = GLOBDATA current_task;
  92.       C_LEAVE;
  93.       return 0;
  94.       }
  95.    GLOBDATA current_task->retptr = NULL;
  96.    tsk_wait (&rsc->waiting, timeout);
  97.    return (int)GLOBDATA current_task->retptr;
  98. }
  99.  
  100.  
  101. /*
  102.    c_request_resource - If the resource is free on entry, it is assigned 
  103.                         to the task, otherwise an error status is returned.
  104. */
  105.  
  106. int far c_request_resource (resourceptr rsc)
  107. {
  108.    int rv;
  109.    CRITICAL;
  110.  
  111.    C_ENTER;
  112.    if ((rv = (!rsc->count || rsc->owner == GLOBDATA current_task)) != 0)
  113.       {
  114.       rsc->count = 1;
  115.       rsc->owner = GLOBDATA current_task;
  116.       }
  117.    C_LEAVE;
  118.    return (rv) ? 0 : -1;
  119. }
  120.  
  121.  
  122. int far request_cresource (resourceptr rsc, dword timeout)
  123. {
  124.    CRITICAL;
  125.  
  126.    C_ENTER;
  127.    if (!rsc->count || rsc->owner == GLOBDATA current_task)
  128.       {
  129.       rsc->count++;
  130.       rsc->owner = GLOBDATA current_task;
  131.       C_LEAVE;
  132.       return 0;
  133.       }
  134.    GLOBDATA current_task->retptr = NULL;
  135.    tsk_wait (&rsc->waiting, timeout);
  136.    return (int)GLOBDATA current_task->retptr;
  137. }
  138.  
  139. int far c_request_cresource (resourceptr rsc)
  140. {
  141.    int rv;
  142.    CRITICAL;
  143.  
  144.    C_ENTER;
  145.    if ((rv = (!rsc->count || rsc->owner == GLOBDATA current_task)) != 0)
  146.       {
  147.       rsc->count++;
  148.       rsc->owner = GLOBDATA current_task;
  149.       }
  150.    C_LEAVE;
  151.    return (rv) ? 0 : -1;
  152. }
  153.  
  154.  
  155. /*
  156.    release_resource - Release the resource. If there are tasks waiting for
  157.                       the resource, it is assigned to the first waiting
  158.                       task, and the task is made eligible.
  159. */
  160.  
  161. void far release_resource (resourceptr rsc)
  162. {
  163.    tcbptr curr;
  164.    CRITICAL;
  165.  
  166.    if (rsc->owner != GLOBDATA current_task)
  167.       return;
  168.  
  169.    C_ENTER;
  170.    if (!--rsc->count)
  171.       {
  172.       if (!(curr = (tcbptr)rsc->waiting.first)->cqueue.kind)
  173.          {
  174.          rsc->owner = NULL;
  175.          C_LEAVE;
  176.          return;
  177.          }
  178.  
  179.       rsc->count++;
  180.       rsc->owner = curr;
  181.       tsk_runable (curr);
  182.       }
  183.    C_LEAVE;
  184. }
  185.  
  186.  
  187. /*
  188.    check_resource - returns 1 if the resource is available.
  189. */
  190.  
  191. int far check_resource (resourceptr rsc)
  192. {
  193.    return rsc->count == 0;
  194. }
  195.  
  196.