Annotation of libaitsched/src/aitsched.c, revision 1.6.2.1
1.1 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.6.2.1 ! misho 6: * $Id: aitsched.c,v 1.6 2012/03/13 10:01:59 misho Exp $
1.1 misho 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:
1.6 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.1 misho 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: #include "global.h"
47: #include "hooks.h"
48:
49:
50: #pragma GCC visibility push(hidden)
51:
52: int sched_Errno;
53: char sched_Error[STRSIZ];
54:
55: #pragma GCC visibility pop
56:
57:
58: // sched_GetErrno() Get error code of last operation
59: inline int
60: sched_GetErrno()
61: {
62: return sched_Errno;
63: }
64:
65: // sched_GetError() Get error text of last operation
66: inline const char *
67: sched_GetError()
68: {
69: return sched_Error;
70: }
71:
72: // sched_SetErr() Set error to variables for internal use!!!
73: inline void
74: sched_SetErr(int eno, char *estr, ...)
75: {
76: va_list lst;
77:
78: sched_Errno = eno;
79: memset(sched_Error, 0, sizeof sched_Error);
80: va_start(lst, estr);
81: vsnprintf(sched_Error, sizeof sched_Error, estr, lst);
82: va_end(lst);
83: }
84:
85: /* Init and prepare scheduler functions */
86:
87: /*
1.2 misho 88: * schedRegisterHooks() - Register IO handles and bind tasks to it
1.6 misho 89: *
1.2 misho 90: * @root = root task
91: * return: -1 error or 0 ok
92: */
93: int
94: schedRegisterHooks(sched_root_task_t * __restrict root)
95: {
96: if (!root || (root->root_data.iov_base && root->root_data.iov_len))
97: return -1;
98:
99: if (root->root_hooks.hook_root.fini)
100: root->root_hooks.hook_root.fini(root, NULL);
101: memset(&root->root_hooks, 0, sizeof root->root_hooks);
102:
103: root->root_hooks.hook_add.read = sched_hook_read;
104: root->root_hooks.hook_add.write = sched_hook_write;
105:
106: root->root_hooks.hook_exec.cancel = sched_hook_cancel;
107: root->root_hooks.hook_exec.fetch = sched_hook_fetch;
1.3 misho 108: root->root_hooks.hook_exec.exception = sched_hook_exception;
1.2 misho 109:
110: root->root_hooks.hook_root.init = sched_hook_init;
111: root->root_hooks.hook_root.fini = sched_hook_fini;
112: return 0;
113: }
114:
115: /*
1.1 misho 116: * schedInit() - Init scheduler
1.6 misho 117: *
1.1 misho 118: * @data = optional data if !=NULL
119: * @datlen = data len if data is set
120: * return: allocated root task if ok or NULL error
121: */
122: sched_root_task_t *
123: schedInit(void ** __restrict data, size_t datlen)
124: {
125: sched_root_task_t *root = NULL;
126: int (*func)(sched_root_task_t *);
1.5 misho 127: #ifdef HAVE_LIBPTHREAD
128: register int i;
129: #endif
1.1 misho 130:
131: root = malloc(sizeof(sched_root_task_t));
1.2 misho 132: if (!root) {
133: LOGERR;
134: } else {
1.1 misho 135: memset(root, 0, sizeof(sched_root_task_t));
1.5 misho 136:
137: /* INFINIT polling period by default */
138: sched_timespecinf(&root->root_poll);
139:
140: #ifdef HAVE_LIBPTHREAD
141: for (i = 0; i < taskMAX; i++)
142: if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
143: LOGERR;
144: while (i)
145: pthread_mutex_destroy(&root->root_mtx[--i]);
146: free(root);
147: return NULL;
148: }
149:
150: for (i = 0; i < taskMAX; i++)
151: pthread_mutex_lock(&root->root_mtx[i]);
152: #endif
153:
1.2 misho 154: TAILQ_INIT(&root->root_read);
155: TAILQ_INIT(&root->root_write);
156: TAILQ_INIT(&root->root_timer);
157: TAILQ_INIT(&root->root_event);
158: TAILQ_INIT(&root->root_eventlo);
159: TAILQ_INIT(&root->root_ready);
160: TAILQ_INIT(&root->root_unuse);
1.1 misho 161:
1.5 misho 162: #ifdef HAVE_LIBPTHREAD
163: for (i = 0; i < taskMAX; i++)
164: pthread_mutex_unlock(&root->root_mtx[i]);
165: #endif
166:
1.1 misho 167: if (data && *data) {
168: if (datlen) {
169: root->root_data.iov_base = *data;
170: root->root_data.iov_len = datlen;
1.3 misho 171: } else { /* if datlen == 0, switch to callbacks init mode */
172: /* little hack :) for correct initialization of scheduler */
1.2 misho 173: func = (int(*)(sched_root_task_t*)) data;
1.1 misho 174: func(root);
175: }
176: }
1.2 misho 177:
178: if (root->root_hooks.hook_root.init)
179: root->root_hooks.hook_root.init(root, NULL);
1.1 misho 180: }
181:
182: return root;
183: }
184:
185: /*
186: * schedEnd() - End scheduler & free all resources
1.6 misho 187: *
1.1 misho 188: * @root = root task
189: * return: -1 error or 0 ok
190: */
191: int
1.2 misho 192: schedEnd(sched_root_task_t ** __restrict root)
1.1 misho 193: {
194: sched_task_t *task;
1.5 misho 195: #ifdef HAVE_LIBPTHREAD
196: register int i;
197: #endif
1.1 misho 198:
1.2 misho 199: if (!root || !*root)
1.1 misho 200: return -1;
201:
1.2 misho 202: TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
1.1 misho 203: schedCancel(task);
204: }
1.2 misho 205: TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
1.1 misho 206: schedCancel(task);
207: }
1.2 misho 208: TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
1.1 misho 209: schedCancel(task);
210: }
1.2 misho 211: TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
1.1 misho 212: schedCancel(task);
213: }
1.5 misho 214: TAILQ_FOREACH(task, &(*root)->root_eventlo, task_node) {
215: schedCancel(task);
216: }
1.2 misho 217: TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
1.1 misho 218: schedCancel(task);
219: }
220:
1.5 misho 221: #ifdef HAVE_LIBPTHREAD
222: pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
223: #endif
1.2 misho 224: while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
225: TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1 misho 226: free(task);
227: }
1.5 misho 228: #ifdef HAVE_LIBPTHREAD
229: pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
230: #endif
1.1 misho 231:
1.2 misho 232: if ((*root)->root_hooks.hook_root.fini)
233: (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1 misho 234:
1.5 misho 235: #ifdef HAVE_LIBPTHREAD
236: for (i = 0; i < taskMAX; i++)
237: pthread_mutex_destroy(&(*root)->root_mtx[i]);
238: #endif
239:
1.2 misho 240: free(*root);
241: *root = NULL;
1.1 misho 242: return 0;
243: }
244:
245: /*
246: * schedCall() - Call task execution function
1.6 misho 247: *
1.1 misho 248: * @task = current task
249: * return: !=NULL error or =NULL ok
250: */
251: inline void *
252: schedCall(sched_task_t * __restrict task)
253: {
1.4 misho 254: void *ptr = (void*) -1;
255:
1.1 misho 256: if (!task)
1.4 misho 257: return ptr;
258:
259: if (!TASK_ISLOCKED(task))
260: TASK_LOCK(task);
1.1 misho 261:
262: task->task_id++;
1.4 misho 263: ptr = task->task_func(task);
264:
265: TASK_UNLOCK(task);
266: return ptr;
1.1 misho 267: }
268:
269: /*
270: * schedFetch() - Fetch ready task
1.6 misho 271: *
1.1 misho 272: * @root = root task
273: * return: =NULL error or !=NULL ready task
274: */
275: inline void *
276: schedFetch(sched_root_task_t * __restrict root)
277: {
278: void *ptr;
279:
280: if (!root)
281: return NULL;
282:
283: if (root->root_hooks.hook_exec.fetch)
284: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
285: else
286: ptr = NULL;
287:
288: return ptr;
289: }
290:
291: /*
292: * schedCancel() - Cancel task from scheduler
1.6 misho 293: *
1.1 misho 294: * @task = task
295: * return: -1 error or 0 ok
296: */
297: int
298: schedCancel(sched_task_t * __restrict task)
299: {
300: sched_queue_t *queue;
301:
1.5 misho 302: if (!task || !TASK_ROOT(task))
1.1 misho 303: return -1;
304:
1.5 misho 305: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
306: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 307: return -1;
308:
1.5 misho 309: switch (TASK_TYPE(task)) {
1.1 misho 310: case taskREAD:
1.5 misho 311: queue = &TASK_ROOT(task)->root_read;
1.1 misho 312: break;
313: case taskWRITE:
1.5 misho 314: queue = &TASK_ROOT(task)->root_write;
1.1 misho 315: break;
316: case taskTIMER:
1.5 misho 317: queue = &TASK_ROOT(task)->root_timer;
1.1 misho 318: break;
319: case taskEVENT:
1.5 misho 320: queue = &TASK_ROOT(task)->root_event;
321: break;
322: case taskEVENTLO:
323: queue = &TASK_ROOT(task)->root_eventlo;
1.1 misho 324: break;
325: case taskREADY:
1.5 misho 326: queue = &TASK_ROOT(task)->root_ready;
1.1 misho 327: break;
328: default:
329: queue = NULL;
330: }
1.5 misho 331: if (queue) {
332: #ifdef HAVE_LIBPTHREAD
333: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
334: #endif
1.1 misho 335: TAILQ_REMOVE(queue, task, task_node);
1.5 misho 336: #ifdef HAVE_LIBPTHREAD
337: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
338: #endif
339: }
340: if (TASK_TYPE(task) != taskUNUSE)
1.4 misho 341: _sched_unuseTask(task);
1.1 misho 342:
343: return 0;
344: }
345:
346: /*
347: * schedCancelby() - Cancel task from scheduler by criteria
1.6 misho 348: *
1.1 misho 349: * @root = root task
1.5 misho 350: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.1 misho 351: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
352: * @param = search parameter
353: * @hook = custom cleanup hook function, may be NULL
1.3 misho 354: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1 misho 355: */
356: int
1.5 misho 357: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
1.1 misho 358: u_char criteria, void *param, sched_hook_func_t hook)
359: {
360: sched_task_t *task;
1.5 misho 361: sched_queue_t *queue;
1.1 misho 362: int flg = 0;
363:
364: if (!root)
365: return -1;
1.5 misho 366: if (type == taskMAX) {
367: if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1 misho 368: return -2;
1.5 misho 369: if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1 misho 370: return -2;
1.5 misho 371: if (schedCancelby(root, taskTIMER, criteria, param, hook))
1.1 misho 372: return -2;
1.5 misho 373: if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1 misho 374: return -2;
1.5 misho 375: if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
1.1 misho 376: return -2;
1.5 misho 377: if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1 misho 378: return -2;
379: return 0;
380: }
1.5 misho 381: switch (type) {
382: case taskREAD:
383: queue = &root->root_read;
384: break;
385: case taskWRITE:
386: queue = &root->root_write;
387: break;
388: case taskTIMER:
389: queue = &root->root_timer;
390: break;
391: case taskEVENT:
392: queue = &root->root_event;
393: break;
394: case taskEVENTLO:
395: queue = &root->root_eventlo;
396: break;
397: case taskREADY:
398: queue = &root->root_ready;
399: break;
400: default:
401: return 0;
402: }
1.1 misho 403:
1.5 misho 404: #ifdef HAVE_LIBPTHREAD
405: pthread_mutex_lock(&root->root_mtx[type]);
406: #endif
1.1 misho 407: TAILQ_FOREACH(task, queue, task_node)
408: if (criteria == CRITERIA_CALL) {
409: if (task->task_func == (sched_task_func_t) param) {
410: flg++;
411: break;
412: }
413: } else if (criteria == CRITERIA_ARG) {
414: if (task->task_arg == param) {
415: flg++;
416: break;
417: }
418: } else if (criteria == CRITERIA_FD) {
1.3 misho 419: if (TASK_FD(task) == (intptr_t) param) {
1.1 misho 420: flg++;
421: break;
422: }
423: } else if (criteria == CRITERIA_VAL) {
424: if (TASK_VAL(task) == (u_long) param) {
425: flg++;
426: break;
427: }
428: } else if (criteria == CRITERIA_TV) {
1.5 misho 429: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -)) {
1.1 misho 430: flg++;
431: break;
432: }
433: } else {
1.5 misho 434: #ifdef HAVE_LIBPTHREAD
435: pthread_mutex_unlock(&root->root_mtx[type]);
436: #endif
1.1 misho 437: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
438: return -1;
439: }
1.5 misho 440: #ifdef HAVE_LIBPTHREAD
441: pthread_mutex_unlock(&root->root_mtx[type]);
442: #endif
1.1 misho 443: if (!flg || !task) /* task not found */
444: return 0;
445:
1.5 misho 446: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
447: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 448: return -1;
449: if (hook)
450: if (hook(task, NULL))
451: return -3;
452:
1.5 misho 453: #ifdef HAVE_LIBPTHREAD
454: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[type]);
455: #endif
1.1 misho 456: TAILQ_REMOVE(queue, task, task_node);
1.5 misho 457: #ifdef HAVE_LIBPTHREAD
458: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[type]);
459: #endif
1.1 misho 460:
1.5 misho 461: if (TASK_TYPE(task) != taskUNUSE)
1.4 misho 462: _sched_unuseTask(task);
1.1 misho 463: return 0;
464: }
465:
466: /*
467: * schedRun() - Scheduler *run loop*
1.6 misho 468: *
1.1 misho 469: * @root = root task
1.2 misho 470: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 471: * return: -1 error or 0 ok
472: */
473: int
1.2 misho 474: schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState)
1.1 misho 475: {
476: sched_task_t *task;
477:
478: if (!root)
479: return -1;
480:
481: if (root->root_hooks.hook_exec.run)
482: if (root->root_hooks.hook_exec.run(root, NULL))
483: return -1;
1.2 misho 484: if (root->root_hooks.hook_exec.fetch) {
1.6 misho 485: if (killState) {
486: if (root->root_hooks.hook_exec.condition)
487: while (root->root_hooks.hook_exec.condition(root, (void*) killState)) {
488: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
489: schedCall(task);
490: }
491: else
492: while (!*killState) {
493: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
494: schedCall(task);
495: }
496: } else
1.6.2.1 ! misho 497: while (42)
! 498: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
! 499: schedCall(task);
1.2 misho 500: }
1.1 misho 501:
502: return 0;
503: }
1.5 misho 504:
505: /*
506: * schedPolling() - Polling timeout period if no timer task is present
1.6 misho 507: *
1.5 misho 508: * @root = root task
509: * @ts = timeout polling period, if ==NULL INFINIT timeout
510: * @tsold = old timeout polling if !=NULL
511: * return: -1 error or 0 ok
512: */
513: inline int
514: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
515: struct timespec * __restrict tsold)
516: {
517: if (!root)
518: return -1;
519:
520: if (tsold)
521: *tsold = root->root_poll;
522:
523: if (!ts)
524: sched_timespecinf(&root->root_poll);
525: else
526: root->root_poll = *ts;
527:
528: return 0;
529: }
1.6 misho 530:
531: /*
532: * schedTermCondition() - Activate hook for scheduler condition kill
533: *
534: * @root = root task
535: * @condValue = condition value, kill schedRun() if condValue == killState
536: * return: -1 error ok 0 ok
537: */
538: inline int
539: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
540: {
541: if (!root)
542: return -1;
543:
544: root->root_cond = condValue;
545: root->root_hooks.hook_exec.condition = sched_hook_condition;
546: return 0;
547: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>