home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctask.zip / TSK.H < prev    next >
C/C++ Source or Header  |  1988-03-01  |  8KB  |  246 lines

  1. /*
  2.    TSK.H - CTask - Type definitions and global routine prototypes.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13.  
  14. #if defined(OPEN_MAX)      /* Only defined in Turbo C stdio.h */
  15. #define _Near
  16. #else
  17. #define _Near near
  18. #endif
  19.  
  20. #define local static       /* Comment out the "static" for debugging */
  21.  
  22. typedef unsigned char byte;
  23. typedef unsigned short word;
  24. typedef unsigned long dword;
  25. typedef void (cdecl far *funcptr)();
  26. typedef void far *farptr;
  27. typedef byte far *byteptr;
  28. typedef word far *wordptr;
  29.  
  30. #define TTIMEOUT ((farptr) -1L)
  31. #define TWAKE    ((farptr) -2L)
  32.  
  33. /* Task states */
  34.  
  35. #define    ST_KILLED   0
  36. #define  ST_STOPPED  1
  37. #define  ST_DELAYED  2
  38. #define  ST_WAITING  3
  39. #define  ST_ELIGIBLE 4
  40. #define  ST_RUNNING  5
  41.  
  42. /* Task flags */
  43.  
  44. #define  F_TIMER  0x80     /* Task is enqueued in timer queue */
  45. #define  F_CRIT   0x01     /* Task is critical, may not be preempted */
  46.  
  47. #define  FL_SYSM  0xf0     /* Mask for system flags */
  48. #define  FL_USRM  0x0f     /* Mask for user flags */
  49.  
  50. typedef struct tcb_rec far *tcbptr;
  51. typedef tcbptr far *tqueptr;
  52.  
  53. typedef struct dlink_rec far *dlinkptr;
  54.  
  55. struct dlink_rec {
  56.                   dlinkptr follow;
  57.                   dlinkptr prev;
  58.                   dword    timeout;    /* Timeout counter */
  59.                   tcbptr   tcbp;
  60.                  };
  61.  
  62. typedef struct dlink_rec dlink;
  63.  
  64.  
  65. struct tcb_rec {
  66.                    tcbptr   next;       /* Next in queue */
  67.                    tqueptr  queue;       /* Queue head pointer */
  68.                    byteptr  stack;      /* Task stack */
  69.                    byteptr  stkbot;     /* Task stack bottom */
  70.                    word     prior;      /* Task priority */
  71.                    word     initprior;  /* Initial Task priority */
  72.                byte     state;      /* Task state */
  73.                byte     flags;      /* Task flags */
  74.                dlink    timerq;     /* Timer queue link */
  75.                farptr   retptr;     /* Event return pointer */
  76.                int      retsize;    /* Return buffer size for pipes */
  77.                   };
  78.  
  79. typedef struct tcb_rec tcb;
  80.  
  81. typedef struct {
  82.                tcbptr  wait_set;
  83.                tcbptr  wait_clear;
  84.                int     state;
  85.                } flag;
  86.  
  87. typedef flag far *flagptr;
  88.  
  89. typedef struct {
  90.                tcbptr  wait_set;
  91.                tcbptr  wait_clear;
  92.                dword   state;
  93.                } counter;
  94.  
  95. typedef counter far *counterptr;
  96.  
  97. typedef struct {
  98.                tcbptr   waiting;
  99.                tcbptr   owner;
  100.                int      state;
  101.                } resource;
  102.  
  103. typedef resource far *resourceptr;
  104.  
  105. struct msg_header {
  106.                   struct msg_header far *next;
  107.                   };
  108.  
  109. typedef struct msg_header far *msgptr;
  110.  
  111. typedef struct {
  112.                tcbptr  waiting;
  113.                msgptr  mail_first;
  114.                msgptr  mail_last;
  115.                } mailbox;
  116.  
  117. typedef mailbox far *mailboxptr;
  118.  
  119. typedef struct {
  120.                tcbptr   wait_read;
  121.                tcbptr   wait_write;
  122.                tcbptr   wait_clear;
  123.                word     bufsize;
  124.                word     filled;
  125.                word     inptr;
  126.                word     outptr;
  127.                byteptr  contents;
  128.                } pipe;
  129.  
  130. typedef pipe far *pipeptr;
  131.  
  132. typedef struct {
  133.                tcbptr   wait_read;
  134.                tcbptr   wait_write;
  135.                tcbptr   wait_clear;
  136.                word     bufsize;
  137.                word     filled;
  138.                word     inptr;
  139.                word     outptr;
  140.                wordptr  wcontents;
  141.                } wpipe;
  142.  
  143. typedef wpipe far *wpipeptr;
  144.  
  145. typedef struct {
  146.                resource    buf_write;
  147.                resource    buf_read;
  148.                wpipe       pip;
  149.                word        msgcnt;
  150.                } buffer;
  151.  
  152. typedef buffer far *bufferptr;
  153.  
  154.  
  155. extern void far install_tasker (byte varpri, int speedup);
  156. extern void far remove_tasker (void);
  157. extern void far preempt_on (void);
  158. extern void far preempt_off (void);
  159.  
  160. extern void far schedule (void);
  161. extern void far create_task (tcbptr task, funcptr func, byteptr stack,
  162.                               word stksz, word prior, farptr arg);
  163. extern void far kill_task (tcbptr task);
  164. extern int far start_task (tcbptr task);
  165. extern int far wake_task (tcbptr task);
  166. extern void far set_priority (tcbptr task, word prior);
  167.  
  168. extern int far t_delay (dword ticks);
  169.  
  170. extern void far create_resource (resourceptr rsc);
  171. extern void far delete_resource (resourceptr rsc);
  172. extern void far release_resource (resourceptr rsc);
  173. extern int far request_resource (resourceptr rsc, dword timeout);
  174. extern int far c_request_resource (resourceptr rsc);
  175. extern int far check_resource (resourceptr rsc);
  176.  
  177. extern void far create_flag (flagptr flg);
  178. extern void far delete_flag (flagptr flg);
  179. extern void far set_flag (flagptr flg);
  180. extern void far clear_flag (flagptr flg);
  181. extern int far wait_flag_set (flagptr flg, dword timeout);
  182. extern int far wait_flag_clear (flagptr flg, dword timeout);
  183. extern int far check_flag (flagptr flg);
  184.  
  185. extern void far create_counter (counterptr cnt);
  186. extern void far delete_counter (counterptr cnt);
  187. extern void far clear_counter (counterptr cnt);
  188. extern int far wait_counter_set (counterptr cnt, dword timeout);
  189. extern int far wait_counter_clear (counterptr cnt, dword timeout);
  190. extern void far inc_counter (counterptr cnt);
  191. extern dword far check_counter (counterptr cnt);
  192.  
  193. extern void far create_mailbox (mailboxptr box);
  194. extern void far delete_mailbox (mailboxptr box);
  195. extern void far send_mail (mailboxptr box, farptr msg);
  196. extern farptr far wait_mail (mailboxptr box, dword timeout);
  197. extern farptr far c_wait_mail (mailboxptr box);
  198. extern int far check_mailbox (mailboxptr box);
  199.  
  200. extern void far create_pipe (pipeptr pip, farptr buf, word bufsize);
  201. extern void far delete_pipe (pipeptr pip);
  202. extern int far read_pipe (pipeptr pip, dword timeout);
  203. extern int far c_read_pipe (pipeptr pip);
  204. extern int far write_pipe (pipeptr pip, byte ch, dword timeout);
  205. extern int far c_write_pipe (pipeptr pip, byte ch);
  206. extern int far wait_pipe_empty (pipeptr pip, dword timeout);
  207. extern int far check_pipe (pipeptr pip);
  208. extern word far pipe_free (pipeptr pip);
  209.  
  210. extern void far create_wpipe (wpipeptr pip, farptr buf, word bufsize);
  211. extern void far delete_wpipe (wpipeptr pip);
  212. extern word far read_wpipe (wpipeptr pip, dword timeout);
  213. extern word far c_read_wpipe (wpipeptr pip);
  214. extern int far write_wpipe (wpipeptr pip, word ch, dword timeout);
  215. extern int far c_write_wpipe (wpipeptr pip, word ch);
  216. extern int far wait_wpipe_empty (wpipeptr pip, dword timeout);
  217. extern word far check_wpipe (wpipeptr pip);
  218. extern word far wpipe_free (wpipeptr pip);
  219.  
  220. extern void far create_buffer (bufferptr buf, farptr pbuf, word bufsize);
  221. extern void far delete_buffer (bufferptr buf);
  222. extern int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout);
  223. extern int far c_read_buffer (bufferptr buf, farptr msg, int size);
  224. extern int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout);
  225. extern int far c_write_buffer (bufferptr buf, farptr msg, int size);
  226. extern word far check_buffer (bufferptr buf);
  227.  
  228. extern word far t_read_key (void);
  229. extern word far t_wait_key (dword timeout);
  230. extern word far t_keyhit (void);
  231.  
  232. extern int far  tsk_dis_int (void);
  233. extern void far tsk_ena_int (int);
  234. extern void far tsk_cli (void);
  235. extern void far tsk_sti (void);
  236. extern void far tsk_nop (void);
  237.  
  238. extern void far tsk_outp (int port, byte b);
  239. extern byte far tsk_inp (int port);
  240.  
  241. #define CRITICAL  int crit_intsav
  242. #define C_ENTER   crit_intsav = tsk_dis_int()
  243. #define C_LEAVE   tsk_ena_int (crit_intsav)
  244.  
  245.  
  246.