home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CTASK.ZIP / TSK.H < prev    next >
C/C++ Source or Header  |  1989-12-23  |  20KB  |  605 lines

  1. /*
  2.     --- Version 2.0 89-12-23 23:32 ---
  3.  
  4.    TSK.H - CTask - Type definitions and global routine prototypes.
  5.  
  6.    Public Domain Software written by
  7.       Thomas Wagner
  8.       Patschkauer Weg 31
  9.       D-1000 Berlin 33
  10.       West Germany
  11. */
  12.  
  13.  
  14. #include "tskconf.h"
  15. #include <stdio.h>
  16.  
  17. #if defined(__TURBOC__)
  18. #include <dos.h>
  19. #define _Near
  20. #define TURBO  1
  21. #define MSC    0
  22. #else
  23. #include <dos.h>
  24. #include <conio.h>
  25. #include <stdlib.h>
  26. #define _Near near
  27. #define TURBO  0
  28. #define MSC    1
  29. #endif
  30.  
  31. #define local  static     /* Comment out the "static" for debugging */
  32.  
  33. typedef unsigned char byte;
  34. typedef unsigned short word;
  35. typedef unsigned long dword;
  36. typedef void (cdecl far *funcptr)();
  37. typedef void far *farptr;
  38. typedef byte far *byteptr;
  39. typedef word far *wordptr;
  40.  
  41. #define TTIMEOUT ((farptr) -1L)
  42. #define TWAKE    ((farptr) -2L)
  43. #define TWATCH   ((farptr) -3L)
  44.  
  45. #define TIMEOUT   (-1)
  46. #define WAKE      (-2)
  47. #define WATCH     (-3)
  48.  
  49. /* Task states */
  50.  
  51. #define    ST_KILLED   0
  52. #define  ST_STOPPED  1
  53. #define  ST_DELAYED  2
  54. #define  ST_WAITING  3
  55. #define  ST_ELIGIBLE 4
  56. #define  ST_RUNNING  5
  57.  
  58. /* Task flags */
  59.  
  60. #define  F_TEMP   0x80     /* Task is temporary, free on kill */
  61. #define  F_STTEMP 0x40     /* Task stack is temporary, free on kill */
  62.  
  63. #define  F_CRIT   0x01     /* Task is critical, may not be preempted */
  64.  
  65. #define  FL_SYSM  0xf0     /* Mask for system flags */
  66. #define  FL_USRM  0x0f     /* Mask for user flags */
  67.  
  68. /* Timer queue element states */
  69.  
  70. #define  TSTAT_IDLE        0     /* Not in queue */
  71. #define  TSTAT_REMOVE      1     /* Remove element from queue */
  72. #define  TSTAT_WATCH       2     /* Watch element */
  73. #define  TSTAT_CONTWATCH   3     /* Continuous Watch element */
  74. #define  TSTAT_COUNTDOWN   4     /* Count down timeout, then remove */
  75. #define  TSTAT_REPEAT      5     /* Count down, reload when done */
  76.  
  77. /* Timer queue element action kinds (upper nibble of elkind) */
  78.  
  79. #define  TELEM_TIMER       0x10  /* Timeout element */
  80. #define  TELEM_MEM         0x20  /* Memory watch element */
  81. #define  TELEM_PORT        0x30  /* Port watch element */
  82.  
  83. /* Timer watch element comparison kinds (lower nibble of elkind) */
  84.  
  85. #define  TCMP_EQ           1     /* Equal */
  86. #define  TCMP_NE           2     /* Not Equal */
  87. #define  TCMP_GE           3     /* Greater or Equal (unsigned) */
  88. #define  TCMP_LE           4     /* Less or Equal (unsigned) */
  89. #define  TCMP_GES          5     /* Greater or Equal (signed) */
  90. #define  TCMP_LES          6     /* Less or Equal (signed) */
  91. #define  TCMP_CHG          7     /* Change in value */
  92.  
  93. /* Timer queue element control structure pointer kinds */
  94.  
  95. #define  TKIND_TASK        1     /* tcbptr, Wakeup associated task */
  96. #define  TKIND_WAKE        2     /* tcbptr, but not same task */
  97. #define  TKIND_PROC        3     /* call function */
  98. #define  TKIND_FLAG        4     /* flagptr, set flag */
  99. #define  TKIND_COUNTER     5     /* counterptr, increment counter */
  100.  
  101. /* Timer queue element flags */
  102.  
  103. #define  TFLAG_BUSY        0x01  /* Timer task is busy processing element */
  104. #define  TFLAG_TEMP        0x80  /* Bit set means temporary element */
  105.  
  106. /* Name link structure types */
  107.  
  108. #define  TYP_GROUP      0
  109. #define  TYP_TCB        1
  110. #define  TYP_FLAG       2
  111. #define  TYP_RESOURCE   3
  112. #define  TYP_COUNTER    4
  113. #define  TYP_MAILBOX    5
  114. #define  TYP_PIPE       6
  115. #define  TYP_WPIPE      7
  116. #define  TYP_BUFFER     8
  117.  
  118. #define  NAMELENGTH     9  /* For structure names: 8 bytes + zero */
  119.  
  120. /* Installation flags */
  121.  
  122. #define IFL_VIDEO      0x0001      /* Install INT 10 access resource */
  123. #define IFL_DISK       0x0002      /* Install INT 13 access resource */
  124. #define IFL_INT8_DIR   0x0004      /* Call original INT 8 directly */
  125. #define IFL_PRINTER    0x0008      /* Install INT 17 handler */
  126. #define IFL_INT15      0x0010      /* Install IBM-AT INT 15 handler */
  127. #define IFL_NODOSVARS  0x0020      /* Don't swap DOS variables */
  128.  
  129. #define IFL_STD   (IFL_DISK | IFL_PRINTER | IFL_INT15)   /* Standard flags */
  130.  
  131. /* 
  132.     Size of the DOS variable swap area plus 8 bytes.
  133.     40+8 is more than sufficient for all current versions of DOS up to
  134.     DOS 4.01.
  135. */
  136.  
  137. #define DOSSWAPSIZE        0x30
  138.  
  139. /* --------------------------------------------------------------------- */
  140.  
  141. /*
  142.    The 'queue' structure is a dual link for linking task control blocks 
  143.    and timer blocks. The first three fields are used both for the queue
  144.    head, and for elements to be inserted in a queue.
  145.  
  146.     CAUTION: Do not change the order of the first three fields in
  147.                either queue or queue_head!
  148. */
  149.  
  150. typedef struct {
  151.                 word     prior;
  152.                 word     ini_prior;
  153.                } qelem_pri;
  154.  
  155. typedef struct queue_rec far *queptr;
  156.  
  157. typedef struct queue_rec {
  158.                           queptr   volatile next;
  159.                           queptr   volatile prev;
  160.                           byte     kind;
  161.                           union  {
  162.                                  qelem_pri   pri;
  163.                                  dword       ticks;
  164.                                  } el;
  165.                          } queue;
  166.  
  167.  
  168. typedef struct {
  169.                queptr   volatile first;
  170.                queptr   volatile last;
  171.                     byte        kind;
  172.                } queue_head;
  173.  
  174. typedef queue_head far *queheadptr;
  175.  
  176. typedef struct name_rec far *nameptr;
  177.  
  178. struct name_rec {
  179.                 queue_head list;          /* Name list */
  180.                 farptr     strucp;        /* Top of structure pointer */
  181.                 char       name [NAMELENGTH];
  182.                 };
  183.  
  184. typedef struct name_rec namerec;
  185.  
  186. typedef struct tcb_rec far *tcbptr;
  187. typedef tcbptr far *tqueptr;
  188.  
  189. typedef struct tlink_rec far *tlinkptr;
  190.  
  191. struct telem_memwatch {
  192.                       wordptr address;
  193.                       word    mask;
  194.                       word    compare;
  195.                       };
  196.  
  197. struct telem_portwatch {
  198.                         word  port;
  199.                         word  mask;
  200.                         word  compare;
  201.                         byte  in_word;
  202.                        };
  203.  
  204. struct telem_timeout {
  205.                      dword    reload;     /* Timeout counter reload value */
  206.                      };
  207.  
  208. struct tlink_rec {
  209.                   queue     link;
  210.                   farptr   strucp;     /* Pointer to control structure */
  211.                   dword    user_parm;  /* User defined parameter */
  212.                   union {
  213.                         struct telem_memwatch   mem;
  214.                         struct telem_portwatch  port;
  215.                         struct telem_timeout    time;
  216.                         }  elem;
  217.                   byte     elkind;     /* Kind of watch element */
  218.                   byte     tstate;     /* Element state */
  219.                   byte     flags;      /* Flags */
  220.                  };
  221.  
  222. typedef struct tlink_rec tlink;
  223.  
  224. typedef struct group_rec gcb;
  225. typedef gcb far *gcbptr;
  226.  
  227. struct tcb_rec {
  228.                    queue        cqueue;     /* Current queue link */
  229.                    queheadptr  qhead;       /* Queue head pointer */
  230.                    byteptr      stkbot;     /* Task stack bottom */
  231.                byte         state;      /* Task state */
  232.                byte         flags;      /* Task flags */
  233.  
  234.                byteptr      stack;        /* Task register save area */
  235.                     word            t_ax;
  236.                     word            t_cx;
  237.                     word            t_dx;
  238.                     word            t_si;
  239.                     word            t_di;
  240.                     word            t_bp;
  241.                     word            t_es;
  242.                     word            t_ds;
  243.  
  244.                tlink        timerq;     /* Timer queue link */
  245.                farptr       retptr;     /* Event return pointer */
  246.                int          retsize;    /* Return buffer size for pipes */
  247.  
  248.                funcptr      save_func;
  249.                funcptr      rest_func;
  250.                farptr       user_ptr;
  251. #if (GROUPS)
  252.                gcbptr       group;      /* Current Group control block pointer */
  253.                gcbptr       homegroup;  /* Creating Group control block pointer */
  254. #endif
  255. #if (DOS)
  256.                byte         indos;      /* Flag for use by DOS handler */
  257.                     byte            new;            /* New task flag for DOS save */
  258.                     word            base_psp;    /* Base PSP segment */
  259.                dword        psp_sssp;   /* Saved PSP SS/SP value */
  260.                     byte            swap_area [DOSSWAPSIZE]; /* DOS vars save area */
  261. #endif
  262. #if (TSK_NAMED)
  263.                namerec      name;
  264. #endif
  265.                   };
  266.  
  267. typedef struct tcb_rec tcb;
  268.  
  269. struct group_rec {
  270.                  gcbptr    home;       /* Home group */
  271.                  gcbptr    level;      /* Next group on level */
  272.                  gcbptr    branch;     /* Next group on higher level */
  273.                  tcbptr    creator;    /* Task that created this group */
  274.                       dword     exit_addr;  /* Exit address */
  275.                  word      create_psp; /* Base PSP addr of this group */
  276.                  word      save_psp;   /* PSP addr of this group's creator */
  277.                  dword     save_sssp;  /* SS/SP from base PSP of creator */
  278.                       namerec   namelist;   /* List of structures in this group */
  279.                  };
  280.  
  281.  
  282. typedef struct {
  283.                queue_head  wait_set;
  284.                queue_head  wait_clear;
  285.                int             state;
  286. #if (TSK_DYNAMIC)
  287.                byte            flags;
  288. #endif
  289. #if (TSK_NAMED)
  290.                namerec         name;
  291. #endif
  292.                } flag;
  293.  
  294. typedef flag far *flagptr;
  295.  
  296. typedef struct {
  297.                queue_head  wait_set;
  298.                queue_head  wait_clear;
  299.                dword           state;
  300. #if (TSK_DYNAMIC)
  301.                byte            flags;
  302. #endif
  303. #if (TSK_NAMED)
  304.                namerec         name;
  305. #endif
  306.                } counter;
  307.  
  308. typedef counter far *counterptr;
  309.  
  310. typedef struct {
  311.                queue_head  waiting;
  312.                tcbptr       owner;
  313.                word         count;
  314. #if (TSK_DYNAMIC)
  315.                byte         flags;
  316. #endif
  317. #if (TSK_NAMED)
  318.                namerec      name;
  319. #endif
  320.                } resource;
  321.  
  322. typedef resource far *resourceptr;
  323.  
  324. struct msg_header {
  325.                   struct msg_header far *next;
  326.                   };
  327.  
  328. typedef struct msg_header far *msgptr;
  329.  
  330. typedef struct {
  331.                queue_head  waiting;
  332.                msgptr          mail_first;
  333.                msgptr          mail_last;
  334. #if (TSK_DYNAMIC)
  335.                byte            flags;
  336. #endif
  337. #if (TSK_NAMED)
  338.                namerec         name;
  339. #endif
  340.                } mailbox;
  341.  
  342. typedef mailbox far *mailboxptr;
  343.  
  344. typedef struct {
  345.                queue_head  wait_read;
  346.                queue_head  wait_write;
  347.                queue_head  wait_clear;
  348.                word         bufsize;
  349.                word         filled;
  350.                word         inptr;
  351.                word         outptr;
  352.                byteptr      contents;
  353. #if (TSK_DYNAMIC)
  354.                byte         flags;
  355. #endif
  356. #if (TSK_NAMED)
  357.                namerec      name;
  358. #endif
  359.                } pipe;
  360.  
  361. typedef pipe far *pipeptr;
  362.  
  363. typedef struct {
  364.                queue_head  wait_read;
  365.                queue_head  wait_write;
  366.                queue_head  wait_clear;
  367.                word         bufsize;
  368.                word         filled;
  369.                word         inptr;
  370.                word         outptr;
  371.                wordptr      wcontents;
  372. #if (TSK_DYNAMIC)
  373.                byte         flags;
  374. #endif
  375. #if (TSK_NAMED)
  376.                namerec      name;
  377. #endif
  378.                } wpipe;
  379.  
  380. typedef wpipe far *wpipeptr;
  381.  
  382. typedef struct {
  383.                resource    buf_write;
  384.                resource    buf_read;
  385.                wpipe       pip;
  386.                word        msgcnt;
  387. #if (TSK_DYNAMIC)
  388.                byte    flags;
  389. #endif
  390. #if (TSK_NAMED)
  391.                namerec     name;
  392. #endif
  393.                } buffer;
  394.  
  395. typedef buffer far *bufferptr;
  396.  
  397.  
  398. typedef struct ticker_rec far *tick_ptr;
  399.  
  400. typedef struct ticker_rec {
  401.                           tick_ptr next;
  402.                           dword    ticks;
  403. #if (TSK_DYNAMIC)
  404.                           byte     flags;
  405. #endif
  406.                           } ticker;
  407.  
  408.  
  409. extern int far install_tasker (byte varpri, int speedup, word flags, byteptr name);
  410. extern void far remove_tasker (void);
  411. extern void far preempt_on (void);
  412. extern void far preempt_off (void);
  413. extern void far tsk_ena_preempt (void);
  414. extern void far tsk_dis_preempt (void);
  415.  
  416. extern void far schedule (void);
  417. extern void far yield (void);
  418.  
  419. extern tcbptr far create_task (tcbptr task, funcptr func, byteptr stack,
  420.                                word stksz, word prior, farptr arg
  421. #if (TSK_NAMED)
  422.                                ,byteptr name
  423. #endif
  424.                                );
  425. extern void far kill_task (tcbptr task);
  426. extern int far start_task (tcbptr task);
  427. extern int far wake_task (tcbptr task);
  428. extern word far get_priority (tcbptr task);
  429. extern void far set_priority (tcbptr task, word prior);
  430. extern void far set_funcs (tcbptr task, funcptr save, funcptr rest);
  431. extern farptr far set_user_ptr (tcbptr task, farptr uptr);
  432. extern farptr far get_user_ptr (tcbptr task);
  433. extern tcbptr far curr_task (void);
  434.  
  435. extern int far t_delay (dword ticks);
  436.  
  437. extern tlinkptr far create_timer (tlinkptr elem, dword tout, farptr strucp,
  438.                                   byte kind, byte rept, ...);
  439. extern void far delete_timer (tlinkptr elem);
  440. extern void far change_timer (tlinkptr elem, dword tout, byte rept, ...);
  441.  
  442. extern tlinkptr far create_memory_watch (tlinkptr elem, farptr address, 
  443.                                          word mask, word compare, 
  444.                                          byte cmpkind, farptr strucp, 
  445.                                          byte kind, byte rept, ...);
  446.  
  447. extern tlinkptr far create_port_watch (tlinkptr elem, 
  448.                                        word port, byte in_word,
  449.                                        word mask, word compare, 
  450.                                        byte cmpkind, farptr strucp, 
  451.                                        byte kind, byte rept, ...);
  452.  
  453. extern void far delete_watch (tlinkptr elem);
  454.  
  455. extern int far wait_memory (farptr address, 
  456.                             word mask, word compare, byte cmpkind);
  457. extern int far wait_port (word port, byte in_word,
  458.                           word mask, word compare, byte cmpkind);
  459.  
  460. extern resourceptr far create_resource (resourceptr rsc
  461. #if (TSK_NAMED)
  462.                                ,byteptr name
  463. #endif
  464.                                );
  465. extern void far delete_resource (resourceptr rsc);
  466. extern void far release_resource (resourceptr rsc);
  467. extern int far request_resource (resourceptr rsc, dword timeout);
  468. extern int far request_cresource (resourceptr rsc, dword timeout);
  469. extern int far c_request_resource (resourceptr rsc);
  470. extern int far c_request_cresource (resourceptr rsc);
  471. extern int far check_resource (resourceptr rsc);
  472.  
  473. extern flagptr far create_flag (flagptr flg
  474. #if (TSK_NAMED)
  475.                                ,byteptr name
  476. #endif
  477.                                );
  478. extern void far delete_flag (flagptr flg);
  479. extern void far set_flag (flagptr flg);
  480. extern void far clear_flag (flagptr flg);
  481. extern int far clear_flag_wait_set (flagptr flg, dword timeout);
  482. extern int far wait_flag_set (flagptr flg, dword timeout);
  483. extern int far wait_flag_clear (flagptr flg, dword timeout);
  484. extern int far check_flag (flagptr flg);
  485.  
  486. extern counterptr far create_counter (counterptr cnt
  487. #if (TSK_NAMED)
  488.                                ,byteptr name
  489. #endif
  490.                                );
  491. extern void far delete_counter (counterptr cnt);
  492. extern void far clear_counter (counterptr cnt);
  493. extern int far wait_counter_set (counterptr cnt, dword timeout);
  494. extern int far wait_counter_clear (counterptr cnt, dword timeout);
  495. extern void far inc_counter (counterptr cnt);
  496. extern void far set_counter (counterptr cnt, dword val);
  497. extern dword far check_counter (counterptr cnt);
  498.  
  499. extern mailboxptr far create_mailbox (mailboxptr box
  500. #if (TSK_NAMED)
  501.                                ,byteptr name
  502. #endif
  503.                                );
  504. extern void far delete_mailbox (mailboxptr box);
  505. extern void far send_mail (mailboxptr box, farptr msg);
  506. extern farptr far wait_mail (mailboxptr box, dword timeout);
  507. extern farptr far c_wait_mail (mailboxptr box);
  508. extern int far check_mailbox (mailboxptr box);
  509.  
  510. extern pipeptr far create_pipe (pipeptr pip, farptr buf, word bufsize
  511. #if (TSK_NAMED)
  512.                                ,byteptr name
  513. #endif
  514.                                );
  515. extern void far delete_pipe (pipeptr pip);
  516. extern int far read_pipe (pipeptr pip, dword timeout);
  517. extern int far c_read_pipe (pipeptr pip);
  518. extern int far write_pipe (pipeptr pip, byte ch, dword timeout);
  519. extern int far c_write_pipe (pipeptr pip, byte ch);
  520. extern int far wait_pipe_empty (pipeptr pip, dword timeout);
  521. extern int far check_pipe (pipeptr pip);
  522. extern word far pipe_free (pipeptr pip);
  523. extern void far flush_pipe (pipeptr pip);
  524.  
  525. extern wpipeptr far create_wpipe (wpipeptr pip, farptr buf, word bufsize
  526. #if (TSK_NAMED)
  527.                                ,byteptr name
  528. #endif
  529.                                );
  530. extern void far delete_wpipe (wpipeptr pip);
  531. extern word far read_wpipe (wpipeptr pip, dword timeout);
  532. extern word far c_read_wpipe (wpipeptr pip);
  533. extern int far write_wpipe (wpipeptr pip, word ch, dword timeout);
  534. extern int far c_write_wpipe (wpipeptr pip, word ch);
  535. extern int far wait_wpipe_empty (wpipeptr pip, dword timeout);
  536. extern word far check_wpipe (wpipeptr pip);
  537. extern word far wpipe_free (wpipeptr pip);
  538. extern void far flush_wpipe (wpipeptr pip);
  539.  
  540.  
  541. extern bufferptr far create_buffer (bufferptr buf, farptr pbuf, word bufsize
  542. #if (TSK_NAMED)
  543.                                ,byteptr name
  544. #endif
  545.                                );
  546. extern void far delete_buffer (bufferptr buf);
  547. extern int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout);
  548. extern int far c_read_buffer (bufferptr buf, farptr msg, int size);
  549. extern int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout);
  550. extern int far c_write_buffer (bufferptr buf, farptr msg, int size);
  551. extern word far check_buffer (bufferptr buf);
  552.  
  553. extern word far t_read_key (void);
  554. extern word far t_wait_key (dword timeout);
  555. extern word far t_keyhit (void);
  556.  
  557. extern int far  tsk_dis_int (void);
  558. extern void far tsk_ena_int (int);
  559. extern void far tsk_nop (void);
  560.  
  561. #if (TURBO)
  562. #define tsk_cli()       disable()
  563. #define tsk_sti()       enable()
  564. #define tsk_outp(p,b)   outportb(p,b)
  565. #define tsk_inp(p)      ((byte) inportb(p))
  566. extern word far tsk_inpw (word port);
  567. #elif MSC
  568. #pragma intrinsic(_disable,_enable,outp,inp,inpw)
  569. #define tsk_cli()       _disable()
  570. #define tsk_sti()       _enable()
  571. #define tsk_outp(p,b)   outp(p,b)
  572. #define tsk_inp(p)      ((byte) inp(p))
  573. #define tsk_inpw(p)     ((byte) inpw(p))
  574.  
  575. #define MK_FP(seg,ofs)    ((farptr)(((dword)(seg) << 16) | (word)(ofs)))
  576. #else
  577. extern int far  tsk_dis_int (void);
  578. extern void far tsk_ena_int (int);
  579. extern void far tsk_cli (void);
  580. extern void far tsk_sti (void);
  581. #endif
  582.  
  583. #define CRITICAL  int crit_intsav
  584. #define C_ENTER   crit_intsav = tsk_dis_int()
  585. #define C_LEAVE   tsk_ena_int (crit_intsav)
  586.  
  587. #if (TSK_DYNAMIC)
  588. extern farptr far tsk_alloc (word size);
  589. extern void far tsk_free (farptr item);
  590. extern resource _Near alloc_resource;
  591. #endif
  592.  
  593. #if (TSK_NAMED)
  594. extern farptr far find_group_name (gcbptr group, byteptr name, int kind);
  595. extern farptr far find_name (byteptr name, int kind);
  596. #endif
  597.  
  598. extern tick_ptr far create_ticker (tick_ptr elem, dword val);
  599. extern void far delete_ticker (tick_ptr elem);
  600. extern void far set_ticker (tick_ptr elem, dword val);
  601. extern dword far get_ticker (tick_ptr elem);
  602.  
  603. extern int far ctask_resident (void);
  604.  
  605.