Annotation of libaitsched/src/aitsched.c, revision 1.6.2.4
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.4 ! misho 6: * $Id: aitsched.c,v 1.6.2.3 2012/04/26 08:15:22 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.6.2.4 ! 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: {
193: sched_task_t *task;
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.2 misho 201: TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
1.1 misho 202: schedCancel(task);
203: }
1.2 misho 204: TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
1.1 misho 205: schedCancel(task);
206: }
1.2 misho 207: TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
1.1 misho 208: schedCancel(task);
209: }
1.2 misho 210: TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
1.1 misho 211: schedCancel(task);
212: }
1.5 misho 213: TAILQ_FOREACH(task, &(*root)->root_eventlo, task_node) {
214: schedCancel(task);
215: }
1.2 misho 216: TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
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.1 misho 350: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
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: {
359: sched_task_t *task;
1.5 misho 360: sched_queue_t *queue;
1.1 misho 361: int flg = 0;
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.1 misho 406: TAILQ_FOREACH(task, queue, task_node)
407: if (criteria == CRITERIA_CALL) {
408: if (task->task_func == (sched_task_func_t) param) {
409: flg++;
410: break;
411: }
412: } else if (criteria == CRITERIA_ARG) {
413: if (task->task_arg == param) {
414: flg++;
415: break;
416: }
417: } else if (criteria == CRITERIA_FD) {
1.3 misho 418: if (TASK_FD(task) == (intptr_t) param) {
1.1 misho 419: flg++;
420: break;
421: }
422: } else if (criteria == CRITERIA_VAL) {
423: if (TASK_VAL(task) == (u_long) param) {
424: flg++;
425: break;
426: }
427: } else if (criteria == CRITERIA_TV) {
1.5 misho 428: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -)) {
1.1 misho 429: flg++;
430: break;
431: }
432: } else {
1.5 misho 433: #ifdef HAVE_LIBPTHREAD
434: pthread_mutex_unlock(&root->root_mtx[type]);
435: #endif
1.1 misho 436: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
437: return -1;
438: }
1.5 misho 439: #ifdef HAVE_LIBPTHREAD
440: pthread_mutex_unlock(&root->root_mtx[type]);
441: #endif
1.1 misho 442: if (!flg || !task) /* task not found */
443: return 0;
444:
1.5 misho 445: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
446: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 447: return -1;
448: if (hook)
449: if (hook(task, NULL))
450: return -3;
451:
1.5 misho 452: #ifdef HAVE_LIBPTHREAD
453: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[type]);
454: #endif
1.1 misho 455: TAILQ_REMOVE(queue, task, task_node);
1.5 misho 456: #ifdef HAVE_LIBPTHREAD
457: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[type]);
458: #endif
1.1 misho 459:
1.5 misho 460: if (TASK_TYPE(task) != taskUNUSE)
1.4 misho 461: _sched_unuseTask(task);
1.1 misho 462: return 0;
463: }
464:
465: /*
466: * schedRun() - Scheduler *run loop*
1.6 misho 467: *
1.1 misho 468: * @root = root task
1.2 misho 469: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 470: * return: -1 error or 0 ok
471: */
472: int
1.6.2.3 misho 473: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1 misho 474: {
475: sched_task_t *task;
476:
477: if (!root)
478: return -1;
479:
480: if (root->root_hooks.hook_exec.run)
481: if (root->root_hooks.hook_exec.run(root, NULL))
482: return -1;
1.6.2.3 misho 483:
484: if (killState) {
485: if (root->root_hooks.hook_exec.condition)
486: /* condition scheduler loop */
487: while (root && root->root_hooks.hook_exec.fetch &&
488: root->root_hooks.hook_exec.condition &&
489: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
1.6.2.1 misho 490: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
491: schedCall(task);
1.6.2.3 misho 492: }
493: else
494: /* trigger scheduler loop */
495: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
496: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
497: schedCall(task);
498: }
499: } else
500: /* infinite scheduler loop */
501: while (root && root->root_hooks.hook_exec.fetch)
502: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
503: schedCall(task);
1.1 misho 504:
505: return 0;
506: }
1.5 misho 507:
508: /*
509: * schedPolling() - Polling timeout period if no timer task is present
1.6 misho 510: *
1.5 misho 511: * @root = root task
512: * @ts = timeout polling period, if ==NULL INFINIT timeout
513: * @tsold = old timeout polling if !=NULL
514: * return: -1 error or 0 ok
515: */
516: inline int
517: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
518: struct timespec * __restrict tsold)
519: {
520: if (!root)
521: return -1;
522:
523: if (tsold)
524: *tsold = root->root_poll;
525:
526: if (!ts)
527: sched_timespecinf(&root->root_poll);
528: else
529: root->root_poll = *ts;
530:
531: return 0;
532: }
1.6 misho 533:
534: /*
535: * schedTermCondition() - Activate hook for scheduler condition kill
536: *
537: * @root = root task
538: * @condValue = condition value, kill schedRun() if condValue == killState
539: * return: -1 error ok 0 ok
540: */
541: inline int
542: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
543: {
544: if (!root)
545: return -1;
546:
547: root->root_cond = condValue;
548: root->root_hooks.hook_exec.condition = sched_hook_condition;
549: return 0;
550: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>