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