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

  1. /*
  2.    --- Version 2.2 90-10-12 10:34 ---
  3.  
  4.    TSKTOPS.C - CTask - Timer operations.
  5.  
  6.    CTask - a Multitasking Kernel for C
  7.  
  8.    Public Domain Software written by
  9.       Thomas Wagner
  10.       Ferrari electronic Gmbh
  11.       Beusselstrasse 27
  12.       D-1000 Berlin 21
  13.       Germany
  14.  
  15.    No rights reserved.
  16.  
  17.    This file is new with 2.1. The timer related functions were moved
  18.    from tsktimer to this module.
  19.  
  20.    Timer logic has been significantly changed in version 2.0.
  21. */
  22.  
  23. #include "tsk.h"
  24. #include "tsklocal.h"
  25.  
  26. #include <stdarg.h>
  27.  
  28. /*
  29.    create_timer_elem
  30.       Creates a timer queue timeout element.
  31. */
  32.  
  33. tlinkptr CGlobalfunc create_timer_elem (tlinkptr elem, dword tout, 
  34.                                         farptr strucp, byte kind, 
  35.                                         int rept, ...)
  36. {
  37.    va_list val;
  38.  
  39.    va_start (val, rept);
  40.    elem = tsk_setup_telem (elem, TYP_TIMER, strucp, kind, va_arg (val, dword));
  41.    va_end (val);
  42.    if (elem == LNULL)
  43.       return LNULL;
  44.  
  45.    if (rept)
  46.       elem->flags |= TFLAG_REPEAT;
  47.    elem->elkind = TELEM_TIMER;
  48.    elem->elem.time.reload = tsk_timeout(tout);
  49.  
  50.    return elem;
  51. }
  52.  
  53.  
  54. /*
  55.    enable_timer
  56.       The timer element is inserted into the timeout queue with the
  57.       timer reload value.
  58. */
  59.  
  60. void Globalfunc enable_timer (tlinkptr elem)
  61. {
  62.    dword tout;
  63.    CRITICAL;
  64.  
  65.    CHECK_TELPTR (elem, TYP_TIMER, "Enable Timer");
  66.  
  67.    if (elem->link.next)
  68.       return;
  69.  
  70.    tout = elem->elem.time.reload;
  71.    if (tout)
  72.       {
  73.       C_ENTER;
  74.       if (elem->flags & TFLAG_BUSY)
  75.          elem->flags |= TFLAG_ENQUEUE;
  76.       else
  77.          tsk_enqtimer (&elem->link, tout);
  78.       C_LEAVE;
  79.       }
  80. }
  81.  
  82.  
  83. /*
  84.    disable_timer
  85.       The timer element is removed from the timeout queue.
  86. */
  87.  
  88. void Globalfunc disable_timer (tlinkptr elem)
  89. {
  90.    CRITICAL;
  91.  
  92.    CHECK_TELPTR (elem, TYP_TIMER, "Disable Timer");
  93.  
  94.    C_ENTER;
  95.    if (elem->flags & TFLAG_BUSY)
  96.       elem->flags |= TFLAG_UNQUEUE;
  97.    else
  98.       tsk_deqtimer (&elem->link);
  99.    C_LEAVE;
  100. }
  101.  
  102.  
  103. /*
  104.    create_timer
  105.       Creates a timer queue timeout element. The element is inserted into
  106.       the timeout queue.
  107. */
  108.  
  109. tlinkptr CGlobalfunc create_timer (tlinkptr elem, dword tout, farptr strucp,
  110.                                    byte kind, int rept, ...)
  111. {
  112.    va_list val;
  113.  
  114.    va_start (val, rept);
  115.    elem = create_timer_elem (elem, tout, strucp, kind, rept, va_arg (val, dword));
  116.    va_end (val);
  117.    if (elem != LNULL)
  118.       enable_timer (elem);
  119.  
  120.    return elem;
  121. }
  122.  
  123.  
  124. /*
  125.    delete_timer
  126.       Deletes a timeout element.
  127. */
  128.  
  129. void Globalfunc delete_timer (tlinkptr elem)
  130. {
  131.    CRITICAL;
  132.  
  133.    CHECK_TELPTR (elem, TYP_TIMER, "Delete Timer");
  134.  
  135.    C_ENTER;
  136.    if (elem->flags & TFLAG_BUSY)
  137.       {
  138.       elem->flags |= TFLAG_REMOVE;
  139.       C_LEAVE;
  140.       return;
  141.       }
  142.  
  143.    tsk_deqtimer (&elem->link);
  144. #if (GROUPS)
  145.    tsk_dequeue ((queptr)&elem->chain);
  146. #endif
  147.    C_LEAVE;
  148.  
  149. #if (TSK_DYNAMIC)
  150.    if (elem->flags & TFLAG_TEMP)
  151.       tsk_pfree (elem);
  152. #endif
  153. }
  154.  
  155.  
  156. /*
  157.    change_timer
  158.       Changes the timeout and/or repeat-flag in a timer element.
  159.       If the timer was idle, it is inserted into the timeout queue.
  160.  
  161.       If 0 is passed as timeout, the element is removed from the
  162.       timeout queue (same as disable_timer).
  163. */
  164.  
  165. void CGlobalfunc change_timer (tlinkptr elem, dword tout, int rept, ...)
  166. {
  167.    va_list val;
  168.    CRITICAL;
  169.  
  170.    CHECK_TELPTR (elem, TYP_TIMER, "Change Timer");
  171.  
  172.    if (!tout)
  173.       {
  174.       disable_timer (elem);
  175.       return;
  176.       }
  177.  
  178.    va_start (val, rept);
  179.    C_ENTER;
  180.    elem->user_parm = va_arg (val, dword);
  181.    va_end (val);
  182.    tout = elem->elem.time.reload = tsk_timeout(tout);
  183.    if (rept)
  184.       elem->flags |= TFLAG_REPEAT;
  185.    else
  186.       elem->flags &= ~TFLAG_REPEAT;
  187.  
  188.    if (elem->flags & TFLAG_BUSY)
  189.       elem->flags |= TFLAG_ENQUEUE;
  190.    else
  191.       {
  192.       tsk_deqtimer (&elem->link);
  193.       if (tout)
  194.          tsk_enqtimer (&elem->link, tout);
  195.       }
  196.    C_LEAVE;
  197. }
  198.  
  199.