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

  1. /*
  2.    --- Version 2.2 90-10-12 10:34 ---
  3.  
  4.    TSKTUTL.C - CTask - Task 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.    wake_task
  33.       Restarts a task waiting for an event or timeout. 
  34.       Returns -1 if the task was not waiting or stopped.
  35. */
  36.  
  37. int Globalfunc wake_task (tcbptr task)
  38. {
  39.    CRITICAL;
  40.  
  41.    if (task == LNULL)
  42. #if (GROUPS)
  43.       task = GLOBDATA current_task->group->main_ptr;
  44. #else
  45.       task = GLOBDATA main_ptr;
  46. #endif
  47.  
  48.    CHECK_TCBPTR (task, "Wake Task");
  49.  
  50.    C_ENTER;
  51.    if (task->state >= ST_ELIGIBLE)
  52.       {
  53.       C_LEAVE;
  54.       return -1;
  55.       }
  56.  
  57.    task->retptr = TWAKE;
  58.    tsk_runable (task);
  59.    C_LEAVE;
  60.    return 0;
  61. }
  62.  
  63.  
  64. /*
  65.    stop_task
  66.       Sets a task to the stopped state.
  67. */
  68.  
  69. int Globalfunc stop_task (tcbptr task)
  70. {
  71.    CRITICAL;
  72.  
  73.    if (task == LNULL)
  74. #if (GROUPS)
  75.       task = GLOBDATA current_task->group->main_ptr;
  76. #else
  77.       task = GLOBDATA main_ptr;
  78. #endif
  79.  
  80.    CHECK_TCBPTR (task, "Stop Task");
  81.  
  82.    if (task->state < ST_STOPPED)
  83.       return -1;
  84.  
  85.    C_ENTER;
  86.    task->state = ST_STOPPED;
  87.    tsk_dequeue (&task->cqueue);
  88.    task->qhead = LNULL;
  89.    tsk_deqtimer (&task->timerq.link);
  90.    C_LEAVE;
  91.  
  92.    return 0;
  93. }
  94.  
  95.  
  96. /*
  97.    get_priority
  98.       Returns the priority of a task.
  99. */
  100.  
  101. word Globalfunc get_priority (tcbptr task)
  102. {
  103.    if (task == LNULL)
  104. #if (GROUPS)
  105.       task = GLOBDATA current_task->group->main_ptr;
  106. #else
  107.       task = GLOBDATA main_ptr;
  108. #endif
  109.  
  110.    CHECK_TCBPTR (task, "Get Priority");
  111.  
  112.    return task->cqueue.el.pri.ini_prior;
  113. }
  114.  
  115.  
  116. /*
  117.    set_priority
  118.       Changes the priority of a task. If the task is enqueued in a
  119.       queue, its position in the queue is affected.
  120. */
  121.  
  122. void Globalfunc set_priority (tcbptr task, word prior)
  123. {
  124.    queheadptr que;
  125.    CRITICAL;
  126.  
  127.    if (task == LNULL)
  128. #if (GROUPS)
  129.       task = GLOBDATA current_task->group->main_ptr;
  130. #else
  131.       task = GLOBDATA main_ptr;
  132. #endif
  133.  
  134.    CHECK_TCBPTR (task, "Set Priority");
  135.  
  136.    C_ENTER;
  137.    task->cqueue.el.pri.prior = task->cqueue.el.pri.ini_prior = prior;
  138.  
  139.    if ((task->state != ST_RUNNING) && ((que = task->qhead) != LNULL))
  140.       {
  141.       tsk_dequeue (&task->cqueue);
  142.       tsk_enqueue (que, &task->cqueue);
  143.       }
  144.    C_LEAVE;
  145. }
  146.  
  147.  
  148. /*
  149.    set_task_flags
  150.       Changes the user modifiable flags of the task.
  151. */
  152.  
  153. void Globalfunc set_task_flags (tcbptr task, byte flags)
  154. {
  155.    CRITICAL;
  156.  
  157.    if (task == LNULL)
  158. #if (GROUPS)
  159.       task = GLOBDATA current_task->group->main_ptr;
  160. #else
  161.       task = GLOBDATA main_ptr;
  162. #endif
  163.  
  164.    CHECK_TCBPTR (task, "Set Task Flags");
  165.    C_ENTER;
  166.    task->flags = (byte)((task->flags & FL_SYSM) | (flags & FL_USRM));
  167.    C_LEAVE;
  168. }
  169.  
  170.  
  171. /*
  172.    set_funcs
  173.       Set the save/restore functions in the TCB.
  174. */
  175.  
  176. void Globalfunc set_funcs (tcbptr task, funcptr save, funcptr rest)
  177. {
  178.    CRITICAL;
  179.  
  180.    if (task == LNULL)
  181. #if (GROUPS)
  182.       task = GLOBDATA current_task->group->main_ptr;
  183. #else
  184.       task = GLOBDATA main_ptr;
  185. #endif
  186.  
  187.    CHECK_TCBPTR (task, "Set Task Funcs");
  188.    C_ENTER;
  189.    task->save_func = save;
  190.    task->rest_func = rest;
  191.    C_LEAVE;
  192. }
  193.  
  194.  
  195. /*
  196.    set_user_ptr
  197.       Set the user pointer in the TCB.
  198. */
  199.  
  200. farptr Globalfunc set_user_ptr (tcbptr task, farptr uptr)
  201. {
  202.    farptr old;
  203.    CRITICAL;
  204.  
  205.    if (task == LNULL)
  206. #if (GROUPS)
  207.       task = GLOBDATA current_task->group->main_ptr;
  208. #else
  209.       task = GLOBDATA main_ptr;
  210. #endif
  211.  
  212.    CHECK_TCBPTR (task, "Set Task User Pointer");
  213.    C_ENTER;
  214.    old = task->user_ptr;
  215.    task->user_ptr = uptr;
  216.    C_LEAVE;
  217.  
  218.    return old;
  219. }
  220.  
  221.  
  222. /*
  223.    get_user_ptr
  224.       Returns the user pointer from the TCB.
  225. */
  226.  
  227. farptr Globalfunc get_user_ptr (tcbptr task)
  228. {
  229.    if (task == LNULL)
  230. #if (GROUPS)
  231.       task = GLOBDATA current_task->group->main_ptr;
  232. #else
  233.       task = GLOBDATA main_ptr;
  234. #endif
  235.  
  236.    CHECK_TCBPTR (task, "Get Task User Pointer");
  237.    return task->user_ptr;
  238. }
  239.  
  240.  
  241. /*
  242.    curr_task
  243.       Returns TCB of current running task.
  244. */
  245.  
  246. tcbptr Globalfunc curr_task (void)
  247. {
  248.    return GLOBDATA current_task;
  249. }
  250.  
  251.  
  252.