home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CTASK.ZIP
/
TSK.H
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-23
|
20KB
|
605 lines
/*
--- Version 2.0 89-12-23 23:32 ---
TSK.H - CTask - Type definitions and global routine prototypes.
Public Domain Software written by
Thomas Wagner
Patschkauer Weg 31
D-1000 Berlin 33
West Germany
*/
#include "tskconf.h"
#include <stdio.h>
#if defined(__TURBOC__)
#include <dos.h>
#define _Near
#define TURBO 1
#define MSC 0
#else
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#define _Near near
#define TURBO 0
#define MSC 1
#endif
#define local static /* Comment out the "static" for debugging */
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long dword;
typedef void (cdecl far *funcptr)();
typedef void far *farptr;
typedef byte far *byteptr;
typedef word far *wordptr;
#define TTIMEOUT ((farptr) -1L)
#define TWAKE ((farptr) -2L)
#define TWATCH ((farptr) -3L)
#define TIMEOUT (-1)
#define WAKE (-2)
#define WATCH (-3)
/* Task states */
#define ST_KILLED 0
#define ST_STOPPED 1
#define ST_DELAYED 2
#define ST_WAITING 3
#define ST_ELIGIBLE 4
#define ST_RUNNING 5
/* Task flags */
#define F_TEMP 0x80 /* Task is temporary, free on kill */
#define F_STTEMP 0x40 /* Task stack is temporary, free on kill */
#define F_CRIT 0x01 /* Task is critical, may not be preempted */
#define FL_SYSM 0xf0 /* Mask for system flags */
#define FL_USRM 0x0f /* Mask for user flags */
/* Timer queue element states */
#define TSTAT_IDLE 0 /* Not in queue */
#define TSTAT_REMOVE 1 /* Remove element from queue */
#define TSTAT_WATCH 2 /* Watch element */
#define TSTAT_CONTWATCH 3 /* Continuous Watch element */
#define TSTAT_COUNTDOWN 4 /* Count down timeout, then remove */
#define TSTAT_REPEAT 5 /* Count down, reload when done */
/* Timer queue element action kinds (upper nibble of elkind) */
#define TELEM_TIMER 0x10 /* Timeout element */
#define TELEM_MEM 0x20 /* Memory watch element */
#define TELEM_PORT 0x30 /* Port watch element */
/* Timer watch element comparison kinds (lower nibble of elkind) */
#define TCMP_EQ 1 /* Equal */
#define TCMP_NE 2 /* Not Equal */
#define TCMP_GE 3 /* Greater or Equal (unsigned) */
#define TCMP_LE 4 /* Less or Equal (unsigned) */
#define TCMP_GES 5 /* Greater or Equal (signed) */
#define TCMP_LES 6 /* Less or Equal (signed) */
#define TCMP_CHG 7 /* Change in value */
/* Timer queue element control structure pointer kinds */
#define TKIND_TASK 1 /* tcbptr, Wakeup associated task */
#define TKIND_WAKE 2 /* tcbptr, but not same task */
#define TKIND_PROC 3 /* call function */
#define TKIND_FLAG 4 /* flagptr, set flag */
#define TKIND_COUNTER 5 /* counterptr, increment counter */
/* Timer queue element flags */
#define TFLAG_BUSY 0x01 /* Timer task is busy processing element */
#define TFLAG_TEMP 0x80 /* Bit set means temporary element */
/* Name link structure types */
#define TYP_GROUP 0
#define TYP_TCB 1
#define TYP_FLAG 2
#define TYP_RESOURCE 3
#define TYP_COUNTER 4
#define TYP_MAILBOX 5
#define TYP_PIPE 6
#define TYP_WPIPE 7
#define TYP_BUFFER 8
#define NAMELENGTH 9 /* For structure names: 8 bytes + zero */
/* Installation flags */
#define IFL_VIDEO 0x0001 /* Install INT 10 access resource */
#define IFL_DISK 0x0002 /* Install INT 13 access resource */
#define IFL_INT8_DIR 0x0004 /* Call original INT 8 directly */
#define IFL_PRINTER 0x0008 /* Install INT 17 handler */
#define IFL_INT15 0x0010 /* Install IBM-AT INT 15 handler */
#define IFL_NODOSVARS 0x0020 /* Don't swap DOS variables */
#define IFL_STD (IFL_DISK | IFL_PRINTER | IFL_INT15) /* Standard flags */
/*
Size of the DOS variable swap area plus 8 bytes.
40+8 is more than sufficient for all current versions of DOS up to
DOS 4.01.
*/
#define DOSSWAPSIZE 0x30
/* --------------------------------------------------------------------- */
/*
The 'queue' structure is a dual link for linking task control blocks
and timer blocks. The first three fields are used both for the queue
head, and for elements to be inserted in a queue.
CAUTION: Do not change the order of the first three fields in
either queue or queue_head!
*/
typedef struct {
word prior;
word ini_prior;
} qelem_pri;
typedef struct queue_rec far *queptr;
typedef struct queue_rec {
queptr volatile next;
queptr volatile prev;
byte kind;
union {
qelem_pri pri;
dword ticks;
} el;
} queue;
typedef struct {
queptr volatile first;
queptr volatile last;
byte kind;
} queue_head;
typedef queue_head far *queheadptr;
typedef struct name_rec far *nameptr;
struct name_rec {
queue_head list; /* Name list */
farptr strucp; /* Top of structure pointer */
char name [NAMELENGTH];
};
typedef struct name_rec namerec;
typedef struct tcb_rec far *tcbptr;
typedef tcbptr far *tqueptr;
typedef struct tlink_rec far *tlinkptr;
struct telem_memwatch {
wordptr address;
word mask;
word compare;
};
struct telem_portwatch {
word port;
word mask;
word compare;
byte in_word;
};
struct telem_timeout {
dword reload; /* Timeout counter reload value */
};
struct tlink_rec {
queue link;
farptr strucp; /* Pointer to control structure */
dword user_parm; /* User defined parameter */
union {
struct telem_memwatch mem;
struct telem_portwatch port;
struct telem_timeout time;
} elem;
byte elkind; /* Kind of watch element */
byte tstate; /* Element state */
byte flags; /* Flags */
};
typedef struct tlink_rec tlink;
typedef struct group_rec gcb;
typedef gcb far *gcbptr;
struct tcb_rec {
queue cqueue; /* Current queue link */
queheadptr qhead; /* Queue head pointer */
byteptr stkbot; /* Task stack bottom */
byte state; /* Task state */
byte flags; /* Task flags */
byteptr stack; /* Task register save area */
word t_ax;
word t_cx;
word t_dx;
word t_si;
word t_di;
word t_bp;
word t_es;
word t_ds;
tlink timerq; /* Timer queue link */
farptr retptr; /* Event return pointer */
int retsize; /* Return buffer size for pipes */
funcptr save_func;
funcptr rest_func;
farptr user_ptr;
#if (GROUPS)
gcbptr group; /* Current Group control block pointer */
gcbptr homegroup; /* Creating Group control block pointer */
#endif
#if (DOS)
byte indos; /* Flag for use by DOS handler */
byte new; /* New task flag for DOS save */
word base_psp; /* Base PSP segment */
dword psp_sssp; /* Saved PSP SS/SP value */
byte swap_area [DOSSWAPSIZE]; /* DOS vars save area */
#endif
#if (TSK_NAMED)
namerec name;
#endif
};
typedef struct tcb_rec tcb;
struct group_rec {
gcbptr home; /* Home group */
gcbptr level; /* Next group on level */
gcbptr branch; /* Next group on higher level */
tcbptr creator; /* Task that created this group */
dword exit_addr; /* Exit address */
word create_psp; /* Base PSP addr of this group */
word save_psp; /* PSP addr of this group's creator */
dword save_sssp; /* SS/SP from base PSP of creator */
namerec namelist; /* List of structures in this group */
};
typedef struct {
queue_head wait_set;
queue_head wait_clear;
int state;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} flag;
typedef flag far *flagptr;
typedef struct {
queue_head wait_set;
queue_head wait_clear;
dword state;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} counter;
typedef counter far *counterptr;
typedef struct {
queue_head waiting;
tcbptr owner;
word count;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} resource;
typedef resource far *resourceptr;
struct msg_header {
struct msg_header far *next;
};
typedef struct msg_header far *msgptr;
typedef struct {
queue_head waiting;
msgptr mail_first;
msgptr mail_last;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} mailbox;
typedef mailbox far *mailboxptr;
typedef struct {
queue_head wait_read;
queue_head wait_write;
queue_head wait_clear;
word bufsize;
word filled;
word inptr;
word outptr;
byteptr contents;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} pipe;
typedef pipe far *pipeptr;
typedef struct {
queue_head wait_read;
queue_head wait_write;
queue_head wait_clear;
word bufsize;
word filled;
word inptr;
word outptr;
wordptr wcontents;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} wpipe;
typedef wpipe far *wpipeptr;
typedef struct {
resource buf_write;
resource buf_read;
wpipe pip;
word msgcnt;
#if (TSK_DYNAMIC)
byte flags;
#endif
#if (TSK_NAMED)
namerec name;
#endif
} buffer;
typedef buffer far *bufferptr;
typedef struct ticker_rec far *tick_ptr;
typedef struct ticker_rec {
tick_ptr next;
dword ticks;
#if (TSK_DYNAMIC)
byte flags;
#endif
} ticker;
extern int far install_tasker (byte varpri, int speedup, word flags, byteptr name);
extern void far remove_tasker (void);
extern void far preempt_on (void);
extern void far preempt_off (void);
extern void far tsk_ena_preempt (void);
extern void far tsk_dis_preempt (void);
extern void far schedule (void);
extern void far yield (void);
extern tcbptr far create_task (tcbptr task, funcptr func, byteptr stack,
word stksz, word prior, farptr arg
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far kill_task (tcbptr task);
extern int far start_task (tcbptr task);
extern int far wake_task (tcbptr task);
extern word far get_priority (tcbptr task);
extern void far set_priority (tcbptr task, word prior);
extern void far set_funcs (tcbptr task, funcptr save, funcptr rest);
extern farptr far set_user_ptr (tcbptr task, farptr uptr);
extern farptr far get_user_ptr (tcbptr task);
extern tcbptr far curr_task (void);
extern int far t_delay (dword ticks);
extern tlinkptr far create_timer (tlinkptr elem, dword tout, farptr strucp,
byte kind, byte rept, ...);
extern void far delete_timer (tlinkptr elem);
extern void far change_timer (tlinkptr elem, dword tout, byte rept, ...);
extern tlinkptr far create_memory_watch (tlinkptr elem, farptr address,
word mask, word compare,
byte cmpkind, farptr strucp,
byte kind, byte rept, ...);
extern tlinkptr far create_port_watch (tlinkptr elem,
word port, byte in_word,
word mask, word compare,
byte cmpkind, farptr strucp,
byte kind, byte rept, ...);
extern void far delete_watch (tlinkptr elem);
extern int far wait_memory (farptr address,
word mask, word compare, byte cmpkind);
extern int far wait_port (word port, byte in_word,
word mask, word compare, byte cmpkind);
extern resourceptr far create_resource (resourceptr rsc
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_resource (resourceptr rsc);
extern void far release_resource (resourceptr rsc);
extern int far request_resource (resourceptr rsc, dword timeout);
extern int far request_cresource (resourceptr rsc, dword timeout);
extern int far c_request_resource (resourceptr rsc);
extern int far c_request_cresource (resourceptr rsc);
extern int far check_resource (resourceptr rsc);
extern flagptr far create_flag (flagptr flg
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_flag (flagptr flg);
extern void far set_flag (flagptr flg);
extern void far clear_flag (flagptr flg);
extern int far clear_flag_wait_set (flagptr flg, dword timeout);
extern int far wait_flag_set (flagptr flg, dword timeout);
extern int far wait_flag_clear (flagptr flg, dword timeout);
extern int far check_flag (flagptr flg);
extern counterptr far create_counter (counterptr cnt
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_counter (counterptr cnt);
extern void far clear_counter (counterptr cnt);
extern int far wait_counter_set (counterptr cnt, dword timeout);
extern int far wait_counter_clear (counterptr cnt, dword timeout);
extern void far inc_counter (counterptr cnt);
extern void far set_counter (counterptr cnt, dword val);
extern dword far check_counter (counterptr cnt);
extern mailboxptr far create_mailbox (mailboxptr box
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_mailbox (mailboxptr box);
extern void far send_mail (mailboxptr box, farptr msg);
extern farptr far wait_mail (mailboxptr box, dword timeout);
extern farptr far c_wait_mail (mailboxptr box);
extern int far check_mailbox (mailboxptr box);
extern pipeptr far create_pipe (pipeptr pip, farptr buf, word bufsize
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_pipe (pipeptr pip);
extern int far read_pipe (pipeptr pip, dword timeout);
extern int far c_read_pipe (pipeptr pip);
extern int far write_pipe (pipeptr pip, byte ch, dword timeout);
extern int far c_write_pipe (pipeptr pip, byte ch);
extern int far wait_pipe_empty (pipeptr pip, dword timeout);
extern int far check_pipe (pipeptr pip);
extern word far pipe_free (pipeptr pip);
extern void far flush_pipe (pipeptr pip);
extern wpipeptr far create_wpipe (wpipeptr pip, farptr buf, word bufsize
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_wpipe (wpipeptr pip);
extern word far read_wpipe (wpipeptr pip, dword timeout);
extern word far c_read_wpipe (wpipeptr pip);
extern int far write_wpipe (wpipeptr pip, word ch, dword timeout);
extern int far c_write_wpipe (wpipeptr pip, word ch);
extern int far wait_wpipe_empty (wpipeptr pip, dword timeout);
extern word far check_wpipe (wpipeptr pip);
extern word far wpipe_free (wpipeptr pip);
extern void far flush_wpipe (wpipeptr pip);
extern bufferptr far create_buffer (bufferptr buf, farptr pbuf, word bufsize
#if (TSK_NAMED)
,byteptr name
#endif
);
extern void far delete_buffer (bufferptr buf);
extern int far read_buffer (bufferptr buf, farptr msg, int size, dword timeout);
extern int far c_read_buffer (bufferptr buf, farptr msg, int size);
extern int far write_buffer (bufferptr buf, farptr msg, int size, dword timeout);
extern int far c_write_buffer (bufferptr buf, farptr msg, int size);
extern word far check_buffer (bufferptr buf);
extern word far t_read_key (void);
extern word far t_wait_key (dword timeout);
extern word far t_keyhit (void);
extern int far tsk_dis_int (void);
extern void far tsk_ena_int (int);
extern void far tsk_nop (void);
#if (TURBO)
#define tsk_cli() disable()
#define tsk_sti() enable()
#define tsk_outp(p,b) outportb(p,b)
#define tsk_inp(p) ((byte) inportb(p))
extern word far tsk_inpw (word port);
#elif MSC
#pragma intrinsic(_disable,_enable,outp,inp,inpw)
#define tsk_cli() _disable()
#define tsk_sti() _enable()
#define tsk_outp(p,b) outp(p,b)
#define tsk_inp(p) ((byte) inp(p))
#define tsk_inpw(p) ((byte) inpw(p))
#define MK_FP(seg,ofs) ((farptr)(((dword)(seg) << 16) | (word)(ofs)))
#else
extern int far tsk_dis_int (void);
extern void far tsk_ena_int (int);
extern void far tsk_cli (void);
extern void far tsk_sti (void);
#endif
#define CRITICAL int crit_intsav
#define C_ENTER crit_intsav = tsk_dis_int()
#define C_LEAVE tsk_ena_int (crit_intsav)
#if (TSK_DYNAMIC)
extern farptr far tsk_alloc (word size);
extern void far tsk_free (farptr item);
extern resource _Near alloc_resource;
#endif
#if (TSK_NAMED)
extern farptr far find_group_name (gcbptr group, byteptr name, int kind);
extern farptr far find_name (byteptr name, int kind);
#endif
extern tick_ptr far create_ticker (tick_ptr elem, dword val);
extern void far delete_ticker (tick_ptr elem);
extern void far set_ticker (tick_ptr elem, dword val);
extern dword far get_ticker (tick_ptr elem);
extern int far ctask_resident (void);