⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmtlib.h

📁 简单实现多任务的C语言库 可以实现各个任务间的简单的消息通讯
💻 H
字号:
#ifndef __CMTLIB_H__

#ifndef __LARGE__
#error CMTLIB only works in large mode.
#endif

#include <stdio.h>

/**********************************************************************/

/*
** CMT - cooperative multitasking engine.
*/

#define CMT_DEFAULTSTACKSIZE 1024

/*man**************************************************************
NAME
	CMT_TASK - task structure

DESCRIPTION
	CMT_TASK is a basic task structure, each forked task gets
	one created for it. the structure has following elements:

	  CMT_TASK far *next,far *prev;
		pointers for creating double-linked task list.
		next points to task that will run next when the
		cmt_pause() is called. If cmt_fork() is ever
		used, then there will always be one task which
		will never terminate, if all forked tasks terminte
		then system task's link pointers will point to
		itself.
	
	  unsigned short far *stack;
		pointer to stack storage. cmt_fork allocates
		stack space for new tasks using cmt_malloc()

	  unsigned short id;
		task ID

	  unsigned short ss,sp;
		current stack pointer and segment for this task

	  void far *message;
		storage for message

	  void far (*entertask)(void);
	  void far (*leavetask)(void);
	  void far (*killtask)(void);

		these 3 are user-defined handers which are called
		with following conditions being true:

		entertask: _cmtactivetask points to task about to
		be run, stack is switched to this task's stack.

		leavetask: _cmtactivetask still points to task
		which cmt_pause()-d and stack is not switched yet.

		killtask: _cmtactivetask points to task which is
		about to die, stack still points to this task's stack.
		
		you can set these handlers after the cmt_fork() as
		cmt_fork only creates the task. There is one exception -
		on the first cmt_fork(), the system task is automatically
		created and _cmtsystemtask is set to point at it.
******************************************************************/
typedef struct _cmt_task CMT_TASK;
struct _cmt_task {
	CMT_TASK far *next,far *prev;
	unsigned short far *stack;
	unsigned short id;
	unsigned short ss,sp;
	void far *message;
	/* user hooks */
	void far (*entertask)(void);
	void far (*leavetask)(void);
	void far (*killtask)(void);
};

typedef struct {
	int count;
} CMT_SEM;

typedef struct {
	int size;
	int nmsgs;
	int head;
	int tail;
	void far *port[];
} CMT_PORT;

typedef struct {
	unsigned long start;
	unsigned long length;
	unsigned long end;
} CMT_TIMER;

extern unsigned int cmt_tasksactive;
extern CMT_TASK far *_cmtactivetask,far *_cmtsystemtask;

#ifdef __cplusplus
extern "C" {
#endif
extern void far cdecl cmt_tstart(CMT_TIMER far *t,unsigned long ticks);
extern int far cdecl cmt_texpired(CMT_TIMER far *t);
extern void far cdecl cmt_pause(void);
extern CMT_TASK far * cdecl cmt_fork(void far cdecl (*task)(void),
				unsigned int stacksize);
extern void far cdecl cmt_sleep(unsigned int seconds);
extern CMT_SEM far * far cdecl cmt_sdelete(CMT_SEM far *sem);
extern CMT_SEM far * far cdecl cmt_screate(int initcount);
extern void far cdecl cmt_wait(CMT_SEM far *sem);
extern void far cdecl cmt_signal(CMT_SEM far *sem);
extern int far cdecl cmt_send(void far *msg,CMT_TASK far *task);
extern void far * far cdecl cmt_receive(void);
extern void far * far cdecl cmt_recvtim(unsigned int tenths);
extern void far * far cdecl cmt_recvclr(void);
extern CMT_PORT far * far cdecl cmt_pdelete(CMT_PORT far *port);
extern CMT_PORT far * far cdecl cmt_pcreate(int size);
extern void far cdecl cmt_psend(CMT_PORT far *port,void far *message);
extern int far cdecl cmt_psendnobl(CMT_PORT far *port,void far *message);
extern void far * far cdecl cmt_preceive(CMT_PORT far *port);
extern int far cdecl cmt_pcount(CMT_PORT far *port);
extern void far * far cdecl cmt_malloc(unsigned size);
extern void far cdecl cmt_free(void far *block);
#ifdef __cplusplus
}
#endif

#define __CMTLIB_H__
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -