/************************************************************************/
/* Copyright 2002 MBARI													*/
/************************************************************************/
/* Summary	: Definitions for Multitasking library for Persistor CF2	*/
/* Filename : task.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: OASIS Mooring Replacement (OASIS3)						*/
/* Revision: 0.1														*/
/* Created	: 10/14/2002												*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/************************************************************************/
/* Modification History:												*/
/* 14oct2002 rah - created from OASIS io.h								*/
/************************************************************************/

/************************************************************************/
/* You must include mbariTypes.h and oasis.h before this file			*/
/************************************************************************/

#ifndef INCtaskLibh
#define INCtaskLibh		1

#include <sem.h>

#define STKSIZE			4096			/* Default task stack size			*/


/****************************************/
/* Type definitions for Task Library	*/
/****************************************/

typedef void			(*FuncPtr)();	/* Function pointer					*/

#define NULLTASK		((TaskDesc *)0)
#define NULLFUNC		((FuncPtr)0)
#define NODEtoTASK(np)	((TaskDesc *)np)

typedef enum							/************************************/
{										/* Task State type					*/
	RUNNING = 0,						/* The single running task			*/
	READY,								/* Ready, but not running			*/
	PENDING,							/* Pending on a semaphore			*/
	DELAY,								/* Waiting for delay()				*/
	ZOMBIE								/* Being killed						*/
}TaskState;								/************************************/
	
typedef struct task_node				/************************************/
{										/* Struct to locate TaskDesc's		*/
	Node		tn_link;				/* For linked lists					*/
	struct task_desc *tn_td;			/* Ptr to TaskDesc					*/
} TaskNode;								/************************************/

typedef struct task_desc				/************************************/
{										/* Task Descriptor STRUCTURE		*/
	Node		td_link;				/* For linked lists					*/
	TaskNode	td_node;				/* Node to keep on global task list */
	char		*td_name;				/* Task name						*/
	TaskState	td_state;				/* Task state						*/
	SemID		td_sem;					/* Semaphore we're waiting on		*/
	Nat32		td_delaysec;			/* Delay wakeup (seconds)			*/
	Nat16		td_delaytick;			/* Delay wakeup (RTC ticks)			*/
	FuncPtr		*td_stack;				/* Base of task stack				*/
	FuncPtr		*td_sp;					/* Task current stack pointer		*/
	Driver		*td_drvr;				/* Driver ptr						*/
	void		*td_arg;				/* Arg to entry routine				*/	
	Int16		td_serchar;				/* Last serial output char			*/
} TaskDesc;								/************************************/


/****************************************/
/* Function Declarations				*/
/****************************************/

TaskDesc *task_init(Void);
TaskDesc *task_get(Void);
TaskDesc *task_create(void *(*entryPt)(void *), void *arg, char *name);
Void	task_exit(Void);
Void	task_delay_rtcTicks(Nat32 secs, Nat16 rtcTicks);
Void	task_delay(Int32 count);
Void	task_delay_ms(Int32 count);
Void	delay_secs(Nat32 secs);
Void	task_timechange(Int32 diffSecs, Int32 diffTicks);
Void	dispatch(Void);
#ifndef NOASM
asm TaskDesc *run_dispatch(TaskDesc *td:__a0):__a0;
#endif
Void	ready(TaskDesc *td);
TaskDesc *unready(Void);
TaskDesc *task_find(char *name);
MBool	task_exists(TaskDesc *td);
Void	task_kill(TaskDesc *td);
TaskDesc *taskGetInitTask(Void);
LstHead *getTaskList(Void);
LstHead *taskGetReadyList(Void);
LstHead *taskGetDelayList(Void);
Nat32	taskGetMinDelay(Void);
Void	taskCriticalEntry(Void);
Void	taskCriticalExit(Void);

#ifdef DEBUG_FREEZE_CMD
MBool	taskSetFreezeState(MBool newState);
MBool	taskGetFreezeState(Void);
#endif

#endif	/* INCtaskLibh */
