--- libaitsched/inc/aitsched.h 2024/03/20 12:52:49 1.33 +++ libaitsched/inc/aitsched.h 2026/11/13 10:07:22 1.37 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsched.h,v 1.33 2024/03/20 12:52:49 misho Exp $ +* $Id: aitsched.h,v 1.37 2026/11/13 10:07:22 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2024 +Copyright 2004 - 2026 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -57,8 +57,15 @@ SUCH DAMAGE. #ifdef EVFILT_LIO #include #endif +#ifdef ATOMIC_SUPPORT +#ifndef __cplusplus + #include +#else + #include + #define _Atomic(X) std::atomic +#endif +#endif - /* criteria type */ #define CRITERIA_ANY 0 #define CRITERIA_CALL 1 @@ -70,7 +77,39 @@ SUCH DAMAGE. #define CRITERIA_DATLEN 7 #define CRITERIA_ID 8 +/* aitsched fd_set */ +typedef unsigned long sched_fd_mask; +#define SCHED_FD_SETSIZE 32768 +#define SCHED_NFDBITS (8 * (int) sizeof(sched_fd_mask)) +#define SCHED_FD_ELT(d) ((d) / SCHED_NFDBITS) +#define SCHED_FD_MASK(d) ((sched_fd_mask) (1UL << ((d) % SCHED_NFDBITS))) +typedef struct { +#ifdef __USE_XOPEN + sched_fd_mask fds_bits[SCHED_FD_SETSIZE / SCHED_NFDBITS]; +#define SCHED_FDS_BITS(set) ((set)->fds_bits) +#else + sched_fd_mask __fds_bits[SCHED_FD_SETSIZE / SCHED_NFDBITS]; +#define SCHED_FDS_BITS(set) ((set)->__fds_bits) +#endif +} sched_fd_set; + +#define SCHED_FD_ZERO(set) \ + do { \ + sched_fd_set *__arr = (set); \ + unsigned int _n = sizeof(sched_fd_set) / sizeof(sched_fd_mask); \ + while (_n > 0) \ + SCHED_FDS_BITS(__arr)[--_n] = 0; \ + } while (0) +#define SCHED_FD_SET(d, set) \ + ((void) (SCHED_FDS_BITS(set)[SCHED_FD_ELT(d)] |= SCHED_FD_MASK(d))) +#define SCHED_FD_CLR(d, set) \ + ((void) (SCHED_FDS_BITS(set)[SCHED_FD_ELT(d)] &= ~SCHED_FD_MASK(d))) +#define SCHED_FD_ISSET(d, set) \ + ((SCHED_FDS_BITS(set)[SCHED_FD_ELT(d)] & SCHED_FD_MASK(d)) != 0) +#define SCHED_FD_COPY(f, t) \ + ((void) (*(t) = *(f))) + /* early declaration for root & task */ typedef struct sched_Task sched_task_t; typedef struct sched_RootTask sched_root_task_t; @@ -146,6 +185,8 @@ struct sched_HooksTask { sched_hook_func_t exception; /* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */ sched_hook_func_t condition; + /* profile(sched_task_t *root, intptr_t *stageValue) -> int */ + sched_hook_func_t profile; } hook_exec; struct { /* init(sched_root_task_t *root, void *data) -> int */ @@ -163,9 +204,15 @@ typedef void *(*sched_task_func_t)(sched_task_t * /* c #define SCHED_TASK_DEFINE(x) void *(x)(sched_task_t*) /* task lock helpers */ -#define TASK_LOCK(x) ((x)->task_lock = 42) -#define TASK_UNLOCK(x) ((x)->task_lock ^= (x)->task_lock) -#define TASK_ISLOCKED(x) ((x)->task_lock) +#ifdef ATOMIC_SUPPORT + #define TASK_LOCK(x) atomic_store_explicit(&(x)->task_lock, 42, memory_order_release) + #define TASK_UNLOCK(x) atomic_store_explicit(&(x)->task_lock, 0, memory_order_release) + #define TASK_ISLOCKED(x) atomic_load_explicit(&(x)->task_lock, memory_order_acquire) +#else + #define TASK_LOCK(x) ((x)->task_lock = 42) + #define TASK_UNLOCK(x) ((x)->task_lock ^= (x)->task_lock) + #define TASK_ISLOCKED(x) ((x)->task_lock) +#endif /* task & queue */ struct sched_Task { @@ -173,7 +220,11 @@ struct sched_Task { #define TASK_ID(x) ((struct sched_Task*) (x)->task_id) sched_task_type_t task_type; #define TASK_TYPE(x) (x)->task_type +#ifdef ATOMIC_SUPPORT + atomic_int task_lock; +#else volatile int task_lock; +#endif sched_root_task_t *task_root; #define TASK_ROOT(x) (x)->task_root @@ -218,7 +269,7 @@ typedef TAILQ_HEAD(, sched_Task) sched_queue_t; /* root task */ struct sched_RootTask { int root_kq; - fd_set root_fds[3]; + sched_fd_set root_fds[3]; struct timespec root_wait; struct timespec root_poll; unsigned long root_miss; @@ -258,7 +309,11 @@ struct sched_RootTask { }; #define ROOT_QUEUE_EMPTY(x, _q) TAILQ_EMPTY(&((x)->root_##_q)) #define ROOT_RETURN(x) (x)->root_ret +#define ROOT_PROFILING(x, _cb) (x)->root_hooks.hook_exec.profile = (_cb) +#ifdef __cplusplus +extern "C" { +#endif int sched_GetErrno(); const char *sched_GetError(); @@ -767,5 +822,8 @@ void *sched_taskExit(sched_task_t *task, intptr_t retc */ #define taskExit(t, x) return sched_taskExit((t), (intptr_t) (x)) +#ifdef __cplusplus +} +#endif #endif