Annotation of libaitsched/src/aitsched.c, revision 1.7.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.7.2.1 ! misho 6: * $Id: aitsched.c,v 1.7 2012/05/14 12:09:13 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: {
1.7 misho 96: assert(root);
1.2 misho 97:
98: if (root->root_hooks.hook_root.fini)
99: root->root_hooks.hook_root.fini(root, NULL);
100: memset(&root->root_hooks, 0, sizeof root->root_hooks);
101:
102: root->root_hooks.hook_add.read = sched_hook_read;
103: root->root_hooks.hook_add.write = sched_hook_write;
104:
105: root->root_hooks.hook_exec.cancel = sched_hook_cancel;
106: root->root_hooks.hook_exec.fetch = sched_hook_fetch;
1.3 misho 107: root->root_hooks.hook_exec.exception = sched_hook_exception;
1.2 misho 108:
109: root->root_hooks.hook_root.init = sched_hook_init;
110: root->root_hooks.hook_root.fini = sched_hook_fini;
111: return 0;
112: }
113:
114: /*
1.1 misho 115: * schedInit() - Init scheduler
1.6 misho 116: *
1.1 misho 117: * @data = optional data if !=NULL
118: * @datlen = data len if data is set
119: * return: allocated root task if ok or NULL error
120: */
121: sched_root_task_t *
122: schedInit(void ** __restrict data, size_t datlen)
123: {
124: sched_root_task_t *root = NULL;
125: int (*func)(sched_root_task_t *);
1.5 misho 126: #ifdef HAVE_LIBPTHREAD
127: register int i;
128: #endif
1.1 misho 129:
130: root = malloc(sizeof(sched_root_task_t));
1.2 misho 131: if (!root) {
132: LOGERR;
133: } else {
1.1 misho 134: memset(root, 0, sizeof(sched_root_task_t));
1.5 misho 135:
136: /* INFINIT polling period by default */
137: sched_timespecinf(&root->root_poll);
138:
139: #ifdef HAVE_LIBPTHREAD
140: for (i = 0; i < taskMAX; i++)
141: if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
142: LOGERR;
143: while (i)
144: pthread_mutex_destroy(&root->root_mtx[--i]);
145: free(root);
146: return NULL;
147: }
148:
149: for (i = 0; i < taskMAX; i++)
150: pthread_mutex_lock(&root->root_mtx[i]);
151: #endif
152:
1.2 misho 153: TAILQ_INIT(&root->root_read);
154: TAILQ_INIT(&root->root_write);
155: TAILQ_INIT(&root->root_timer);
156: TAILQ_INIT(&root->root_event);
157: TAILQ_INIT(&root->root_eventlo);
158: TAILQ_INIT(&root->root_ready);
159: TAILQ_INIT(&root->root_unuse);
1.1 misho 160:
1.5 misho 161: #ifdef HAVE_LIBPTHREAD
162: for (i = 0; i < taskMAX; i++)
163: pthread_mutex_unlock(&root->root_mtx[i]);
164: #endif
165:
1.1 misho 166: if (data && *data) {
167: if (datlen) {
168: root->root_data.iov_base = *data;
169: root->root_data.iov_len = datlen;
1.3 misho 170: } else { /* if datlen == 0, switch to callbacks init mode */
171: /* little hack :) for correct initialization of scheduler */
1.2 misho 172: func = (int(*)(sched_root_task_t*)) data;
1.1 misho 173: func(root);
174: }
175: }
1.2 misho 176:
177: if (root->root_hooks.hook_root.init)
178: root->root_hooks.hook_root.init(root, NULL);
1.1 misho 179: }
180:
181: return root;
182: }
183:
184: /*
185: * schedEnd() - End scheduler & free all resources
1.6 misho 186: *
1.1 misho 187: * @root = root task
188: * return: -1 error or 0 ok
189: */
190: int
1.2 misho 191: schedEnd(sched_root_task_t ** __restrict root)
1.1 misho 192: {
1.7 misho 193: sched_task_t *task, *tmp;
1.5 misho 194: #ifdef HAVE_LIBPTHREAD
195: register int i;
196: #endif
1.1 misho 197:
1.2 misho 198: if (!root || !*root)
1.1 misho 199: return -1;
200:
1.7 misho 201: TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp) {
1.1 misho 202: schedCancel(task);
203: }
1.7 misho 204: TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp) {
1.1 misho 205: schedCancel(task);
206: }
1.7 misho 207: TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp) {
1.1 misho 208: schedCancel(task);
209: }
1.7 misho 210: TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp) {
1.1 misho 211: schedCancel(task);
212: }
1.7 misho 213: TAILQ_FOREACH_SAFE(task, &(*root)->root_eventlo, task_node, tmp) {
1.5 misho 214: schedCancel(task);
215: }
1.7 misho 216: TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp) {
1.1 misho 217: schedCancel(task);
218: }
219:
1.5 misho 220: #ifdef HAVE_LIBPTHREAD
221: pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
222: #endif
1.2 misho 223: while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
224: TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1 misho 225: free(task);
226: }
1.5 misho 227: #ifdef HAVE_LIBPTHREAD
228: pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
229: #endif
1.1 misho 230:
1.2 misho 231: if ((*root)->root_hooks.hook_root.fini)
232: (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1 misho 233:
1.5 misho 234: #ifdef HAVE_LIBPTHREAD
235: for (i = 0; i < taskMAX; i++)
236: pthread_mutex_destroy(&(*root)->root_mtx[i]);
237: #endif
238:
1.2 misho 239: free(*root);
240: *root = NULL;
1.1 misho 241: return 0;
242: }
243:
244: /*
245: * schedCall() - Call task execution function
1.6 misho 246: *
1.1 misho 247: * @task = current task
248: * return: !=NULL error or =NULL ok
249: */
250: inline void *
251: schedCall(sched_task_t * __restrict task)
252: {
1.4 misho 253: void *ptr = (void*) -1;
254:
1.1 misho 255: if (!task)
1.4 misho 256: return ptr;
257:
258: if (!TASK_ISLOCKED(task))
259: TASK_LOCK(task);
1.1 misho 260:
261: task->task_id++;
1.4 misho 262: ptr = task->task_func(task);
263:
264: TASK_UNLOCK(task);
265: return ptr;
1.1 misho 266: }
267:
268: /*
269: * schedFetch() - Fetch ready task
1.6 misho 270: *
1.1 misho 271: * @root = root task
272: * return: =NULL error or !=NULL ready task
273: */
274: inline void *
275: schedFetch(sched_root_task_t * __restrict root)
276: {
277: void *ptr;
278:
279: if (!root)
280: return NULL;
281:
282: if (root->root_hooks.hook_exec.fetch)
283: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
284: else
285: ptr = NULL;
286:
287: return ptr;
288: }
289:
290: /*
291: * schedCancel() - Cancel task from scheduler
1.6 misho 292: *
1.1 misho 293: * @task = task
294: * return: -1 error or 0 ok
295: */
296: int
297: schedCancel(sched_task_t * __restrict task)
298: {
299: sched_queue_t *queue;
300:
1.5 misho 301: if (!task || !TASK_ROOT(task))
1.1 misho 302: return -1;
303:
1.5 misho 304: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
305: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 306: return -1;
307:
1.5 misho 308: switch (TASK_TYPE(task)) {
1.1 misho 309: case taskREAD:
1.5 misho 310: queue = &TASK_ROOT(task)->root_read;
1.1 misho 311: break;
312: case taskWRITE:
1.5 misho 313: queue = &TASK_ROOT(task)->root_write;
1.1 misho 314: break;
315: case taskTIMER:
1.5 misho 316: queue = &TASK_ROOT(task)->root_timer;
1.1 misho 317: break;
318: case taskEVENT:
1.5 misho 319: queue = &TASK_ROOT(task)->root_event;
320: break;
321: case taskEVENTLO:
322: queue = &TASK_ROOT(task)->root_eventlo;
1.1 misho 323: break;
324: case taskREADY:
1.5 misho 325: queue = &TASK_ROOT(task)->root_ready;
1.1 misho 326: break;
327: default:
328: queue = NULL;
329: }
1.5 misho 330: if (queue) {
331: #ifdef HAVE_LIBPTHREAD
332: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
333: #endif
1.1 misho 334: TAILQ_REMOVE(queue, task, task_node);
1.5 misho 335: #ifdef HAVE_LIBPTHREAD
336: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
337: #endif
338: }
339: if (TASK_TYPE(task) != taskUNUSE)
1.4 misho 340: _sched_unuseTask(task);
1.1 misho 341:
342: return 0;
343: }
344:
345: /*
346: * schedCancelby() - Cancel task from scheduler by criteria
1.6 misho 347: *
1.1 misho 348: * @root = root task
1.5 misho 349: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.7.2.1 ! misho 350: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS]
1.1 misho 351: * @param = search parameter
352: * @hook = custom cleanup hook function, may be NULL
1.3 misho 353: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1 misho 354: */
355: int
1.5 misho 356: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
1.1 misho 357: u_char criteria, void *param, sched_hook_func_t hook)
358: {
1.7.2.1 ! misho 359: sched_task_t *task, *tmp;
1.5 misho 360: sched_queue_t *queue;
1.7.2.1 ! misho 361: register int flg = 0;
1.1 misho 362:
363: if (!root)
364: return -1;
1.5 misho 365: if (type == taskMAX) {
366: if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1 misho 367: return -2;
1.5 misho 368: if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1 misho 369: return -2;
1.5 misho 370: if (schedCancelby(root, taskTIMER, criteria, param, hook))
1.1 misho 371: return -2;
1.5 misho 372: if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1 misho 373: return -2;
1.5 misho 374: if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
1.1 misho 375: return -2;
1.5 misho 376: if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1 misho 377: return -2;
378: return 0;
379: }
1.5 misho 380: switch (type) {
381: case taskREAD:
382: queue = &root->root_read;
383: break;
384: case taskWRITE:
385: queue = &root->root_write;
386: break;
387: case taskTIMER:
388: queue = &root->root_timer;
389: break;
390: case taskEVENT:
391: queue = &root->root_event;
392: break;
393: case taskEVENTLO:
394: queue = &root->root_eventlo;
395: break;
396: case taskREADY:
397: queue = &root->root_ready;
398: break;
399: default:
400: return 0;
401: }
1.1 misho 402:
1.5 misho 403: #ifdef HAVE_LIBPTHREAD
404: pthread_mutex_lock(&root->root_mtx[type]);
405: #endif
1.7.2.1 ! misho 406: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
! 407: flg ^= flg;
! 408: switch (criteria) {
! 409: case CRITERIA_CALL:
! 410: if (TASK_FUNC(task) == (sched_task_func_t) param)
! 411: flg = 1;
1.1 misho 412: break;
1.7.2.1 ! misho 413: case CRITERIA_ARG:
! 414: if (TASK_ARG(task) == param)
! 415: flg = 1;
1.1 misho 416: break;
1.7.2.1 ! misho 417: case CRITERIA_FD:
! 418: if (TASK_FD(task) == (intptr_t) param)
! 419: flg = 1;
1.1 misho 420: break;
1.7.2.1 ! misho 421: case CRITERIA_VAL:
! 422: if (TASK_VAL(task) == (u_long) param)
! 423: flg = 1;
1.1 misho 424: break;
1.7.2.1 ! misho 425: case CRITERIA_TS:
! 426: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
! 427: flg = 1;
1.1 misho 428: break;
1.7.2.1 ! misho 429: default:
! 430: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
! 431: flg = -1;
1.1 misho 432: }
1.7.2.1 ! misho 433: if (flg < 0)
! 434: break;
! 435: /* cancel choosen task */
! 436: if (flg > 0) {
! 437: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
! 438: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
! 439: flg = -1;
! 440: break;
! 441: }
! 442: /* custom hook */
! 443: if (hook)
! 444: if (hook(task, NULL)) {
! 445: flg = -3;
! 446: break;
! 447: }
! 448:
! 449: TAILQ_REMOVE(queue, task, task_node);
! 450: if (TASK_TYPE(task) != taskUNUSE)
! 451: _sched_unuseTask(task);
1.1 misho 452:
1.7.2.1 ! misho 453: flg ^= flg; /* ok */
! 454: }
! 455: }
1.5 misho 456: #ifdef HAVE_LIBPTHREAD
1.7.2.1 ! misho 457: pthread_mutex_unlock(&root->root_mtx[type]);
1.5 misho 458: #endif
1.7.2.1 ! misho 459: return flg;
1.1 misho 460: }
461:
462: /*
463: * schedRun() - Scheduler *run loop*
1.6 misho 464: *
1.1 misho 465: * @root = root task
1.2 misho 466: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 467: * return: -1 error or 0 ok
468: */
469: int
1.7 misho 470: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1 misho 471: {
472: sched_task_t *task;
473:
474: if (!root)
475: return -1;
476:
477: if (root->root_hooks.hook_exec.run)
478: if (root->root_hooks.hook_exec.run(root, NULL))
479: return -1;
1.7 misho 480:
481: if (killState) {
482: if (root->root_hooks.hook_exec.condition)
483: /* condition scheduler loop */
484: while (root && root->root_hooks.hook_exec.fetch &&
485: root->root_hooks.hook_exec.condition &&
486: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
487: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
488: schedCall(task);
489: }
490: else
491: /* trigger scheduler loop */
492: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
493: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
494: schedCall(task);
495: }
496: } else
497: /* infinite scheduler loop */
498: while (root && root->root_hooks.hook_exec.fetch)
499: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.2 misho 500: schedCall(task);
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>