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

  1. /*
  2.    --- Version 2.2 90-10-12 10:34 ---
  3.  
  4.    TSK.H - CTask - Type definitions and global routine 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.  
  14.  
  15. #include "tskconf.h"
  16. #include <stdio.h>
  17. #include <dos.h>
  18. #include <conio.h>
  19. #include <stdlib.h>
  20.  
  21. #if defined(__TURBOC__)
  22. #define TSK_TURBO  1
  23. #define TSK_MSC    0
  24. #else
  25. #define TSK_TURBO  0
  26. #define TSK_MSC    1
  27. #endif
  28.  
  29. /* 
  30.    Selecting near/far and calling sequence attributes:
  31.  
  32.    For Turbo and Microsoft, you won't have to change the following
  33.    defines. Just set the NEAR_CODE and LOCALS_FAR defines, and the
  34.    CALL_PASCAL define, in tskconf.h.
  35.  
  36.    Other compilers may or may not support some aspects of the model
  37.    mix used in standard CTask. For those compilers, you may have to
  38.    edit the definitions to suit your memory model.
  39.  
  40.    Globalfunc  defines a global CTask routine.
  41.                The attributes are normally "far cdecl", but may also
  42.                be "near" and/or "pascal", depending on configuration.
  43.  
  44.    CGlobalfunc defines a global CTask routine that must be called
  45.                using the C calling sequence.
  46.                The attributes are normally "far cdecl", but may also
  47.                be "near cdecl".
  48.  
  49.    Localfunc   defines a routine that is exclusively used inside CTask.
  50.                Those routines are normally 'near' to save some code.
  51.                If your compiler does not allow placing all CTask
  52.                routines in a common code segment, you will have to
  53.                define this as 'far'.
  54.                It normally has the "near cdecl" attribute, but may also
  55.                be "far" and/or "pascal".
  56.  
  57.    CLocalfunc  same as Localfunc, but must use C calling sequence.
  58.  
  59.    Staticfunc  defines a routine that is local to a module.
  60.                Those routines are only used within a module, and can 
  61.                normally be 'near'. Only if your compiler does not 
  62.                support the 'near' keyword, or places all routines in
  63.                separate segments, will you have to redefine this as 
  64.                empty or 'far'.
  65.                The attributes normally are "near pascal".
  66.  
  67.    CStaticfunc same as Staticfunc, but must use C calling sequence.
  68.  
  69.    Taskfunc    defines task and call-back functions.
  70.                Must be 'far cdecl' unless your compiler does not
  71.                support those keywords.
  72.  
  73.    Neardata    Is the attribute for local shared data in CTask. It is
  74.                defined as 'near' for MSC, and empty for Turbo C.
  75.                This causes the shared data to be placed in the
  76.                default data segment (except for Turbo C Huge, where
  77.                the assignment to the CTask data segment is done with
  78.                compiler switches).
  79.                If your compiler does not support the 'near' attribute
  80.                for global data, you may define this as empty, but please
  81.                make sure that the assembler modules can access the data.
  82.  
  83. */
  84.  
  85. #if (TSK_TURBO)
  86. #define Neardata           /* Turbo doesn't like near on data */
  87. #else
  88. #define Neardata near      /* MSC requires it when in Large model */
  89. #endif
  90.  
  91. #if (NEAR_CODE)
  92. #define Farc  near        /* near code means all routines are near */
  93. #define Nearc near        /* including internal ones */
  94. #else
  95. #if (CODE_SHARING && TSK_MSC)
  96. #define Farc  far _loadds /* for code sharing, _loadds is required */
  97. #else
  98. #define Farc  far         /* else it's just 'far' */
  99. #endif
  100. #if (LOCALS_FAR)
  101. #define Nearc far         /* Near code is far, too */
  102. #else
  103. #define Nearc near        /* That's the normal case */
  104. #endif
  105. #endif
  106. #if (CALL_PASCAL)
  107. #define Cdeclc pascal
  108. #else
  109. #define Cdeclc cdecl
  110. #endif
  111.  
  112. #define Globalfunc   Farc Cdeclc
  113. #define Localfunc    Nearc Cdeclc
  114. #define CGlobalfunc  Farc cdecl
  115. #define CLocalfunc   Nearc cdecl
  116. #define Staticfunc   near pascal
  117. #define CStaticfunc  near cdecl
  118. #define Taskfunc     far cdecl
  119.  
  120. /*
  121.    The local attribute is used mainly for documentation purposes.
  122.    It should be 'static', unless you want the routines to be globally
  123.    visible for debugging (define empty).
  124. */
  125.  
  126. #define local  static
  127.  
  128. /*---------------------------------------------------------------------*/
  129.  
  130. typedef unsigned char byte;
  131. typedef unsigned short word;
  132. typedef unsigned long dword;
  133. typedef void far *farptr;
  134. typedef byte far *byteptr;
  135. typedef word far *wordptr;
  136. typedef void (Taskfunc *funcptr)();
  137. typedef void (Taskfunc *funcptr_void)(void);
  138. typedef void (Taskfunc *funcptr_int)(int);
  139. typedef void (Taskfunc *funcptr_fp)(farptr);
  140. typedef void (Taskfunc *funcptr_dword)(dword);
  141. typedef void (interrupt far * intprocptr)(void);
  142.  
  143. #define TTIMEOUT ((farptr) -1L)
  144. #define TWAKE    ((farptr) -2L)
  145. #define TWATCH   ((farptr) -3L)
  146.  
  147. #define TIMEOUT   (-1)
  148. #define WAKE      (-2)
  149. #define WATCH     (-3)
  150.  
  151. /* Task states */
  152.  
  153. #define  ST_KILLED   (byte)0
  154. #define  ST_STOPPED  (byte)1
  155. #define  ST_DELAYED  (byte)2
  156. #define  ST_WAITING  (byte)3
  157. #define  ST_ELIGIBLE (byte)4
  158. #define  ST_RUNNING  (byte)5
  159.  
  160. /* Task flags */
  161.  
  162. #define  F_TEMP      (byte)0x80     /* Task is temporary, free on kill */
  163. #define  F_STTEMP    (byte)0x40     /* Task stack is temporary, free on kill */
  164. #define  F_PERM      (byte)0x20     /* Task is permanent, do not kill */
  165.  
  166. #define  F_USES_NDP  (byte)0x02     /* Task uses the NDP */
  167. #define  F_CRIT      (byte)0x01     /* Task is critical, may not be preempted */
  168.  
  169. #define  FL_SYSM     (byte)0xf0     /* Mask for system flags */
  170. #define  FL_USRM     (byte)0x0f     /* Mask for user flags */
  171.  
  172. /* Timer queue element action kinds (upper nibble of elkind) */
  173.  
  174. #define  TELEM_TIMER       (byte)0x10  /* Timeout element */
  175. #define  TELEM_MEM         (byte)0x20  /* Memory watch element */
  176. #define  TELEM_PORT        (byte)0x30  /* Port watch element */
  177. #define  TELEM_HOTKEY      (byte)0x40  /* Hotkey element */
  178.  
  179. /* Timer watch element comparison kinds (lower nibble of elkind) */
  180.  
  181. #define  TCMP_EQ           (byte)1     /* Equal */
  182. #define  TCMP_NE           (byte)2     /* Not Equal */
  183. #define  TCMP_GE           (byte)3     /* Greater or Equal (unsigned) */
  184. #define  TCMP_LE           (byte)4     /* Less or Equal (unsigned) */
  185. #define  TCMP_GES          (byte)5     /* Greater or Equal (signed) */
  186. #define  TCMP_LES          (byte)6     /* Less or Equal (signed) */
  187. #define  TCMP_CHG          (byte)7     /* Change in value */
  188.  
  189. /* Timer queue element control structure pointer kinds */
  190.  
  191. #define  TKIND_TASK        (byte)1     /* tcbptr, Wakeup associated task */
  192. #define  TKIND_WAKE        (byte)2     /* tcbptr, but not same task */
  193. #define  TKIND_PROC        (byte)3     /* call function */
  194. #define  TKIND_FLAG        (byte)4     /* flagptr, set flag */
  195. #define  TKIND_COUNTER     (byte)5     /* counterptr, increment counter */
  196. #define  TKIND_COUNTDEC    (byte)6     /* counterptr, decrement counter */
  197.  
  198. /* Timer queue element flags */
  199.  
  200. #define  TFLAG_BUSY        (byte)0x01  /* Timer task is busy processing element */
  201. #define  TFLAG_ENQUEUE     (byte)0x02  /* Enqueue after processing */
  202. #define  TFLAG_UNQUEUE     (byte)0x04  /* Don't enqueue after processing */
  203. #define  TFLAG_REMOVE      (byte)0x08  /* Free element after processing */
  204. #define  TFLAG_REPEAT      (byte)0x40  /* Bit set signals repeat processing */
  205. #define  TFLAG_TEMP        (byte)0x80  /* Bit set means temporary element */
  206.  
  207. /* Name link and queue head structure types */
  208.  
  209. #define  Q_HEAD         (byte)0x80     /* Queue head flag */
  210.  
  211. #define  TYP_GROUP      (byte)0
  212. #define  TYP_TCB        (byte)1
  213. #define  TYP_FLAG       (byte)2
  214. #define  TYP_RESOURCE   (byte)3
  215. #define  TYP_COUNTER    (byte)4
  216. #define  TYP_MAILBOX    (byte)5
  217. #define  TYP_PIPE       (byte)6
  218. #define  TYP_WPIPE      (byte)7
  219. #define  TYP_BUFFER     (byte)8
  220. #define  TYP_TIMER      (byte)9
  221. #define  TYP_WATCH     (byte)10
  222. #define  TYP_HOTKEY    (byte)11
  223.  
  224. #define  NAMELENGTH     9  /* For structure names: 8 bytes + zero */
  225.  
  226. /* Installation flags */
  227.  
  228. #define IFL_VIDEO      0x0001      /* Install INT 10 access resource */
  229. #define IFL_DISK       0x0002      /* Install INT 13 access resource */
  230. #define IFL_INT8_DIR   0x0004      /* Call original INT 8 directly */
  231. #define IFL_PRINTER    0x0008      /* Install INT 17 handler */
  232. #define IFL_INT15      0x0010      /* Install IBM-AT INT 15 handler */
  233. #define IFL_NODOSVARS  0x0020      /* Don't swap DOS variables */
  234. #define IFL_NOEXITCHECK 0x0040     /* Don't check for premature exit */
  235.  
  236. #define IFL_STD   (IFL_DISK | IFL_PRINTER | IFL_INT15)   /* Standard flags */
  237.  
  238. /* 
  239.    Size of the DOS variable swap area plus 8 bytes.
  240.    40+8 is more than sufficient for all current versions of DOS up to
  241.    DOS 4.01.
  242. */
  243.  
  244. #define DOSSWAPSIZE     0x30
  245.  
  246. /* --------------------------------------------------------------------- */
  247.  
  248. /*
  249.    The following types define 80x87 data. They are used only when
  250.    NDP is defined TRUE in tskconf.h.
  251.    The type definitions and the basic algorithms for saving the
  252.    coprocessor state were provided by Dan Heine.
  253. */
  254.  
  255. /* Temporary real (80-bit) type */
  256.  
  257. typedef struct {
  258.                word  mant15_0;
  259.                word  mant31_16;
  260.                word  mant47_32;
  261.                word  mant63_48;
  262.                word  s_exponent;
  263.                } t_real;
  264.  
  265. typedef t_real far *t_realptr;
  266.  
  267. /* 80x87 state save area */
  268.  
  269. typedef struct {
  270.                word     control87;
  271.                word     status87;
  272.                word     tag87;
  273.                word     iplo87;
  274.                word     iphi87_opcode87;
  275.                word     opaddrlo87;
  276.                word     opaddrhi87_null;
  277.                t_real   st0;
  278.                t_real   st1;
  279.                t_real   st2;
  280.                t_real   st3;
  281.                t_real   st4;
  282.                t_real   st5;
  283.                t_real   st6;
  284.                t_real   st7;
  285.                } ndpsave_t;
  286.  
  287. typedef ndpsave_t far *ndpsaveptr;
  288.  
  289. /* --------------------------------------------------------------------- */
  290.  
  291. typedef struct callchain_rec far *callchainptr;
  292. typedef void (Taskfunc *funcptr_ccp)(callchainptr);
  293.  
  294. typedef struct callchain_rec {
  295.                              callchainptr next;    /* Must be first */
  296.                              funcptr_ccp  func;
  297.                              farptr       user_ptr;
  298. #if (TSK_DYNAMIC)
  299.                              byte         flags;
  300. #endif
  301.                              } callchain;
  302.  
  303. /* --------------------------------------------------------------------- */
  304.  
  305. /*
  306.    The 'queue' structure is a dual link for linking task control blocks 
  307.    and timer blocks. The first three fields are used both for the queue
  308.    head, and for elements to be inserted in a queue.
  309.  
  310.    CAUTION: Do not change the order of the first three fields in
  311.             either queue or queue_head!
  312. */
  313.  
  314. typedef struct {
  315.                 word     prior;
  316.                 word     ini_prior;
  317.                } qelem_pri;
  318.  
  319. typedef struct queue_rec far *queptr;
  320.  
  321. typedef struct queue_rec {
  322.                           volatile queptr next;
  323.                           volatile queptr prev;
  324.                           byte            kind;
  325.                           union  {
  326.                                  qelem_pri   pri;
  327.                                  dword       ticks;
  328.                                  } el;
  329.                          } queue;
  330.  
  331.  
  332. typedef struct {
  333.                volatile queptr first;
  334.                volatile queptr last;
  335.                byte            kind;
  336.                } queue_head;
  337.  
  338. typedef queue_head far *queheadptr;
  339.  
  340. typedef struct name_rec far *nameptr;
  341.  
  342. struct name_rec {
  343.                 queue_head list;          /* Name list */
  344.                 farptr     strucp;        /* Top of structure pointer */
  345.                 char       name [NAMELENGTH];
  346.                 };
  347.  
  348. typedef struct name_rec namerec;
  349.  
  350. typedef struct tcb_rec far *tcbptr;
  351. typedef tcbptr far *tqueptr;
  352.  
  353. typedef struct tlink_rec far *tlinkptr;
  354.  
  355. struct telem_memwatch {
  356.                       wordptr address;
  357.                       word    mask;
  358.                       word    compare;
  359.                       };
  360.  
  361. struct telem_portwatch {
  362.                         word  port;
  363.                         word  mask;
  364.                         word  compare;
  365.                         byte  in_word;
  366.                        };
  367.  
  368. typedef struct {
  369.                byte  mask;
  370.                byte  compare;
  371.                } kbflag;
  372.  
  373. struct telem_hotkey {
  374.                         kbflag   flag1;
  375.                         kbflag   flag2;
  376.                         kbflag   flag3;
  377.                         byte     scancode;
  378.                        };
  379.  
  380. struct telem_timeout {
  381.                      dword    reload;     /* Timeout counter reload value */
  382.                      };
  383.  
  384. typedef struct tlink_rec tlink;
  385.  
  386. struct tlink_rec {
  387.                   queue    link;
  388.                   tlinkptr next;       /* for exclusive use by timer task */
  389.                   farptr   strucp;     /* Pointer to control structure */
  390.                   dword    user_parm;  /* User defined parameter */
  391. #if (GROUPS)
  392.                   queue_head chain;      /* Timer control block chain */
  393. #endif
  394.                   union {
  395.                         struct telem_memwatch   mem;
  396.                         struct telem_portwatch  port;
  397.                         struct telem_timeout    time;
  398.                         struct telem_hotkey     key;
  399.                         }  elem;
  400.                   byte     elkind;     /* Kind of watch element */
  401.                   byte     struckind;  /* Kind of structure pointed to */
  402.                   byte     flags;      /* Flags */
  403.                  };
  404.  
  405.  
  406. typedef struct group_rec gcb;
  407. typedef gcb far *gcbptr;
  408.  
  409. struct tcb_rec {
  410.                queue          cqueue;     /* Current queue link */
  411.                queheadptr     qhead;      /* Queue head pointer */
  412.                byteptr        stkbot;     /* Task stack bottom */
  413.                volatile byte  state;      /* Task state */
  414.                byte           flags;      /* Task flags */
  415.  
  416.                byteptr        stack;      /* Task register save area */
  417.                word           t_ax;
  418.                word           t_cx;
  419.                word           t_dx;
  420.                word           t_si;
  421.                word           t_di;
  422.                word           t_bp;
  423.                word           t_es;
  424.                word           t_ds;
  425.  
  426.                tlink           timerq;     /* Timer queue link */
  427.                volatile farptr retptr;     /* Event return pointer */
  428.                volatile int    retsize;    /* Return buffer size for pipes */
  429.  
  430.                funcptr     save_func;
  431.                funcptr     rest_func;
  432.                farptr      user_ptr;   /* User defined parameter */
  433. #if (GROUPS)
  434.                gcbptr      group;      /* Current Group control block pointer */
  435.                gcbptr      homegroup;  /* Creating Group control block pointer */
  436. #endif
  437. #if (DOS)
  438.                funcptr     sched_ent_func; /* Used by DOS handler */
  439.                volatile byte indos;      /* Flag for use by DOS handler */
  440.                volatile byte t_new;    /* New task flag for DOS save */
  441.                word        base_psp;   /* Base PSP segment */
  442.                dword       psp_sssp;   /* Saved PSP SS/SP value */
  443.                byte        swap_area [DOSSWAPSIZE]; /* DOS vars save area */
  444. #endif
  445. #if (TSK_NAMED)
  446.                namerec     name;
  447. #endif
  448. #if (EMS)
  449.                byte        ems_map [EMS_SAVE_SIZE];
  450. #endif
  451. #if (NDP)
  452.                ndpsave_t   ndpsave;    /* 80x87 save area */
  453. #endif
  454.                };
  455.  
  456. typedef struct tcb_rec tcb;
  457.  
  458. struct group_rec {
  459.                  gcbptr    home;       /* Home group */
  460.                  gcbptr    level;      /* Next group on level */
  461.                  gcbptr    branch;     /* Next group on higher level */
  462.                  tcbptr    creator;    /* Task that created this group */
  463.                  dword     exit_addr;  /* Exit address */
  464.                  word      create_psp; /* Base PSP addr of this group */
  465.                  word      save_psp;   /* PSP addr of this group's creator */
  466.                  dword     save_sssp;  /* SS/SP from base PSP of creator */
  467.                  namerec   namelist;   /* List of structures in this group */
  468.                  tcbptr    main_ptr;   /* Main task for this group */
  469.                  callchainptr remove;   /* Int handler remove chain */
  470.                  queue_head  telem_list;  /* Timer element chain */
  471.                  queue_head  ticker_list; /* Timer element chain */
  472. #if (TSK_DYNAMIC)
  473.                  farptr (Globalfunc *palloc)(word); /* alloc function ptr */
  474.                  farptr (Globalfunc *pfree)(farptr);  /* free function ptr */
  475. #endif
  476.                  };
  477.  
  478.  
  479. typedef struct {
  480.                queue_head   wait_set;
  481.                queue_head   wait_clear;
  482.                volatile int state;
  483. #if (TSK_DYNAMIC)
  484.                byte         flags;
  485. #endif
  486. #if (TSK_NAMED)
  487.                namerec      name;
  488. #endif
  489.                } flag;
  490.  
  491. typedef flag far *flagptr;
  492.  
  493. typedef struct {
  494.                queue_head     wait_set;
  495.                queue_head     wait_clear;
  496.                volatile dword state;
  497. #if (TSK_DYNAMIC)
  498.                byte           flags;
  499. #endif
  500. #if (TSK_NAMED)
  501.                namerec        name;
  502. #endif
  503.                } counter;
  504.  
  505. typedef counter far *counterptr;
  506.  
  507. typedef struct {
  508.                queue_head        waiting;
  509.                volatile tcbptr   owner;
  510.                volatile word     count;
  511. #if (TSK_DYNAMIC)
  512.                byte              flags;
  513. #endif
  514. #if (TSK_NAMED)
  515.                namerec           name;
  516. #endif
  517.                } resource;
  518.  
  519. typedef resource far *resourceptr;
  520.  
  521. typedef struct msg_header far *msgptr;
  522.  
  523. struct msg_header {
  524.                   volatile msgptr next;
  525.                   };
  526.  
  527. typedef struct {
  528.                queue_head  waiting;
  529.                msgptr      mail_first;
  530.                msgptr      mail_last;
  531. #if (TSK_DYNAMIC)
  532.                byte        flags;
  533. #endif
  534. #if (TSK_NAMED)
  535.                namerec     name;
  536. #endif
  537.                } mailbox;
  538.  
  539. typedef mailbox far *mailboxptr;
  540.  
  541. typedef struct {
  542.                queue_head  wait_read;
  543.                queue_head  wait_write;
  544.                queue_head  wait_clear;
  545.                word        bufsize;
  546.                volatile word filled;
  547.                volatile word inptr;
  548.                volatile word outptr;
  549.                byteptr     contents;
  550. #if (TSK_DYNAMIC)
  551.                byte        flags;
  552. #endif
  553. #if (TSK_NAMED)
  554.                namerec     name;
  555. #endif
  556.                } pipe;
  557.  
  558. typedef pipe far *pipeptr;
  559.  
  560. typedef struct {
  561.                queue_head  wait_read;
  562.                queue_head  wait_write;
  563.                queue_head  wait_clear;
  564.                word        bufsize;
  565.                volatile word filled;
  566.                volatile word inptr;
  567.                volatile word outptr;
  568.                wordptr     wcontents;
  569. #if (TSK_DYNAMIC)
  570.                byte        flags;
  571. #endif
  572. #if (TSK_NAMED)
  573.                namerec     name;
  574. #endif
  575.                } wpipe;
  576.  
  577. typedef wpipe far *wpipeptr;
  578.  
  579. typedef struct {
  580.                resource    buf_write;
  581.                resource    buf_read;
  582.                wpipe       pip;
  583.                volatile word msgcnt;
  584. #if (TSK_DYNAMIC)
  585.                byte    flags;
  586. #endif
  587. #if (TSK_NAMED)
  588.                namerec     name;
  589. #endif
  590.                } buffer;
  591.  
  592. typedef buffer far *bufferptr;
  593.  
  594.  
  595. typedef struct ticker_rec far *tick_ptr;
  596.  
  597. typedef struct ticker_rec {
  598.                           volatile tick_ptr next;
  599.                           volatile dword ticks;
  600. #if (GROUPS)
  601.                           queue_head chain;
  602. #endif
  603. #if (TSK_DYNAMIC)
  604.                           byte     flags;
  605. #endif
  606.                           } ticker;
  607.  
  608. /* --------------------------------------------------------------------- */
  609.  
  610. #if TSK_NAMEPAR
  611. #define TN(name_def) ,name_def
  612. #else
  613. #define TN(name_def) 
  614. #endif
  615.  
  616. #define CRITICAL  int crit_intsav
  617. #define C_ENTER   crit_intsav = tsk_dis_int()
  618. #define C_LEAVE   tsk_ena_int (crit_intsav)
  619.  
  620.  
  621. /* module tskmain */
  622.  
  623. extern int ctask_active;
  624.  
  625. extern int Globalfunc install_tasker (byte varpri, int speedup, word flags
  626.                                       TN(byteptr name));
  627. extern void Globalfunc remove_tasker (void);
  628.  
  629.  
  630. /* module tskutil */
  631.  
  632. extern void Globalfunc preempt_off (void);
  633. extern void Globalfunc preempt_on (void);
  634. extern void Globalfunc tsk_ena_preempt (void);
  635. extern void Globalfunc tsk_dis_preempt (void);
  636. extern int Globalfunc t_delay (dword ticks);
  637. extern callchainptr Globalfunc chain_removefunc (funcptr_ccp func, callchainptr chain, farptr user_ptr);
  638. extern void Globalfunc unchain_removefunc (callchainptr chain);
  639. extern word Globalfunc ticks_per_sec (void);
  640.  
  641.  
  642. /* module tskasm */
  643.  
  644. extern void Globalfunc schedule (void);
  645. extern void Globalfunc c_schedule (void);
  646. extern void Globalfunc yield (void);
  647.  
  648. extern int Globalfunc  tsk_dis_int (void);
  649. extern void Globalfunc tsk_ena_int (int);
  650. extern void Globalfunc tsk_nop (void);
  651.  
  652. #if (TSK_TURBO)
  653. /* The following are missing from the TC++ 1.0 DOS.H file */
  654. unsigned char _Cdecl __inportb__(int __portid);
  655. void    _Cdecl    __outportb__    (int __portid, unsigned char __value);
  656. #define inportb __inportb__
  657. #define outportb __outportb__
  658.  
  659. #define tsk_cli()       disable()
  660. #define tsk_sti()       enable()
  661. #define tsk_outp(p,b)   outportb(p,b)
  662. #define tsk_inp(p)      ((byte) inportb(p))
  663. extern word Globalfunc tsk_inpw (word port);
  664. #elif TSK_MSC
  665. #pragma intrinsic(_disable,_enable,outp,inp,inpw)
  666. #define tsk_cli()       _disable()
  667. #define tsk_sti()       _enable()
  668. #define tsk_outp(p,b)   outp(p,b)
  669. #define tsk_inp(p)      ((byte) inp(p))
  670. #define tsk_inpw(p)     ((byte) inpw(p))
  671.  
  672. #define MK_FP(seg,ofs)  ((farptr)(((dword)(seg) << 16) | (word)(ofs)))
  673. #else
  674. extern int Globalfunc  tsk_dis_int (void);
  675. extern void Globalfunc tsk_ena_int (int);
  676. extern void Globalfunc tsk_cli (void);
  677. extern void Globalfunc tsk_sti (void);
  678. #endif
  679.  
  680. #define TMK_FP(seg,ofs) ((farptr)(((dword)(seg) << 16) | (word)(ofs)))
  681. #define TFP_SEG(ptr)    ((word)((dword)ptr >> 16))
  682. #define TFP_OFF(ptr)    ((word)((dword)ptr))
  683. #define TSK_OFFSETOF(struc,elem)  ((word)(&((struc near *)0)->elem))
  684. #define TSK_STRUCTOP(struc,ptr,elem)  ((struc far *)((byteptr)ptr - TSK_OFFSETOF(struc, elem)))
  685. #define LNULL           ((void far *)0L)
  686.  
  687. /* module tsktask */
  688.  
  689. extern int tsk_use_ndp;
  690.  
  691. extern tcbptr Globalfunc create_task (tcbptr task, funcptr func, byteptr stack,
  692.                                       word stksz, word prior, farptr arg
  693.                                       TN(byteptr name));
  694. extern void Globalfunc kill_task (tcbptr task);
  695. extern int Globalfunc start_task (tcbptr task);
  696.  
  697.  
  698. /* module tsktutl */
  699.  
  700. extern int Globalfunc wake_task (tcbptr task);
  701. extern int Globalfunc stop_task (tcbptr task);
  702. extern word Globalfunc get_priority (tcbptr task);
  703. extern void Globalfunc set_priority (tcbptr task, word prior);
  704. extern void Globalfunc set_task_flags (tcbptr task, byte flags);
  705. extern void Globalfunc set_funcs (tcbptr task, funcptr save, funcptr rest);
  706. extern farptr Globalfunc set_user_ptr (tcbptr task, farptr uptr);
  707. extern farptr Globalfunc get_user_ptr (tcbptr task);
  708. extern tcbptr Globalfunc curr_task (void);
  709.  
  710.  
  711. /* module tsktops */
  712.  
  713. extern tlinkptr CGlobalfunc create_timer_elem (tlinkptr elem, dword tout, 
  714.                                                farptr strucp, byte kind, 
  715.                                                int rept, ...);
  716.  
  717. extern tlinkptr CGlobalfunc create_timer (tlinkptr elem, dword tout, farptr strucp,
  718.                                           byte kind, int rept, ...);
  719. extern void Globalfunc enable_timer (tlinkptr elem);
  720. extern void Globalfunc disable_timer (tlinkptr elem);
  721. extern void Globalfunc delete_timer (tlinkptr elem);
  722. extern void CGlobalfunc change_timer (tlinkptr elem, dword tout, int rept, ...);
  723.  
  724.  
  725. /* module tsktsub */
  726.  
  727. extern void Globalfunc enable_watch (tlinkptr elem);
  728.  
  729. #if (CHECKING)
  730. extern void Globalfunc disable_hwatch (tlinkptr elem, byte typ);
  731. extern void Globalfunc delete_hwatch (tlinkptr elem, byte typ);
  732.  
  733. #define disable_watch(elem)   disable_hwatch(elem,TYP_WATCH);
  734. #define delete_watch(elem)    delete_hwatch(elem,TYP_WATCH);
  735.  
  736. #define disable_hotkey(elem)  disable_hwatch(elem,TYP_HOTKEY);
  737. #define delete_hotkey(elem)   delete_hwatch(elem,TYP_HOTKEY);
  738.  
  739. #else
  740. extern void Globalfunc disable_watch (tlinkptr elem);
  741. extern void Globalfunc delete_watch (tlinkptr elem);
  742.  
  743. #define disable_hotkey(elem)  disable_watch(elem);
  744. #define delete_hotkey(elem)   delete_watch(elem);
  745. #endif
  746.  
  747. /* module tskmemw */
  748.  
  749. extern tlinkptr CGlobalfunc create_memory_watch_elem (tlinkptr elem, farptr address, 
  750.                                                  word mask, word compare, 
  751.                                                  byte cmpkind, farptr strucp, 
  752.                                                  byte kind, int rept, ...);
  753. extern tlinkptr CGlobalfunc create_memory_watch (tlinkptr elem, farptr address, 
  754.                                                  word mask, word compare, 
  755.                                                  byte cmpkind, farptr strucp, 
  756.                                                  byte kind, int rept, ...);
  757. extern int Globalfunc wait_memory (farptr address, 
  758.                                    word mask, word compare, byte cmpkind);
  759.  
  760.  
  761. /* module tskporw */
  762.  
  763. extern tlinkptr CGlobalfunc create_port_watch_elem (tlinkptr elem, 
  764.                                                word port, byte in_word,
  765.                                                word mask, word compare, 
  766.                                                byte cmpkind, farptr strucp, 
  767.                                                byte kind, int rept, ...);
  768.  
  769. extern tlinkptr CGlobalfunc create_port_watch (tlinkptr elem, 
  770.                                                word port, byte in_word,
  771.                                                word mask, word compare, 
  772.                                                byte cmpkind, farptr strucp, 
  773.                                                byte kind, int rept, ...);
  774.  
  775. extern int Globalfunc wait_port (word port, byte in_word,
  776.                                  word mask, word compare, byte cmpkind);
  777.  
  778.  
  779. /* module tskhot */
  780.  
  781. #if (HOTKEYS)
  782. extern tlinkptr CGlobalfunc create_hotkey_elem (tlinkptr elem, byte scancode,
  783.                                                 byte mask1, byte flag1,
  784.                                                 byte mask2, byte flag2,
  785.                                                 byte mask3, byte flag3,
  786.                                                 farptr strucp, byte kind, 
  787.                                                 int rept, ...);
  788. extern tlinkptr CGlobalfunc create_hotkey_entry (tlinkptr elem, byte scancode,
  789.                                                  byte mask1, byte flag1,
  790.                                                  byte mask2, byte flag2,
  791.                                                  byte mask3, byte flag3,
  792.                                                  farptr strucp, byte kind, 
  793.                                                  int rept, ...);
  794.  
  795. extern int Globalfunc wait_hotkey (byte scancode,
  796.                                    byte mask1, byte flag1,
  797.                                    byte mask2, byte flag2,
  798.                                    byte mask3, byte flag3);
  799.  
  800. extern void Globalfunc enable_hotkey (tlinkptr elem);
  801.  
  802. #endif
  803.  
  804.  
  805. /* module tskrsc */
  806.  
  807. extern resourceptr Globalfunc create_resource (resourceptr rsc TN(byteptr name));
  808. extern void Globalfunc delete_resource (resourceptr rsc);
  809. extern int Globalfunc request_resource (resourceptr rsc, dword timeout);
  810. extern int Globalfunc c_request_resource (resourceptr rsc);
  811. extern int Globalfunc request_cresource (resourceptr rsc, dword timeout);
  812. extern int Globalfunc c_request_cresource (resourceptr rsc);
  813. extern void Globalfunc release_resource (resourceptr rsc);
  814. extern int Globalfunc check_resource (resourceptr rsc);
  815.  
  816.  
  817. /* module tskflg */
  818.  
  819. extern flagptr Globalfunc create_flag (flagptr flg TN(byteptr name));
  820. extern void Globalfunc delete_flag (flagptr flg);
  821. extern int Globalfunc wait_flag_set (flagptr flg, dword timeout);
  822. extern int Globalfunc wait_flag_clear (flagptr flg, dword timeout);
  823. extern void Globalfunc set_flag (flagptr flg);
  824. extern void Globalfunc clear_flag (flagptr flg);
  825. extern int Globalfunc clear_flag_wait_set (flagptr flg, dword timeout);
  826. extern int Globalfunc check_flag (flagptr flg);
  827.  
  828.  
  829. /* module tskcnt */
  830.  
  831. extern counterptr Globalfunc create_counter (counterptr cnt TN(byteptr name));
  832. extern void Globalfunc delete_counter (counterptr cnt);
  833. extern void Globalfunc clear_counter (counterptr cnt);
  834. extern int Globalfunc wait_counter_set (counterptr cnt, dword timeout);
  835. extern int Globalfunc wait_counter_clear (counterptr cnt, dword timeout);
  836. extern dword Globalfunc inc_counter (counterptr cnt);
  837. extern dword Globalfunc dec_counter (counterptr cnt);
  838. extern dword Globalfunc check_counter (counterptr cnt);
  839. extern void Globalfunc set_counter (counterptr cnt, dword val);
  840.  
  841.  
  842. /* module tskmsg */
  843.  
  844. extern mailboxptr Globalfunc create_mailbox (mailboxptr box TN(byteptr name));
  845. extern void Globalfunc delete_mailbox (mailboxptr box);
  846. extern farptr Globalfunc wait_mail (mailboxptr box, dword timeout);
  847. extern farptr Globalfunc c_wait_mail (mailboxptr box);
  848. extern void Globalfunc send_mail (mailboxptr box, farptr msg);
  849. extern int Globalfunc check_mailbox (mailboxptr box);
  850.  
  851.  
  852. /* module tskpip */
  853.  
  854. extern pipeptr Globalfunc create_pipe (pipeptr pip, farptr buf, word bufsize
  855.                                          TN(byteptr name));
  856. extern void Globalfunc delete_pipe (pipeptr pip);
  857. extern int Globalfunc read_pipe (pipeptr pip, dword timeout);
  858. extern int Globalfunc c_read_pipe (pipeptr pip);
  859. extern int Globalfunc write_pipe (pipeptr pip, byte ch, dword timeout);
  860. extern int Globalfunc c_write_pipe (pipeptr pip, byte ch);
  861. extern int Globalfunc wait_pipe_empty (pipeptr pip, dword timeout);
  862. extern int Globalfunc check_pipe (pipeptr pip);
  863. extern word Globalfunc pipe_free (pipeptr pip);
  864. extern void Globalfunc flush_pipe (pipeptr pip);
  865.  
  866.  
  867. /* module tskwpip */
  868.  
  869. extern wpipeptr Globalfunc create_wpipe (wpipeptr pip, farptr buf, word bufsize
  870.                                            TN(byteptr name));
  871. extern void Globalfunc delete_wpipe (wpipeptr pip);
  872. extern word Globalfunc read_wpipe (wpipeptr pip, dword timeout);
  873. extern word Globalfunc c_read_wpipe (wpipeptr pip);
  874. extern int Globalfunc write_wpipe (wpipeptr pip, word ch, dword timeout);
  875. extern int Globalfunc c_write_wpipe (wpipeptr pip, word ch);
  876. extern int Globalfunc wait_wpipe_empty (wpipeptr pip, dword timeout);
  877. extern word Globalfunc check_wpipe (wpipeptr pip);
  878. extern word Globalfunc wpipe_free (wpipeptr pip);
  879. extern void Globalfunc flush_wpipe (wpipeptr pip);
  880.  
  881.  
  882. /* module tskbuf */
  883.  
  884. extern bufferptr Globalfunc create_buffer (bufferptr buf, farptr pbuf, 
  885.                                            word bufsize TN(byteptr name));
  886. extern void Globalfunc delete_buffer (bufferptr buf);
  887. extern int Globalfunc read_buffer (bufferptr buf, farptr msg, int size, dword timeout);
  888. extern int Globalfunc c_read_buffer (bufferptr buf, farptr msg, int size);
  889. extern int Globalfunc write_buffer (bufferptr buf, farptr msg, int size, dword timeout);
  890. extern int Globalfunc c_write_buffer (bufferptr buf, farptr msg, int size);
  891. extern word Globalfunc check_buffer (bufferptr buf);
  892.  
  893.  
  894. /* module tsksec */
  895.  
  896. extern word Globalfunc t_read_key (void);
  897. extern word Globalfunc t_wait_key (dword timeout);
  898. extern word Globalfunc t_keyhit (void);
  899.  
  900.  
  901. /* module tskalloc (model dependent) */
  902.  
  903. #if (TSK_DYNAMIC && TSK_DYNLOAD)
  904. extern farptr Globalfunc tsk_alloc (word size);
  905. extern farptr Globalfunc tsk_calloc (word item, word size);
  906. extern farptr Globalfunc tsk_free (farptr item);
  907. extern farptr Globalfunc tsk_realloc (farptr item, word size);
  908.  
  909. extern resource Neardata alloc_resource;
  910. #endif
  911.  
  912.  
  913. /* module tskname */
  914.  
  915. #if (TSK_NAMED)
  916. extern farptr Globalfunc find_group_name (gcbptr group, byteptr name, int kind);
  917. extern farptr Globalfunc find_name (byteptr name, int kind);
  918. extern void Globalfunc add_event_name (nameptr elem);
  919. #else
  920. #define add_event_name(x)
  921. #endif
  922.  
  923.  
  924. /* module tsktick */
  925.  
  926. extern tick_ptr Globalfunc create_ticker (tick_ptr elem, dword val);
  927. extern void Globalfunc delete_ticker (tick_ptr elem);
  928. extern void Globalfunc set_ticker (tick_ptr elem, dword val);
  929. extern dword Globalfunc get_ticker (tick_ptr elem);
  930.  
  931.  
  932. /* module tskres */
  933.  
  934. extern int Globalfunc ctask_resident (void);
  935. extern int Globalfunc link_ctask (void);
  936.  
  937. /* --------------------------------------------------------------------- */
  938.  
  939. /* 
  940.    Macros to define and initialize static control structures.
  941.    General usage is:
  942.          DEF_xxx (variable, name [other params]);
  943.    Example:
  944.          DEF_FLAG (my_flag, "MyFlag");
  945. */
  946.  
  947. #define TQH(var,typ)  {(queptr)&var, (queptr)&var, Q_HEAD | typ}
  948. #if (TSK_NAMED)
  949. #define TNR(var,nam,typ) ,{{LNULL, LNULL, typ}, &var, nam}
  950. #else
  951. #define TNR(var,nam,typ)
  952. #endif
  953. #if (TSK_DYNAMIC)
  954. #define TFLG ,0
  955. #else
  956. #define TFLG
  957. #endif
  958.  
  959. #define DEF_FLAG(var,nam) flag var = { TQH(var.wait_set,TYP_FLAG), TQH(var.wait_clear,TYP_FLAG), 0 TFLG TNR(var,nam,TYP_FLAG) }
  960. #define DEF_COUNTER(var,nam) counter var = { TQH(var.wait_set,TYP_COUNTER), TQH(var.wait_clear,TYP_COUNTER), 0L TFLG TNR(var,nam,TYP_COUNTER) }
  961. #define DEF_RESOURCE(var,nam) resource var = { TQH(var.waiting,TYP_RESOURCE), LNULL, 0 TFLG TNR(var,nam,TYP_RESOURCE) }
  962. #define DEF_MAILBOX(var,nam) mailbox var = { TQH(var.waiting,TYP_RESOURCE), LNULL, LNULL TFLG TNR(var,nam,TYP_MAILBOX) }
  963. #define DEF_PIPE(var,nam,buf,len) pipe var = { TQH(var.wait_read,TYP_PIPE), TQH(var.wait_write,TYP_PIPE), TQH(var.wait_clear,TYP_PIPE), \
  964.                                                len, 0, 0, 0, (byteptr)buf TFLG TNR(var,nam,TYP_PIPE) }
  965. #define DEF_WPIPE(var,nam,buf,len) wpipe var = { TQH(var.wait_read,TYP_WPIPE), TQH(var.wait_write,TYP_WPIPE), TQH(var.wait_clear,TYP_WPIPE), \
  966.                                                  len / 2, 0, 0, 0, (wordptr)buf TFLG TNR(var,nam,TYP_WPIPE) }
  967.  
  968.