home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_03 / tsklocal.h < prev    next >
C/C++ Source or Header  |  1990-10-12  |  12KB  |  402 lines

  1. /*
  2.    --- Version 2.1 90-10-12 10:34 ---
  3.  
  4.    TSKLOCAL.H - CTask - Internal definitions and prototypes.
  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.    CAUTION: Routines defined here may *not* be called from outside the
  14.             CTask kernel.
  15. */
  16.  
  17. /* Miscellaneous internal definitions */
  18.  
  19. typedef void (cdecl far *funcptr_void_fp)(farptr);
  20.  
  21. #define TERR_WAIT_SCHED 1
  22.  
  23.  
  24. /*
  25.    struct task_stack describes the contents of a tasks stack after creation.
  26.    The first 2 words are the registers to be restored by the scheduler.
  27.    Only the segment register is significant initially.
  28.    The next three words contain the function address plus the CPU flags
  29.    setup as if an interrupt had occurred at the function's entry address.
  30.  
  31.    This setup is the same as the stack of an interrupted task after
  32.    scheduling.
  33.  
  34.    The following two words contain a dummy return address, which points
  35.    to the routine "killretn". Thus, if the task main function should ever
  36.    return, the task is automatically killed. The last doubleword is
  37.    used for the optional argument to the task.
  38. */
  39.  
  40. struct task_stack {
  41.                   word     r_bx;
  42.                   word     r_ds;
  43.                   funcptr  retn;
  44.                   word     r_flags;
  45.                   funcptr  dummyret;
  46.                   farptr   arg;
  47.                   };
  48.  
  49. /* --------------------------------------------------------------------- */
  50.  
  51. /*
  52.                      The global variable block
  53.  
  54.    The task queues:
  55.  
  56.       timer_queue       All tasks using a timeout, either through "t_delay" 
  57.                         or an event wait, are enqueued into "timer_queue",
  58.                         using the "timerq" link.
  59.  
  60.       eligible_queue    All tasks eligible for running are enqueued 
  61.                         in "eligible_queue".
  62.  
  63.       current_task      Points to the current running task's tcb.
  64.  
  65.    System flags:
  66.  
  67.       preempt           is zero if preemption is allowed.
  68.                         Bit 0 is set if preemption has been disabled globally.
  69.                         Bit 1 is set for temporary disabling preemption.
  70.                         Temporary preemption is automatically removed by the
  71.                         scheduler.
  72.  
  73.       pretick           is nonzero if a schedule request from an 
  74.                         interrupt handler was rejected due to 
  75.                         tsk_preempt nonzero. This allows an immediate 
  76.                         scheduling whenever tsk_preempt is set to 0.
  77.  
  78.       var_prior         Can be set nonzero to enable variable priority.
  79.                         Variable priority will increase the priority of
  80.                         eligible tasks on each scheduler call while they
  81.                         are waiting to be executed, so that low priority 
  82.                         tasks will slowly get to the head of the eligible
  83.                         queue, getting a chance to be run. With variable
  84.                         priority off, lower priority tasks will never be
  85.                         executed while higher priority tasks are eligible.
  86.                     
  87.    System variables:
  88.  
  89.       in_sched          is used in the scheduler only. It is nonzero while
  90.                         the scheduler is active.
  91.  
  92.       tick_factor       is undefined if CLOCK_MSEC is 0, otherwise it 
  93.                         contains the clock tick factor used by tsk_timeout.
  94.  
  95.       ticks_per_sec     is the approximate number of ticks per second
  96.                         expressed as an unsigned integer.
  97.  
  98.       stub_table        is a pointer to the entry stub table if
  99.                         code sharing is enabled, else it is NULL.
  100.  
  101.       l_swap            is set by tskdos and used by the scheduler.
  102.                         It contains the length of the DOS swap area.
  103.                         (DOS only)
  104.  
  105.       dos_vars          contains the address of the DOS swap area.
  106.                         (DOS only)
  107.  
  108.       dos_in_use        contains the address of the DOS in-use flag.
  109.                         (DOS only)
  110.  
  111.       groups            is the group list head. It points to the most 
  112.                         recently created group.
  113.  
  114.       name_list         is only present if TSK_NAMED is nonzero, and
  115.                         if there are no groups.
  116.                         It is the head of the name queue.
  117.  
  118.       kill_queue        is a queue of TCBs waiting to be released
  119.                         after being killed.
  120.  
  121.       kill_task         is a pointer to the TCB of the special killer
  122.                         task.
  123.  
  124.       hotkey_scan       is the list of hotkey entries with non-zero
  125.                         scan-code field.
  126.  
  127.       hotkey_noscan     is the list of hotkey entries with zero
  128.                         scan-code field.
  129.  
  130.       ems_save          is a pointer to the EMS context save routine.
  131.       ems_rest          is a pointer to the EMS context restore routine.
  132.       ems_savetsk       is a pointer to the store EMS context in TCB routine.
  133.  
  134.       emergency_exit    points to the emergency exit routine.
  135.  
  136.       main_ptr          points to the TCB of the main task (non-group only).
  137.  
  138.       remove            is a pointer to the remove chain (non-group only).
  139.  
  140.       ndp_present       is non-zero if a numeric coprocessor is installed.
  141. */
  142.  
  143. typedef struct {
  144.                char        id [8];     /* contains version string */
  145.  
  146.                tcbptr      current_task;
  147.                queue_head  eligible_queue;
  148.  
  149.                queue_head  timer_queue;
  150.                queue_head  watch_queue;
  151.  
  152.                byte        preempt;
  153.                byte        pretick;
  154.                byte        var_prior;
  155.  
  156.                byte        in_sched;
  157.  
  158.                word        tick_factor;
  159.                word        ticks_per_sec;
  160.                tick_ptr    ticker_chain;
  161.  
  162.                farptr      stub_table;
  163.  
  164. #if (TSK_DYNAMIC)
  165.                queue_head  kill_queue;
  166.                tcbptr      kill_task;
  167. #endif
  168.  
  169. #if (HOTKEYS)
  170.                queue_head  hotkey_scan;
  171.                queue_head  hotkey_noscan;
  172. #endif
  173.  
  174. #if (EMS)
  175.                farptr      ems_save;
  176.                farptr      ems_rest;
  177.                funcptr_void_fp ems_savetsk;
  178. #endif
  179. #if (DOS)
  180.                funcptr     emergency_exit;
  181.                word        l_swap;
  182.                dword       dos_vars;
  183.                dword       dos_in_use;
  184. #endif
  185. #if (GROUPS)
  186.                gcb         group;
  187. #else
  188.                tcbptr      main_ptr;
  189.                callchainptr remove;
  190. #if (TSK_NAMED)
  191.                namerec     name_list;
  192. #endif
  193. #endif
  194. #if (NDP)
  195.                byte        ndp_present;
  196. #endif
  197.                } ctask_globvars;
  198.  
  199. typedef ctask_globvars far *globvarptr;
  200.  
  201. /* ------------------ CTask-internal variables ------------------------- */
  202.  
  203. extern counter Neardata tsk_timer_counter;
  204. extern word Neardata tsk_instflags;
  205. extern char Neardata tsk_version [];
  206.  
  207. #if (DOS)
  208. extern counter Neardata tsk_int8_counter;
  209. #endif
  210.  
  211. extern ctask_globvars Neardata tsk_glob_rec;
  212. #if (!SINGLE_DATA)
  213. extern globvarptr Neardata tsk_global;
  214. #endif
  215.  
  216. #if (SINGLE_DATA)
  217. #define  GLOBDATA        tsk_glob_rec.
  218. #else
  219. #define  GLOBDATA        tsk_global->
  220. #endif
  221.  
  222. /* -------- CTask-internal functions (don't use in applications) ------- */
  223.  
  224. /*
  225.    Allocation routines are local to a group. If groups are defined,
  226.    alloc/free must be called indirectly through the pointer in the
  227.    GCB for code sharing to access the correct routines.
  228. */
  229.  
  230. #if (TSK_DYNAMIC && GROUPS)
  231. #define tsk_palloc(x)   (tsk_global->current_task->group->palloc (x))
  232. #define tsk_pfree(x)    (tsk_global->current_task->group->pfree (x))
  233. #elif (TSK_DYNAMIC)
  234. #define tsk_palloc(x)   tsk_alloc(x)
  235. #define tsk_pfree(x)    tsk_free(x)
  236. #endif
  237.  
  238.  
  239. /* module tskasm */
  240.  
  241. #if (TSK_TURBO)
  242. #define tsk_dseg()   _DS
  243. #else
  244. extern word Globalfunc tsk_dseg (void);
  245. #endif
  246. extern word Globalfunc tsk_flags (void);
  247. extern void Localfunc tsk_callfunc (farptr funcad, farptr param);
  248. extern void Globalfunc tsk_memcpy (farptr dest, farptr src, word len);
  249.  
  250. #if (CLOCK_MSEC)
  251. extern dword Localfunc tsk_timeout (dword tout);
  252. #else
  253. #define tsk_timeout(tout)  tout
  254. #endif
  255.  
  256.  
  257. /* module tsktask */
  258.  
  259. extern void Localfunc tsk_kill_queue (queheadptr que);
  260.  
  261. /* module tsksub */
  262.  
  263. extern void Localfunc tsk_runable (tcbptr task);
  264. extern void Localfunc tsk_run_pending (tcbptr task);
  265. extern void Localfunc tsk_runable_all (queheadptr que);
  266. extern void Localfunc tsk_wait (queheadptr que, dword timeout);
  267. extern void Localfunc tsk_init_qhead (queheadptr head, byte kind);
  268. #define tsk_eligible(task) tsk_enqueue(task->qhead = &GLOBDATA eligible_queue, &task->cqueue)
  269.  
  270. #if (TSK_NAMED)
  271. extern void Localfunc tsk_copy_name (nameptr elem, byteptr name);
  272. extern void Localfunc tsk_add_name (nameptr elem, byteptr name, byte kind,
  273.                                     farptr strucp);
  274. extern void Localfunc tsk_del_name (nameptr elem);
  275. #endif
  276.  
  277.  
  278. /* module tskque */
  279.  
  280. extern void Localfunc tsk_enqueue (queheadptr head, queptr elem);
  281. extern void Localfunc tsk_dequeue (queptr elem);
  282. extern void Localfunc tsk_enqtimer (queptr elem, dword tout);
  283. extern void Localfunc tsk_deqtimer (queptr elem);
  284. extern void Localfunc tsk_putqueue (queheadptr q, queptr elem);
  285.  
  286.  
  287. /* module tsktim */
  288.  
  289. extern void Localfunc tsk_install_timer (word divisor, word sys_ticks);
  290. extern void Localfunc tsk_remove_timer (void);
  291. extern void Localfunc tsk_chain_timer (void);
  292.  
  293.  
  294. /* module tskttsk */
  295.  
  296. #if (IBM && !INT8_EARLY)
  297. extern void Taskfunc tsk_int8 (void);
  298. #endif
  299. extern void Taskfunc tsk_timer (void);
  300.  
  301.  
  302. /* module tsktsub */
  303.  
  304. extern tlinkptr Localfunc tsk_setup_telem (tlinkptr elem, byte kind, 
  305.                                            farptr strucp, byte struckind, 
  306.                                            dword upar);
  307.  
  308. /* module tskdos */
  309.  
  310. #if (DOS)
  311. extern void Localfunc tsk_install_dos (void);
  312. extern void Localfunc tsk_remove_dos (void);
  313. extern void far cdecl tsk_emergency_exit (void);
  314. extern void Localfunc tsk_fatal (byteptr errmsg);
  315. #endif
  316.  
  317.  
  318. /* module tskkbd */
  319.  
  320. #if (IBM)
  321. extern void Localfunc tsk_install_kbd (void);
  322. extern void Localfunc tsk_remove_kbd (void);
  323. #endif
  324.  
  325.  
  326. /* module tskbios */
  327.  
  328. #if (AT_BIOS)
  329. extern void Localfunc tsk_install_bios (void);
  330. extern void Localfunc tsk_remove_bios (void);
  331. #endif
  332.  
  333.  
  334. /* module tskint17 */
  335.  
  336. #if (IBM)
  337. extern void Localfunc tsk_install_int17 (void);
  338. extern void Localfunc tsk_remove_int17 (void);
  339. #endif
  340.  
  341.  
  342. /* module tskems */
  343.  
  344. #if (EMS)
  345. extern int Localfunc tsk_install_ems (void);
  346. #endif
  347.  
  348.  
  349. /* module tsksec */
  350.  
  351. #if (DOS)
  352. extern void Localfunc tsk_free_mem (word psp);
  353. extern word Localfunc tsk_getpsp (void);
  354. #endif
  355.  
  356.  
  357. /* module tskgrp */
  358.  
  359. #if (GROUPS)
  360. extern void Localfunc tsk_create_group (gcbptr group, byteptr name);
  361. extern void Localfunc tsk_kill_group (gcbptr group);
  362. extern int Localfunc tsk_remove_group (gcbptr group, int freemem);
  363. #endif
  364.  
  365.  
  366. /* module tskinst */
  367.  
  368. extern int Localfunc tsk_install_main (byte varpri, int speedup, 
  369.                                         word flags TN(byteptr name));
  370. extern void Localfunc tsk_remove_chain (callchainptr chain);
  371. extern void Localfunc tsk_remove_tasker (void);
  372.  
  373.  
  374. /* module tskndp */
  375.  
  376. extern int Localfunc tsk_check_ndp (void);
  377.  
  378.  
  379. #if (CHECKING)
  380.  
  381. extern void CGlobalfunc tsk_fatal_pmd (byteptr txt, ...);
  382. #define TFPMD(txt,ptr)  tsk_fatal_pmd ("%s - Invalid pointer %FP",txt,ptr)
  383.  
  384. #if (TSK_NAMED)
  385. #define CHECK_TCBPTR(ptr,txt) {if(ptr == LNULL || ptr->cqueue.kind != TYP_TCB || ptr->name.list.kind != TYP_TCB) TFPMD(txt,ptr);}
  386. #else
  387. #define CHECK_TCBPTR(ptr,txt) {if(ptr == LNULL || ptr->cqueue.kind != TYP_TCB) TFPMD(txt,ptr);}
  388. #endif
  389. #define CHECK_QHEAD(ptr,txt) {if(ptr == LNULL || !(((queptr)ptr)->kind & Q_HEAD)) TFPMD(txt,ptr);}
  390. #define CHECK_EVTPTR(ptr,knd,txt) {if(ptr == LNULL || ((queptr)ptr)->kind != (knd | Q_HEAD)) TFPMD(txt,ptr);}
  391. #define CHECK_TELPTR(ptr,knd,txt) {if(ptr == LNULL || ptr->link.kind != knd) TFPMD(txt,ptr);}
  392.  
  393. #else
  394.  
  395. #define CHECK_TCBPTR(ptr,txt)
  396. #define CHECK_QHEAD(ptr,txt)
  397. #define CHECK_EVTPTR(ptr,knd,txt)
  398. #define CHECK_TELPTR(ptr,knd,txt)
  399.  
  400. #endif
  401.  
  402.