home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CTASK22.ZIP / TSKRSC.C < prev    next >
C/C++ Source or Header  |  1990-10-12  |  4KB  |  204 lines

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