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

  1. /*
  2.    --- Version 2.2 90-10-12 10:34 ---
  3.  
  4.    TSKUTIL.C - CTask - Utilitiy routines.
  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 version 2.1.
  18.    Version 2.1 separates the functions previously collected in tskmain.c into
  19.          tskmain.c - Front ends for installation/removal
  20.          tskinst.c - Install/Remove main kernel
  21.          tskgrp.c  - Create/remove groups
  22.          tsktask.c - Task creation and deletion
  23.          tsktutl.c - Task utilities (get/set priority etc.)
  24.          tskutil.c - General utilities (preemption, t_delay)
  25. */
  26.  
  27. #include "tsk.h"
  28. #include "tsklocal.h"
  29.  
  30.  
  31. /*
  32.    preempt_off
  33.       Turns off task preemption (will stay off until explicitly enabled).
  34. */
  35.  
  36. void Globalfunc preempt_off (void)
  37. {
  38.    GLOBDATA preempt = 1;
  39. }
  40.  
  41.  
  42. /*
  43.    preempt_on
  44.       Resets permanent and temporary task preemption flag. If 
  45.       preemption is pending, the scheduler is called.
  46. */
  47.  
  48. void Globalfunc preempt_on (void)
  49. {
  50.    GLOBDATA preempt = 0;
  51.    tsk_cli ();
  52.    if (GLOBDATA pretick)
  53.       schedule ();
  54.    tsk_sti ();
  55. }
  56.  
  57.  
  58. /*
  59.    tsk_ena_preempt
  60.       Resets temporary task preemption flag. If preemption is pending,
  61.       the scheduler is called.
  62. */
  63.  
  64. void Globalfunc tsk_ena_preempt (void)
  65. {
  66.    tsk_cli ();
  67.    if (!(GLOBDATA preempt &= ~2))
  68.       if (GLOBDATA pretick)
  69.          schedule ();
  70.    tsk_sti ();
  71. }
  72.  
  73.  
  74. /*
  75.    tsk_dis_preempt
  76.       Sets temporary task preemption flag.
  77. */
  78.  
  79. void Globalfunc tsk_dis_preempt (void)
  80. {
  81.    GLOBDATA preempt |= 2;
  82. }
  83.  
  84.  
  85. /*
  86.    t_delay
  87.       delay the current task by "ticks" clock ticks.
  88.       If ticks is zero, the task is stopped.
  89. */
  90.  
  91. int Globalfunc t_delay (dword ticks)
  92. {
  93.    tcbptr task = GLOBDATA current_task;
  94.  
  95.    tsk_cli ();
  96.    tsk_dequeue (&task->cqueue);
  97.    tsk_deqtimer (&task->timerq.link);
  98.    task->qhead = LNULL;
  99.    if (ticks)
  100.       {
  101.       task->state = ST_DELAYED;
  102.       task->timerq.flags = 0;
  103.       task->timerq.link.kind = TYP_TIMER;
  104.       tsk_enqtimer (&task->timerq.link, ticks);
  105.       }
  106.    else
  107.       task->state = ST_STOPPED;
  108.  
  109.    schedule ();
  110.    return (int)((dword)task->retptr);
  111. }
  112.  
  113.  
  114. /*
  115.    chain_removefunc
  116.       Enters the passed function into the remove function list,
  117.       returns the chain pointer, or LNULL on error.
  118. */
  119.  
  120. callchainptr Globalfunc chain_removefunc (funcptr_ccp func, callchainptr chain, farptr user_ptr)
  121. {
  122. #if (TSK_DYNAMIC)
  123.    if (chain == LNULL)
  124.       {
  125.       if ((chain = tsk_palloc (sizeof (callchain))) == LNULL)
  126.          return LNULL;
  127.       chain->flags = F_TEMP;
  128.       }
  129.    else
  130.       chain->flags = 0;
  131. #endif
  132.    chain->func = func;
  133.    chain->user_ptr = user_ptr;
  134. #if (GROUPS)
  135.    chain->next = GLOBDATA current_task->group->remove;
  136.    GLOBDATA current_task->group->remove = chain;
  137. #else
  138.    chain->next = GLOBDATA remove;
  139.    GLOBDATA remove = chain;
  140. #endif
  141.    return chain;
  142. }
  143.  
  144. /*
  145.    unchain_removefunc
  146.       Removes the passed chain pointer from the remove list.
  147. */
  148.  
  149. void Globalfunc unchain_removefunc (callchainptr chain)
  150. {
  151.    callchainptr last, curr;
  152.  
  153.    if (chain->func == LNULL)
  154.       return;
  155.  
  156. #if (GROUPS)
  157.    last = (callchainptr)&GLOBDATA current_task->group->remove;
  158. #else
  159.    last = (callchainptr)&GLOBDATA remove;
  160. #endif
  161.    do
  162.       {
  163.       curr = last->next;
  164.       }
  165.    while (curr != LNULL && curr != chain);
  166.    if (curr == LNULL)
  167.       return;
  168.    last->next = curr->next;
  169.    curr->func = LNULL;
  170.  
  171. #if (TSK_DYNAMIC)
  172.    if (curr->flags & F_TEMP)
  173.       tsk_pfree (curr);
  174. #endif
  175. }
  176.  
  177.  
  178. word Globalfunc ticks_per_sec (void)
  179. {
  180.    return GLOBDATA ticks_per_sec;
  181. }
  182.  
  183.