1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: aitsched.h,v 1.15.2.1 2012/08/21 13:15:49 misho Exp $
7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #ifndef __AITSCHED_H
47: #define __AITSCHED_H
48:
49:
50: #include <sys/types.h>
51: #include <sys/queue.h>
52: #include <sys/event.h>
53: #include <sys/uio.h>
54: #include <stdint.h>
55: #include <pthread.h>
56: #include <assert.h>
57: #ifdef EVFILT_LIO
58: #include <aio.h>
59: #endif
60:
61:
62: /* criteria type */
63: #define CRITERIA_ANY 0
64: #define CRITERIA_CALL 1
65: #define CRITERIA_ARG 2
66: #define CRITERIA_FD 3
67: #define CRITERIA_VAL 4
68: #define CRITERIA_TS 5
69: #define CRITERIA_DATA 6
70: #define CRITERIA_ID 7
71:
72:
73: /* early declaration for root & task */
74: typedef struct sched_Task sched_task_t;
75: typedef struct sched_RootTask sched_root_task_t;
76:
77: typedef enum {
78: taskREAD = 0,
79: taskWRITE,
80: taskTIMER,
81: taskALARM,
82: taskNODE,
83: taskPROC,
84: taskSIGNAL,
85: taskAIO,
86: taskLIO,
87: taskUSER,
88: taskEVENT,
89: taskTASK,
90: taskSUSPEND,
91: taskREADY,
92: taskUNUSE,
93: taskTHREAD,
94: taskMAX
95: } sched_task_type_t;
96:
97: /* hooks */
98: typedef void *(*sched_hook_func_t)(void *, void *);
99: struct sched_HooksTask {
100: struct {
101: /* read(sched_task_t *task, NULL) -> int */
102: sched_hook_func_t read;
103: /* write(sched_task_t *task, NULL) -> int */
104: sched_hook_func_t write;
105: /* timer(sched_task_t *task, struct timespec *ts) -> int */
106: sched_hook_func_t timer;
107: /* alarm(sched_task_t *task, NULL) -> int */
108: sched_hook_func_t alarm;
109: /* node(sched_task_t *task, NULL) -> int */
110: sched_hook_func_t node;
111: /* proc(sched_task_t *task, NULL) -> int */
112: sched_hook_func_t proc;
113: /* signal(sched_task_t *task, NULL) -> int */
114: sched_hook_func_t signal;
115: /* aio(sched_task_t *task, NULL) -> int */
116: sched_hook_func_t aio;
117: /* lio(sched_task_t *task, NULL) -> int */
118: sched_hook_func_t lio;
119: /* user(sched_task_t *task, NULL) -> int */
120: sched_hook_func_t user;
121: /* event(sched_task_t *task, NULL) -> int */
122: sched_hook_func_t event;
123: /* task(sched_task_t *task, NULL) -> int */
124: sched_hook_func_t task;
125: /* suspend(sched_task_t *task, NULL) -> int */
126: sched_hook_func_t suspend;
127: /* thread(sched_task_t *task, NULL) -> int */
128: sched_hook_func_t thread;
129: } hook_add;
130: struct {
131: /* exit(sched_task_t *task, void *exitValue) -> int */
132: sched_hook_func_t exit;
133: /* cancel(sched_task_t *task, NULL) -> int */
134: sched_hook_func_t cancel;
135: /* resume(sched_task_t *task, NULL) -> int */
136: sched_hook_func_t resume;
137: /* run(sched_root_task_t *root, NULL) -> int */
138: sched_hook_func_t run;
139: /* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
140: sched_hook_func_t fetch;
141: /* exception(sched_root_task_t *root, NULL) -> int */
142: sched_hook_func_t exception;
143: /* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
144: sched_hook_func_t condition;
145: } hook_exec;
146: struct {
147: /* init(sched_root_task_t *root, void *data) -> int */
148: sched_hook_func_t init;
149: /* fini(sched_root_task_t *root, NULL) -> int */
150: sched_hook_func_t fini;
151: /* error(sched_root_task_t *root, int errno) -> int */
152: sched_hook_func_t error;
153: } hook_root;
154: };
155: typedef struct sched_HooksTask hooks_task_t;
156:
157: /* task callback, like pthread callback! */
158: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
159:
160: /* task lock helpers */
161: #define TASK_LOCK(x) ((x)->task_lock = 42)
162: #define TASK_UNLOCK(x) ((x)->task_lock ^= (x)->task_lock)
163: #define TASK_ISLOCKED(x) ((x)->task_lock)
164:
165: /* task & queue */
166: struct sched_Task {
167: uintptr_t task_id;
168: #define TASK_ID(x) ((struct sched_Task*) (x)->task_id)
169: sched_task_type_t task_type;
170: #define TASK_TYPE(x) (x)->task_type
171: volatile int task_lock;
172:
173: sched_root_task_t *task_root;
174: #define TASK_ROOT(x) (x)->task_root
175: sched_task_func_t task_func;
176: #define TASK_FUNC(x) (x)->task_func
177: intptr_t task_ret;
178: #define TASK_RET(x) (x)->task_ret
179: unsigned int task_flag;
180: #define TASK_FLAG(x) (x)->task_flag
181:
182: void *task_arg;
183: union {
184: unsigned long v;
185: intptr_t fd;
186: struct timespec ts;
187: } task_val;
188: #define TASK_ARG(x) (x)->task_arg
189: #define TASK_VAL(x) (x)->task_val.v
190: #define TASK_FD(x) (x)->task_val.fd
191: #define TASK_TS(x) (x)->task_val.ts
192:
193: struct iovec task_data;
194: #define TASK_DATA(x) (x)->task_data.iov_base
195: #define TASK_DATLEN(x) (x)->task_data.iov_len
196:
197: TAILQ_ENTRY(sched_Task) task_node;
198: };
199: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
200: #define TASK_DATA_SET(x, _dp, _dl) do { \
201: if ((x)) { \
202: (x)->task_data.iov_base = (_dp); \
203: (x)->task_data.iov_len = _dl; \
204: } \
205: while (0)
206:
207: /* root task */
208: struct sched_RootTask {
209: int root_kq;
210: unsigned long root_miss;
211: struct timespec root_wait;
212: struct timespec root_poll;
213: intptr_t root_cond;
214: void *root_ret;
215:
216: pthread_mutex_t root_mtx[taskMAX];
217:
218: sched_queue_t root_read;
219: sched_queue_t root_write;
220: sched_queue_t root_timer;
221: sched_queue_t root_alarm;
222: sched_queue_t root_node;
223: sched_queue_t root_proc;
224: sched_queue_t root_signal;
225: sched_queue_t root_aio;
226: sched_queue_t root_lio;
227: sched_queue_t root_user;
228: sched_queue_t root_event;
229: sched_queue_t root_task;
230: sched_queue_t root_suspend;
231: sched_queue_t root_ready;
232: sched_queue_t root_unuse;
233: sched_queue_t root_thread;
234:
235: hooks_task_t root_hooks;
236: struct iovec root_data;
237: #define ROOT_DATA(x) (x)->root_data.iov_base
238: #define ROOT_DATLEN(x) (x)->root_data.iov_len
239: };
240: #define ROOT_QUEUE_EMPTY(x, _q) TAILQ_EMPTY(&((x)->root_##_q))
241: #define ROOT_RETURN(x) (x)->root_ret
242:
243:
244: inline int sched_GetErrno();
245: inline const char *sched_GetError();
246:
247:
248: /*
249: * schedInit() - Init scheduler
250: *
251: * @data = optional data if !=NULL
252: * @datlen = data len if data is set
253: * return: allocated root task if ok or NULL error
254: */
255: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
256: #define schedBegin() schedInit((void**) &schedRegisterHooks, 0)
257: /*
258: * schedEnd() - End scheduler & free all resources
259: *
260: * @root = root task
261: * return: -1 error or 0 ok
262: */
263: int schedEnd(sched_root_task_t ** __restrict root);
264: /*
265: * schedRegisterHooks() - Register IO handles and bind tasks to it
266: *
267: * @root = root task
268: * return: -1 error or 0 ok
269: */
270: int schedRegisterHooks(sched_root_task_t * __restrict root);
271: /*
272: * sched_useTask() - Get and init new task
273: *
274: * @root = root task
275: * return: NULL error or !=NULL prepared task
276: */
277: inline sched_task_t *sched_useTask(sched_root_task_t * __restrict root);
278: /*
279: * sched_unuseTask() - Unlock and put task to unuse queue
280: *
281: * @task = task
282: * return: always is NULL
283: */
284: inline sched_task_t *sched_unuseTask(sched_task_t * __restrict task);
285: /*
286: * schedPolling() - Polling timeout period if no timer task is present
287: *
288: * @root = root task
289: * @ts = timeout polling period, if ==NULL INFINIT timeout
290: * @tsold = old timeout polling if !=NULL
291: * return: -1 error or 0 ok
292: */
293: inline int schedPolling(sched_root_task_t * __restrict root,
294: struct timespec * __restrict ts, struct timespec * __restrict tsold);
295: /*
296: * schedTermCondition() - Activate hook for scheduler condition kill
297: *
298: * @root = root task
299: * @condValue = condition value, kill schedRun() if condValue == killState
300: * return: -1 error or 0 ok
301: */
302: inline int schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue);
303: /*
304: * schedCall() - Call task execution function
305: *
306: * @task = current task
307: * return: !=NULL error or =NULL ok
308: */
309: inline void *schedCall(sched_task_t * __restrict task);
310: /*
311: * schedFetch() - Fetch ready task
312: *
313: * @root = root task
314: * return: =NULL error or !=NULL ready task
315: */
316: inline void *schedFetch(sched_root_task_t * __restrict root);
317: /*
318: * schedRun() - Scheduler *run loop*
319: *
320: * @root = root task
321: * @killState = kill condition variable, if !=0 stop scheduler loop
322: * return: -1 error or 0 ok
323: */
324: int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
325: /*
326: * schedCancel() - Cancel task from scheduler
327: *
328: * @task = task
329: * return: -1 error or 0 ok
330: */
331: int schedCancel(sched_task_t * __restrict task);
332: /*
333: * schedCancelby() - Cancel task from scheduler by criteria
334: *
335: * @root = root task
336: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
337: * @criteria = find task by criteria
338: * [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA]
339: * @param = search parameter
340: * @hook = custom cleanup hook function, may be NULL
341: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
342: */
343: int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
344: u_char criteria, void *param, sched_hook_func_t hook);
345:
346:
347: /*
348: * schedRead() - Add READ I/O task to scheduler queue
349: *
350: * @root = root task
351: * @func = task execution function
352: * @arg = 1st func argument
353: * @fd = fd handle
354: * @opt_data = Optional data
355: * @opt_dlen = Optional data length
356: * return: NULL error or !=NULL new queued task
357: */
358: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
359: int fd, void *opt_data, size_t opt_dlen);
360: #define schedReadSelf(x) schedRead(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
361: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
362: /*
363: * schedWrite() - Add WRITE I/O task to scheduler queue
364: *
365: * @root = root task
366: * @func = task execution function
367: * @arg = 1st func argument
368: * @fd = fd handle
369: * @opt_data = Optional data
370: * @opt_dlen = Optional data length
371: * return: NULL error or !=NULL new queued task
372: */
373: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
374: int fd, void *opt_data, size_t opt_dlen);
375: #define schedWriteSelf(x) schedWrite(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
376: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
377: /*
378: * schedAlarm() - Add ALARM task to scheduler queue
379: *
380: * @root = root task
381: * @func = task execution function
382: * @arg = 1st func argument
383: * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
384: * @opt_data = Optional data
385: * @opt_dlen = Optional data length
386: * return: NULL error or !=NULL new queued task
387: */
388: sched_task_t *schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
389: struct timespec ts, void *opt_data, size_t opt_dlen);
390: #define schedAlarmSelf(x) schedAlarm(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
391: TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
392: /*
393: * schedNode() - Add NODE task to scheduler queue
394: *
395: * @root = root task
396: * @func = task execution function
397: * @arg = 1st func argument
398: * @fd = fd handle
399: * @opt_data = Optional data
400: * @opt_dlen = Optional data length
401: * return: NULL error or !=NULL new queued task
402: */
403: sched_task_t *schedNode(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
404: int fd, void *opt_data, size_t opt_dlen);
405: #define schedNodeSelf(x) schedNode(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
406: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
407: /*
408: * schedProc() - Add PROC task to scheduler queue
409: *
410: * @root = root task
411: * @func = task execution function
412: * @arg = 1st func argument
413: * @pid = PID
414: * @opt_data = Optional data
415: * @opt_dlen = Optional data length
416: * return: NULL error or !=NULL new queued task
417: */
418: sched_task_t *schedProc(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
419: unsigned long pid, void *opt_data, size_t opt_dlen);
420: #define schedProcSelf(x) schedProc(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
421: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
422: /*
423: * schedSignal() - Add SIGNAL task to scheduler queue
424: *
425: * @root = root task
426: * @func = task execution function
427: * @arg = 1st func argument
428: * @sig = Signal
429: * @opt_data = Optional data
430: * @opt_dlen = Optional data length
431: * return: NULL error or !=NULL new queued task
432: */
433: sched_task_t *schedSignal(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
434: unsigned long sig, void *opt_data, size_t opt_dlen);
435: #define schedSignalSelf(x) schedSignal(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
436: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
437:
438: #ifdef EVFILT_LIO
439: /*
440: * schedAIO() - Add AIO task to scheduler queue
441: *
442: * @root = root task
443: * @func = task execution function
444: * @arg = 1st func argument
445: * @acb = AIO cb structure address
446: * @opt_data = Optional data
447: * @opt_dlen = Optional data length
448: * return: NULL error or !=NULL new queued task
449: */
450: sched_task_t *schedAIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
451: struct aiocb * __restrict acb, void *opt_data, size_t opt_dlen);
452: /*
453: * schedAIORead() - Add AIO read task to scheduler queue
454: *
455: * @root = root task
456: * @func = task execution function
457: * @arg = 1st func argument
458: * @fd = file descriptor
459: * @buffer = Buffer
460: * @buflen = Buffer length
461: * @offset = Offset from start of file, if =-1 from current position
462: * return: NULL error or !=NULL new queued task
463: */
464: inline sched_task_t *schedAIORead(sched_root_task_t * __restrict root, sched_task_func_t func,
465: void *arg, int fd, void *buffer, size_t buflen, off_t offset);
466: /*
467: * schedAIOWrite() - Add AIO write task to scheduler queue
468: *
469: * @root = root task
470: * @func = task execution function
471: * @arg = 1st func argument
472: * @fd = file descriptor
473: * @buffer = Buffer
474: * @buflen = Buffer length
475: * @offset = Offset from start of file, if =-1 from current position
476: * return: NULL error or !=NULL new queued task
477: */
478: inline sched_task_t *schedAIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func,
479: void *arg, int fd, void *buffer, size_t buflen, off_t offset);
480:
481: /*
482: * schedLIO() - Add AIO bulk tasks to scheduler queue
483: *
484: * @root = root task
485: * @func = task execution function
486: * @arg = 1st func argument
487: * @acbs = AIO cb structure addresses
488: * @opt_data = Optional data
489: * @opt_dlen = Optional data length
490: * return: NULL error or !=NULL new queued task
491: */
492: sched_task_t *schedLIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
493: struct aiocb ** __restrict acbs, void *opt_data, size_t opt_dlen);
494: /*
495: * schedLIORead() - Add list of AIO read tasks to scheduler queue
496: *
497: * @root = root task
498: * @func = task execution function
499: * @arg = 1st func argument
500: * @fd = file descriptor
501: * @bufs = Buffer's list
502: * @nbufs = Number of Buffers
503: * @offset = Offset from start of file, if =-1 from current position
504: * return: NULL error or !=NULL new queued task
505: */
506: inline sched_task_t *schedLIORead(sched_root_task_t * __restrict root, sched_task_func_t func,
507: void *arg, int fd, struct iovec *bufs, size_t nbufs, off_t offset);
508: /*
509: * schedLIOWrite() - Add list of AIO write tasks to scheduler queue
510: *
511: * @root = root task
512: * @func = task execution function
513: * @arg = 1st func argument
514: * @fd = file descriptor
515: * @bufs = Buffer's list
516: * @nbufs = Number of Buffers
517: * @offset = Offset from start of file, if =-1 from current position
518: * return: NULL error or !=NULL new queued task
519: */
520: inline sched_task_t *schedLIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func,
521: void *arg, int fd, struct iovec *bufs, size_t nbufs, off_t offset);
522: #endif /* EVFILT_LIO */
523:
524: /*
525: * schedUser() - Add trigger USER task to scheduler queue
526: *
527: * @root = root task
528: * @func = task execution function
529: * @arg = 1st func argument
530: * @id = Trigger ID
531: * @opt_data = Optional data
532: * @opt_dlen = Optional user's trigger flags
533: * return: NULL error or !=NULL new queued task
534: */
535: sched_task_t *schedUser(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
536: unsigned long id, void *opt_data, size_t opt_dlen);
537: #define schedUserSelf(x) schedUser(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
538: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
539: /*
540: * schedTrigger() - Triggering USER task
541: *
542: * @task = task
543: * return: -1 error or 0 ok
544: */
545: int schedTrigger(sched_task_t * __restrict task);
546:
547: /*
548: * schedTimer() - Add TIMER task to scheduler queue
549: *
550: * @root = root task
551: * @func = task execution function
552: * @arg = 1st func argument
553: * @ts = timeout argument structure
554: * @opt_data = Optional data
555: * @opt_dlen = Optional data length
556: * return: NULL error or !=NULL new queued task
557: */
558: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
559: struct timespec ts, void *opt_data, size_t opt_dlen);
560: #define schedTimerSelf(x) schedTimer(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
561: TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
562: /*
563: * schedEvent() - Add EVENT task to scheduler queue
564: *
565: * @root = root task
566: * @func = task execution function
567: * @arg = 1st func argument
568: * @val = additional func argument
569: * @opt_data = Optional data
570: * @opt_dlen = Optional data length
571: * return: NULL error or !=NULL new queued task
572: */
573: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
574: unsigned long val, void *opt_data, size_t opt_dlen);
575: #define schedEventSelf(x) schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
576: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
577: /*
578: * schedTask() - Add regular task to scheduler queue
579: *
580: * @root = root task
581: * @func = task execution function
582: * @arg = 1st func argument
583: * @prio = regular task priority, 0 is hi priority for regular tasks
584: * @opt_data = Optional data
585: * @opt_dlen = Optional data length
586: * return: NULL error or !=NULL new queued task
587: */
588: sched_task_t *schedTask(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
589: unsigned long prio, void *opt_data, size_t opt_dlen);
590: #define schedTaskSelf(x) schedTask(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
591: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
592: /*
593: * schedSuspend() - Add Suspended task to scheduler queue
594: *
595: * @root = root task
596: * @func = task execution function
597: * @arg = 1st func argument
598: * @id = Trigger ID
599: * @opt_data = Optional data
600: * @opt_dlen = Optional user's trigger flags
601: * return: NULL error or !=NULL new queued task
602: */
603: sched_task_t *schedSuspend(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
604: unsigned long id, void *opt_data, size_t opt_dlen);
605: #define schedSuspendSelf(x) schedUser(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
606: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
607: /*
608: * schedResumeby() - Resume suspended task
609: *
610: * @root = root task
611: * @criteria = find task by criteria
612: * [CRITERIA_ANY|CRITERIA_ID|CRITERIA_DATA]
613: * @param = search parameter (sched_task_t *task| u_long id)
614: * return: -1 error or 0 resumed ok
615: */
616: int schedResumeby(sched_root_task_t * __restrict root, unsigned char criteria, void *param);
617:
618: /*
619: * schedCallOnce() - Call once from scheduler
620: *
621: * @root = root task
622: * @func = task execution function
623: * @arg = 1st func argument
624: * @val = additional func argument
625: * @opt_data = Optional data
626: * @opt_dlen = Optional data length
627: * return: return value from called func
628: */
629: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
630: unsigned long val, void *opt_data, size_t opt_dlen);
631: #define schedCallAgain(x) schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
632: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
633:
634: /*
635: * schedThread() - Add thread task to scheduler queue
636: *
637: * @root = root task
638: * @func = task execution function
639: * @arg = 1st func argument
640: * @detach = Detach thread from scheduler, if !=0
641: * @opt_data = Optional data
642: * @opt_dlen = Optional data length
643: * return: NULL error or !=NULL new queued task
644: */
645: sched_task_t *schedThread(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
646: int detach, void *opt_data, size_t opt_dlen);
647: #define schedThreadSelf(x) schedThread(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
648: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
649: /*
650: * taskExit() - Exit routine for scheduler task
651: *
652: * @t = current executed task
653: * @x = exit value for task
654: * return: none
655: */
656: #define taskExit(t, x) do { assert((t) && TASK_ROOT(t)); \
657: if (TASK_ROOT(t)->root_hooks.hook_exec.exit) \
658: TASK_ROOT(t)->root_hooks.hook_exec.exit((t), \
659: (void*) (x)); \
660: TASK_ROOT(t)->root_ret = (void*) (x); \
661: if (TASK_TYPE(t) == taskTHREAD) { \
662: sched_unuseTask(t); \
663: pthread_exit((void*) (x)); \
664: } else \
665: return ((void*) (x)); \
666: } while (0)
667: #define taskKill(t, s) do { assert((t) && TASK_ROOT(t)); \
668: if (TASK_TYPE(t) == taskTHREAD) { \
669: pthread_t _tid = (pthread_t) TASK_VAL((t)); \
670: sched_unuseTask(t); \
671: pthread_kill(_tid, (s)); \
672: } else \
673: schedCancel((t)); \
674: } while (0)
675:
676:
677: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>