File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / inc / aitsched.h
Revision 1.8.2.2: download - view: text, annotated - select for diffs - revision graph
Wed May 30 08:51:49 2012 UTC (12 years, 1 month ago) by misho
Branches: sched2_1
Diff to: branchpoint 1.8: preferred, unified
finishing alarm feature with last changes
using optional data like alarm ident

/*************************************************************************
* (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: aitsched.h,v 1.8.2.2 2012/05/30 08:51:49 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __AITSCHED_H
#define __AITSCHED_H


#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <stdint.h>
#include <pthread.h>


/* criteria type */
#define CRITERIA_CALL	0
#define CRITERIA_ARG	1
#define CRITERIA_FD	2
#define CRITERIA_VAL	3
#define CRITERIA_TS	4


/* early declaration for root & task */
typedef struct sched_Task	sched_task_t;
typedef struct sched_RootTask	sched_root_task_t;

typedef enum {
	taskREAD = 0,
	taskWRITE,
	taskTIMER,
	taskEVENT,
	taskEVENTLO,
	taskALARM,
	taskREADY,
	taskUNUSE,
	taskMAX
} sched_task_type_t;

/* hooks */
typedef void *(*sched_hook_func_t)(void *, void *);
struct sched_HooksTask {
	struct {
		/* read(sched_task_t *task, NULL) -> int */
		sched_hook_func_t	read;
		/* write(sched_task_t *task, NULL) -> int */
		sched_hook_func_t	write;
		/* event(sched_task_t *task, NULL) -> int */
		sched_hook_func_t	event;
		/* eventlo(sched_task_t *task, NULL) -> int */
		sched_hook_func_t	eventlo;
		/* alarm(sched_task_t *task, NULL) -> int */
		sched_hook_func_t	alarm;
		/* timer(sched_task_t *task, struct timespec *ts) -> int */
		sched_hook_func_t	timer;
	}	hook_add;
	struct {
		/* cancel(sched_task_t *task, NULL) -> int */
		sched_hook_func_t	cancel;
		/* run(sched_root_task_t *root, NULL) -> int */
		sched_hook_func_t	run;
		/* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
		sched_hook_func_t	fetch;
		/* exception(sched_root_task_t *root, NULL) -> int */
		sched_hook_func_t	exception;
		/* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
		sched_hook_func_t	condition;
	}	hook_exec;
	struct {
		/* init(sched_root_task_t *root, void *data) -> int */
		sched_hook_func_t	init;
		/* fini(sched_root_task_t *root, NULL) -> int */
		sched_hook_func_t	fini;
		/* error(sched_root_task_t *root, int errno) -> int */
		sched_hook_func_t	error;
	}	hook_root;
};
typedef struct sched_HooksTask hooks_task_t;

/* task callback, like pthread callback! */
typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);

/* task lock helpers */
#define TASK_LOCK(x)		((x)->task_lock++)
#define TASK_UNLOCK(x)		((x)->task_lock ^= (x)->task_lock)
#define TASK_ISLOCKED(x)	((x)->task_lock)

/* task & queue */
struct sched_Task {
	volatile int			task_lock;
	unsigned int			task_id;
	sched_task_type_t		task_type;
#define TASK_TYPE(x)	(x)->task_type

	sched_root_task_t		*task_root;
#define TASK_ROOT(x)	(x)->task_root
	sched_task_func_t		task_func;
#define TASK_FUNC(x)	(x)->task_func

	void				*task_arg;
	union {
		unsigned long	v;
		intptr_t	fd;
		struct timespec	ts;
	}				task_val;
#define TASK_ARG(x)	(x)->task_arg
#define TASK_VAL(x)	(x)->task_val.v
#define TASK_FD(x)	(x)->task_val.fd
#define TASK_TS(x)	(x)->task_val.ts

	struct iovec			task_data;
#define TASK_DATA(x)	(x)->task_data.iov_base
#define TASK_DATLEN(x)	(x)->task_data.iov_len

	TAILQ_ENTRY(sched_Task)		task_node;
};
typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
#define TASK_DATA_SET(x, _dp, _dl)	do { \
						if ((x)) { \
							(x)->task_data.iov_base = (_dp); \
							(x)->task_data.iov_len = _dl; \
						} \
					while (0)

/* root task */
struct sched_RootTask {
	int		root_kq;
	struct timespec	root_wait;
	struct timespec	root_poll;
	intptr_t	root_cond;

	pthread_mutex_t	root_mtx[taskMAX];

	sched_queue_t	root_read;
	sched_queue_t	root_write;
	sched_queue_t	root_timer;
	sched_queue_t	root_alarm;
	sched_queue_t	root_event;
	sched_queue_t	root_ready;
	sched_queue_t	root_unuse;
	sched_queue_t	root_eventlo;
	int		root_eventlo_miss;

	hooks_task_t	root_hooks;
	struct iovec	root_data;
#define ROOT_DATA(x)	(x)->root_data.iov_base
#define ROOT_DATLEN(x)	(x)->root_data.iov_len
};
#define ROOT_QUEUE_EMPTY(x, _q)	TAILQ_EMPTY(&((x)->root_##_q))


inline int sched_GetErrno();
inline const char *sched_GetError();


/*
 * schedInit() - Init scheduler
 *
 * @data = optional data if !=NULL
 * @datlen = data len if data is set
 * return: allocated root task if ok or NULL error
 */
sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
#define schedBegin()	schedInit((void**) &schedRegisterHooks, 0)
/*
 * schedEnd() - End scheduler & free all resources
 *
 * @root = root task
 * return: -1 error or 0 ok
 */
int schedEnd(sched_root_task_t ** __restrict root);
/*
 * schedRegisterHooks() - Register IO handles and bind tasks to it
 *
 * @root = root task
 * return: -1 error or 0 ok
 */
int schedRegisterHooks(sched_root_task_t * __restrict root);
/*
 * schedPolling() - Polling timeout period if no timer task is present
 *
 * @root = root task
 * @ts = timeout polling period, if ==NULL INFINIT timeout
 * @tsold = old timeout polling if !=NULL
 * return: -1 error or 0 ok
 */
inline int schedPolling(sched_root_task_t * __restrict root, 
		struct timespec * __restrict ts, struct timespec * __restrict tsold);
/*
 * schedTermCondition() - Activate hook for scheduler condition kill
 *
 * @root = root task
 * @condValue = condition value, kill schedRun() if condValue == killState
 * return: -1 error ok 0 ok
 */
inline int schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue);
/*
 * schedCall() - Call task execution function
 *
 * @task = current task
 * return: !=NULL error or =NULL ok
 */
inline void *schedCall(sched_task_t * __restrict task);
/*
 * schedFetch() - Fetch ready task
 *
 * @root = root task
 * return: =NULL error or !=NULL ready task
 */
inline void *schedFetch(sched_root_task_t * __restrict root);
/*
 * schedRun() - Scheduler *run loop*
 *
 * @root = root task
 * @killState = kill condition variable, if !=0 stop scheduler loop
 * return: -1 error or 0 ok
 */
int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
/*
 * schedCancel() - Cancel task from scheduler
 *
 * @task = task
 * return: -1 error or 0 ok
 */
int schedCancel(sched_task_t * __restrict task);
/*
 * schedCancelby() - Cancel task from scheduler by criteria
 *
 * @root = root task
 * @type = cancel from queue type, if =taskMAX cancel same task from all queues
 * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS]
 * @param = search parameter
 * @hook = custom cleanup hook function, may be NULL
 * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
 */
int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
		u_char criteria, void *param, sched_hook_func_t hook);


/*
 * schedRead() - Add READ I/O task to scheduler queue
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @fd = fd handle
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: NULL error or !=NULL new queued task
 */
sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		int fd, void *opt_data, size_t opt_dlen);
#define schedReadSelf(x)	schedRead(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
/*
 * schedWrite() - Add WRITE I/O task to scheduler queue
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @fd = fd handle
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: NULL error or !=NULL new queued task
 */
sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		int fd, void *opt_data, size_t opt_dlen);
#define schedWriteSelf(x)	schedWrite(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
/*
 * schedAlarm() - Add ALARM task to scheduler queue
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: NULL error or !=NULL new queued task
 */
sched_task_t *schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		struct timespec ts, void *opt_data, size_t opt_dlen);
#define schedAlarmSelf(x)	schedAlarm(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
/*
 * schedTimer() - Add TIMER task to scheduler queue
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @ts = timeout argument structure
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: NULL error or !=NULL new queued task
 */
sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		struct timespec ts, void *opt_data, size_t opt_dlen);
#define schedTimerSelf(x)	schedTimer(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
/*
 * schedEvent() - Add EVENT task to scheduler queue
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @val = additional func argument
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: NULL error or !=NULL new queued task
 */
sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		unsigned long val, void *opt_data, size_t opt_dlen);
#define schedEventSelf(x)	schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
/*
 * schedEventLo() - Add EVENT_Lo task to scheduler queue
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @val = additional func argument
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: NULL error or !=NULL new queued task
 */
sched_task_t *schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		unsigned long val, void *opt_data, size_t opt_dlen);
#define schedEventLoSelf(x)	schedEventLo(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
/*
 * schedCallOnce() - Call once from scheduler
 *
 * @root = root task
 * @func = task execution function
 * @arg = 1st func argument
 * @val = additional func argument
 * @opt_data = Optional data
 * @opt_dlen = Optional data length
 * return: return value from called func
 */
sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
		unsigned long val, void *opt_data, size_t opt_dlen);
#define schedCallAgain(x)	schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))


#endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>