File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / inc / defs.h
Revision 1.18: download - view: text, annotated - select for diffs - revision graph
Sat Feb 25 15:55:01 2023 UTC (14 months, 3 weeks ago) by misho
Branches: MAIN
CVS tags: sched8_3, sched8_2, sched8_1, sched8_0, sched7_9, sched7_8, sched7_7, sched7_6, sched7_5, SCHED8_2, SCHED8_1, SCHED8_0, SCHED7_9, SCHED7_8, SCHED7_7, SCHED7_6, SCHED7_5, SCHED7_4, HEAD
version 7.4

/*************************************************************************
* (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: defs.h,v 1.18 2023/02/25 15:55:01 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 - 2023
	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 __DEFS_H
#define __DEFS_H


#ifndef STRSIZ
#define STRSIZ			256
#endif

#ifndef KQ_EVENTS
#define KQ_EVENTS		32
#endif

#ifndef EV_EOF
#define EV_EOF			0x8000
#endif

#ifndef SIGRTMIN
#define SIGRTMIN		65
#endif

#ifndef FIONWRITE
#define FIONWRITE		SIOCOUTQ
#endif

#ifndef TIMER_RELTIME
#define TIMER_RELTIME		0
#endif

#ifndef EV_EOF
#define EV_EOF			0x8000	/* EOF detected */
#endif
#ifndef EV_ERROR
#define EV_ERROR		0x4000	/* error, data contains errno */
#endif

#ifndef __unused
#define __unused		__attribute__((unused))
#endif

#ifndef __ELWIX
#define e_malloc	malloc
#define e_calloc	calloc
#define e_realloc	realloc
#define e_free		free
#define e_strdup	strdup
#endif

#define MAX_TASK_MISS		12

#define LOGERR	{ \
			sched_Errno = errno; \
			strlcpy(sched_Error, strerror(errno), STRSIZ); \
		}

#ifndef HAVE_LIBPTHREAD
#define pthread_mutex_lock(x)
#define pthread_mutex_unlock(x)
#endif

#define SCHED_QTRYLOCK(r, x)		pthread_mutex_trylock(&(r)->root_mtx[(x)])
#define SCHED_QLOCK(r, x)		pthread_mutex_lock(&(r)->root_mtx[(x)])
#define SCHED_QUNLOCK(r, x)		pthread_mutex_unlock(&(r)->root_mtx[(x)])

#define sched_timespecclear(tsp)	((tsp)->tv_sec = (tsp)->tv_nsec = 0)
#define sched_timespecinf(tsp)		((tsp)->tv_sec = (tsp)->tv_nsec = -1)
#define sched_timespecisinf(tsp)	((tsp)->tv_sec == -1 && (tsp)->tv_nsec == -1)
#define sched_timespecisset(tsp)	(((tsp)->tv_sec && (tsp)->tv_sec != -1) || \
						((tsp)->tv_nsec && (tsp)->tv_nsec != -1))
#define sched_timespeccmp(tsp, usp, cmp)				\
	(((tsp)->tv_sec == (usp)->tv_sec) ?				\
		((tsp)->tv_nsec cmp (usp)->tv_nsec) :			\
		((tsp)->tv_sec cmp (usp)->tv_sec))
#define sched_timespecadd(tsp, usp, vsp)				\
	do {								\
		(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;		\
		(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;	\
		if ((vsp)->tv_nsec >= 1000000000L) {			\
			(vsp)->tv_sec++;				\
			(vsp)->tv_nsec -= 1000000000L;			\
		}							\
	} while (0)
#define sched_timespecsub(tsp, usp, vsp)				\
	do {								\
		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
		if ((vsp)->tv_nsec < 0) {				\
			(vsp)->tv_sec--;				\
			(vsp)->tv_nsec += 1000000000L;			\
		}							\
	} while (0)

#define sched_timespec2val(tsp, tvp)	((tvp)->tv_sec = (tsp)->tv_sec, \
						(tvp)->tv_usec = (tsp)->tv_nsec / 1000)
#define sched_timeval2spec(tvp, tsp)	((tsp)->tv_sec = (tvp)->tv_sec, \
						(tsp)->tv_nsec = (tvp)->tv_usec * 1000)

#define sched_timevalclear(tvp)	((tvp)->tv_sec = (tvp)->tv_usec = 0)
#define sched_timevalinf(tvp)	((tvp)->tv_sec = (tvp)->tv_usec = -1)
#define sched_timevalisinf(tvp)	((tvp)->tv_sec == -1 && (tvp)->tv_usec == -1)
#define sched_timevalisset(tvp)	(((tvp)->tv_sec && (tvp)->tv_sec != -1) || \
						((tvp)->tv_usec && (tvp)->tv_usec != -1))
#define sched_timevalcmp(tvp, uvp, cmp)				\
	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
		((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
		((tvp)->tv_sec cmp (uvp)->tv_sec))
#define sched_timevaladd(tvp, uvp, vvp)				\
	do {								\
		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
		if ((vvp)->tv_usec >= 1000000) {			\
			(vvp)->tv_sec++;				\
			(vvp)->tv_usec -= 1000000;			\
		}							\
	} while (0)
#define sched_timevalsub(tvp, uvp, vvp)				\
	do {								\
		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
		if ((vvp)->tv_usec < 0) {				\
			(vvp)->tv_sec--;				\
			(vvp)->tv_usec += 1000000;			\
		}							\
	} while (0)


#ifndef HAVE_STRLCAT
size_t strlcat(char * __restrict dst, const char * __restrict src, size_t siz);
#endif
#ifndef HAVE_STRLCPY
size_t strlcpy(char * __restrict dst, const char * __restrict src, size_t siz);
#endif


extern int sched_Errno;
extern char sched_Error[];

void sched_SetErr(int, char *, ...);


static inline struct timespec *
sched_timespecmin(struct timespec * __restrict spa, struct timespec * __restrict spb)
{
	assert(spa && spb);

	if (sched_timespecisinf(spa))
		return spb;
	if (sched_timespecisinf(spb))
		return spa;

	if (spa->tv_sec == spb->tv_sec)
		return (spa->tv_nsec < spb->tv_nsec) ? spa : spb;
	else
		return (spa->tv_sec < spb->tv_sec) ? spa : spb;
}

static inline void
remove_task_from(sched_task_t * __restrict t, sched_queue_t * __restrict q)
{
	assert(t && q);

	SCHED_QLOCK(TASK_ROOT(t), TASK_TYPE(t));
	TAILQ_REMOVE(q, t, task_node);
	SCHED_QUNLOCK(TASK_ROOT(t), TASK_TYPE(t));
}

static inline void
insert_task_to(sched_task_t * __restrict t, sched_queue_t * __restrict q)
{
	assert(t && q);

	SCHED_QLOCK(TASK_ROOT(t), TASK_TYPE(t));
	TAILQ_INSERT_TAIL(q, t, task_node);
	SCHED_QUNLOCK(TASK_ROOT(t), TASK_TYPE(t));
}

static inline void
transit_task2unuse(sched_task_t * __restrict t, sched_queue_t * __restrict q)
{
	assert(t && q);

	remove_task_from(t, q);

	TASK_UNLOCK(t);
	TASK_TYPE(t) = taskUNUSE;
	insert_task_to(t, &(TASK_ROOT(t))->root_unuse);
}

static inline void
transit_task2ready(sched_task_t * __restrict t, sched_queue_t * __restrict q)
{
	remove_task_from(t, q);

	t->task_type = taskREADY;
	insert_task_to(t, &(TASK_ROOT(t))->root_ready);
}


#endif

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