File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / tasks.c
Revision 1.10.2.8: download - view: text, annotated - select for diffs - revision graph
Thu Aug 2 12:19:29 2012 UTC (11 years, 10 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.10: preferred, unified
shuffle AIO func arguments, added custom offset

    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: tasks.c,v 1.10.2.8 2012/08/02 12:19:29 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: #include "global.h"
   47: 
   48: 
   49: #pragma GCC visibility push(hidden)
   50: 
   51: inline sched_task_t *
   52: _sched_useTask(sched_root_task_t * __restrict root)
   53: {
   54: 	sched_task_t *task, *tmp;
   55: 
   56: 	TAILQ_FOREACH_SAFE(task, &root->root_unuse, task_node, tmp) {
   57: 		if (!TASK_ISLOCKED(task)) {
   58: #ifdef HAVE_LIBPTHREAD
   59: 			pthread_mutex_lock(&root->root_mtx[taskUNUSE]);
   60: #endif
   61: 			TAILQ_REMOVE(&root->root_unuse, task, task_node);
   62: #ifdef HAVE_LIBPTHREAD
   63: 			pthread_mutex_unlock(&root->root_mtx[taskUNUSE]);
   64: #endif
   65: 			break;
   66: 		}
   67: 	}
   68: 
   69: 	if (!task) {
   70: 		task = malloc(sizeof(sched_task_t));
   71: 		if (!task) {
   72: 			LOGERR;
   73: 			return NULL;
   74: 		}
   75: 	}
   76: 
   77: 	memset(task, 0, sizeof(sched_task_t));
   78: 	task->task_id = (uintptr_t) task;
   79: 	return task;
   80: }
   81: 
   82: inline sched_task_t *
   83: _sched_unuseTask(sched_task_t * __restrict task)
   84: {
   85: 	TASK_UNLOCK(task);
   86: 	TASK_TYPE(task) = taskUNUSE;
   87: #ifdef HAVE_LIBPTHREAD
   88: 	pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[taskUNUSE]);
   89: #endif
   90: 	TAILQ_INSERT_TAIL(&TASK_ROOT(task)->root_unuse, TASK_ID(task), task_node);
   91: #ifdef HAVE_LIBPTHREAD
   92: 	pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[taskUNUSE]);
   93: #endif
   94: 	task = NULL;
   95: 
   96: 	return task;
   97: }
   98: 
   99: #pragma GCC visibility pop
  100: 
  101: 
  102: /*
  103:  * schedRead() - Add READ I/O task to scheduler queue
  104:  *
  105:  * @root = root task
  106:  * @func = task execution function
  107:  * @arg = 1st func argument
  108:  * @fd = fd handle
  109:  * @opt_data = Optional data
  110:  * @opt_dlen = Optional data length
  111:  * return: NULL error or !=NULL new queued task
  112:  */
  113: sched_task_t *
  114: schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  115: 		void *opt_data, size_t opt_dlen)
  116: {
  117: 	sched_task_t *task;
  118: 	void *ptr;
  119: 
  120: 	if (!root || !func)
  121: 		return NULL;
  122: 
  123: 	/* get new task */
  124: 	if (!(task = _sched_useTask(root)))
  125: 		return NULL;
  126: 
  127: 	task->task_func = func;
  128: 	TASK_TYPE(task) = taskREAD;
  129: 	TASK_ROOT(task) = root;
  130: 
  131: 	TASK_ARG(task) = arg;
  132: 	TASK_FD(task) = fd;
  133: 
  134: 	TASK_DATA(task) = opt_data;
  135: 	TASK_DATLEN(task) = opt_dlen;
  136: 
  137: 	if (root->root_hooks.hook_add.read)
  138: 		ptr = root->root_hooks.hook_add.read(task, NULL);
  139: 	else
  140: 		ptr = NULL;
  141: 
  142: 	if (!ptr) {
  143: #ifdef HAVE_LIBPTHREAD
  144: 		pthread_mutex_lock(&root->root_mtx[taskREAD]);
  145: #endif
  146: 		TAILQ_INSERT_TAIL(&root->root_read, TASK_ID(task), task_node);
  147: #ifdef HAVE_LIBPTHREAD
  148: 		pthread_mutex_unlock(&root->root_mtx[taskREAD]);
  149: #endif
  150: 	} else
  151: 		task = _sched_unuseTask(task);
  152: 
  153: 	return task;
  154: }
  155: 
  156: /*
  157:  * schedWrite() - Add WRITE I/O task to scheduler queue
  158:  *
  159:  * @root = root task
  160:  * @func = task execution function
  161:  * @arg = 1st func argument
  162:  * @fd = fd handle
  163:  * @opt_data = Optional data
  164:  * @opt_dlen = Optional data length
  165:  * return: NULL error or !=NULL new queued task
  166:  */
  167: sched_task_t *
  168: schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  169: 		void *opt_data, size_t opt_dlen)
  170: {
  171: 	sched_task_t *task;
  172: 	void *ptr;
  173: 
  174: 	if (!root || !func)
  175: 		return NULL;
  176: 
  177: 	/* get new task */
  178: 	if (!(task = _sched_useTask(root)))
  179: 		return NULL;
  180: 
  181: 	task->task_func = func;
  182: 	TASK_TYPE(task) = taskWRITE;
  183: 	TASK_ROOT(task) = root;
  184: 
  185: 	TASK_ARG(task) = arg;
  186: 	TASK_FD(task) = fd;
  187: 
  188: 	TASK_DATA(task) = opt_data;
  189: 	TASK_DATLEN(task) = opt_dlen;
  190: 
  191: 	if (root->root_hooks.hook_add.write)
  192: 		ptr = root->root_hooks.hook_add.write(task, NULL);
  193: 	else
  194: 		ptr = NULL;
  195: 
  196: 	if (!ptr) {
  197: #ifdef HAVE_LIBPTHREAD
  198: 		pthread_mutex_lock(&root->root_mtx[taskWRITE]);
  199: #endif
  200: 		TAILQ_INSERT_TAIL(&root->root_write, TASK_ID(task), task_node);
  201: #ifdef HAVE_LIBPTHREAD
  202: 		pthread_mutex_unlock(&root->root_mtx[taskWRITE]);
  203: #endif
  204: 	} else
  205: 		task = _sched_unuseTask(task);
  206: 
  207: 	return task;
  208: }
  209: 
  210: /*
  211:  * schedNode() - Add NODE task to scheduler queue
  212:  *
  213:  * @root = root task
  214:  * @func = task execution function
  215:  * @arg = 1st func argument
  216:  * @fd = fd handle
  217:  * @opt_data = Optional data
  218:  * @opt_dlen = Optional data length
  219:  * return: NULL error or !=NULL new queued task
  220:  */
  221: sched_task_t *
  222: schedNode(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  223: 		void *opt_data, size_t opt_dlen)
  224: {
  225: 	sched_task_t *task;
  226: 	void *ptr;
  227: 
  228: 	if (!root || !func)
  229: 		return NULL;
  230: 
  231: 	/* get new task */
  232: 	if (!(task = _sched_useTask(root)))
  233: 		return NULL;
  234: 
  235: 	task->task_func = func;
  236: 	TASK_TYPE(task) = taskNODE;
  237: 	TASK_ROOT(task) = root;
  238: 
  239: 	TASK_ARG(task) = arg;
  240: 	TASK_FD(task) = fd;
  241: 
  242: 	TASK_DATA(task) = opt_data;
  243: 	TASK_DATLEN(task) = opt_dlen;
  244: 
  245: 	if (root->root_hooks.hook_add.node)
  246: 		ptr = root->root_hooks.hook_add.node(task, NULL);
  247: 	else
  248: 		ptr = NULL;
  249: 
  250: 	if (!ptr) {
  251: #ifdef HAVE_LIBPTHREAD
  252: 		pthread_mutex_lock(&root->root_mtx[taskNODE]);
  253: #endif
  254: 		TAILQ_INSERT_TAIL(&root->root_node, TASK_ID(task), task_node);
  255: #ifdef HAVE_LIBPTHREAD
  256: 		pthread_mutex_unlock(&root->root_mtx[taskNODE]);
  257: #endif
  258: 	} else
  259: 		task = _sched_unuseTask(task);
  260: 
  261: 	return task;
  262: }
  263: 
  264: /*
  265:  * schedProc() - Add PROC task to scheduler queue
  266:  *
  267:  * @root = root task
  268:  * @func = task execution function
  269:  * @arg = 1st func argument
  270:  * @pid = PID
  271:  * @opt_data = Optional data
  272:  * @opt_dlen = Optional data length
  273:  * return: NULL error or !=NULL new queued task
  274:  */
  275: sched_task_t *
  276: schedProc(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long pid, 
  277: 		void *opt_data, size_t opt_dlen)
  278: {
  279: 	sched_task_t *task;
  280: 	void *ptr;
  281: 
  282: 	if (!root || !func)
  283: 		return NULL;
  284: 
  285: 	/* get new task */
  286: 	if (!(task = _sched_useTask(root)))
  287: 		return NULL;
  288: 
  289: 	task->task_func = func;
  290: 	TASK_TYPE(task) = taskPROC;
  291: 	TASK_ROOT(task) = root;
  292: 
  293: 	TASK_ARG(task) = arg;
  294: 	TASK_VAL(task) = pid;
  295: 
  296: 	TASK_DATA(task) = opt_data;
  297: 	TASK_DATLEN(task) = opt_dlen;
  298: 
  299: 	if (root->root_hooks.hook_add.proc)
  300: 		ptr = root->root_hooks.hook_add.proc(task, NULL);
  301: 	else
  302: 		ptr = NULL;
  303: 
  304: 	if (!ptr) {
  305: #ifdef HAVE_LIBPTHREAD
  306: 		pthread_mutex_lock(&root->root_mtx[taskPROC]);
  307: #endif
  308: 		TAILQ_INSERT_TAIL(&root->root_proc, TASK_ID(task), task_node);
  309: #ifdef HAVE_LIBPTHREAD
  310: 		pthread_mutex_unlock(&root->root_mtx[taskPROC]);
  311: #endif
  312: 	} else
  313: 		task = _sched_unuseTask(task);
  314: 
  315: 	return task;
  316: }
  317: 
  318: /*
  319:  * schedUser() - Add trigger USER task to scheduler queue
  320:  *
  321:  * @root = root task
  322:  * @func = task execution function
  323:  * @arg = 1st func argument
  324:  * @id = Trigger ID
  325:  * @opt_data = Optional data
  326:  * @opt_dlen = Optional user's trigger flags
  327:  * return: NULL error or !=NULL new queued task
  328:  */
  329: sched_task_t *
  330: schedUser(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long id, 
  331: 		void *opt_data, size_t opt_dlen)
  332: {
  333: #ifndef EVFILT_USER
  334: 	sched_SetErr(ENOTSUP, "Not supported kevent() filter");
  335: 	return NULL;
  336: #else
  337: 	sched_task_t *task;
  338: 	void *ptr;
  339: 
  340: 	if (!root || !func)
  341: 		return NULL;
  342: 
  343: 	/* get new task */
  344: 	if (!(task = _sched_useTask(root)))
  345: 		return NULL;
  346: 
  347: 	task->task_func = func;
  348: 	TASK_TYPE(task) = taskUSER;
  349: 	TASK_ROOT(task) = root;
  350: 
  351: 	TASK_ARG(task) = arg;
  352: 	TASK_VAL(task) = id;
  353: 
  354: 	TASK_DATA(task) = opt_data;
  355: 	TASK_DATLEN(task) = opt_dlen;
  356: 
  357: 	if (root->root_hooks.hook_add.user)
  358: 		ptr = root->root_hooks.hook_add.user(task, NULL);
  359: 	else
  360: 		ptr = NULL;
  361: 
  362: 	if (!ptr) {
  363: #ifdef HAVE_LIBPTHREAD
  364: 		pthread_mutex_lock(&root->root_mtx[taskUSER]);
  365: #endif
  366: 		TAILQ_INSERT_TAIL(&root->root_user, TASK_ID(task), task_node);
  367: #ifdef HAVE_LIBPTHREAD
  368: 		pthread_mutex_unlock(&root->root_mtx[taskUSER]);
  369: #endif
  370: 	} else
  371: 		task = _sched_unuseTask(task);
  372: 
  373: 	return task;
  374: #endif
  375: }
  376: 
  377: /*
  378:  * schedSignal() - Add SIGNAL task to scheduler queue
  379:  *
  380:  * @root = root task
  381:  * @func = task execution function
  382:  * @arg = 1st func argument
  383:  * @sig = Signal
  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 *
  389: schedSignal(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long sig, 
  390: 		void *opt_data, size_t opt_dlen)
  391: {
  392: 	sched_task_t *task;
  393: 	void *ptr;
  394: 
  395: 	if (!root || !func)
  396: 		return NULL;
  397: 
  398: 	/* get new task */
  399: 	if (!(task = _sched_useTask(root)))
  400: 		return NULL;
  401: 
  402: 	task->task_func = func;
  403: 	TASK_TYPE(task) = taskSIGNAL;
  404: 	TASK_ROOT(task) = root;
  405: 
  406: 	TASK_ARG(task) = arg;
  407: 	TASK_VAL(task) = sig;
  408: 
  409: 	TASK_DATA(task) = opt_data;
  410: 	TASK_DATLEN(task) = opt_dlen;
  411: 
  412: 	if (root->root_hooks.hook_add.signal)
  413: 		ptr = root->root_hooks.hook_add.signal(task, NULL);
  414: 	else
  415: 		ptr = NULL;
  416: 
  417: 	if (!ptr) {
  418: #ifdef HAVE_LIBPTHREAD
  419: 		pthread_mutex_lock(&root->root_mtx[taskSIGNAL]);
  420: #endif
  421: 		TAILQ_INSERT_TAIL(&root->root_signal, TASK_ID(task), task_node);
  422: #ifdef HAVE_LIBPTHREAD
  423: 		pthread_mutex_unlock(&root->root_mtx[taskSIGNAL]);
  424: #endif
  425: 	} else
  426: 		task = _sched_unuseTask(task);
  427: 
  428: 	return task;
  429: }
  430: 
  431: /*
  432:  * schedAlarm() - Add ALARM task to scheduler queue
  433:  *
  434:  * @root = root task
  435:  * @func = task execution function
  436:  * @arg = 1st func argument
  437:  * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
  438:  * @opt_data = Optional data
  439:  * @opt_dlen = Optional data length
  440:  * return: NULL error or !=NULL new queued task
  441:  */
  442: sched_task_t *
  443: schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, struct timespec ts, 
  444: 		void *opt_data, size_t opt_dlen)
  445: {
  446: 	sched_task_t *task;
  447: 	void *ptr;
  448: 
  449: 	if (!root || !func)
  450: 		return NULL;
  451: 
  452: 	/* get new task */
  453: 	if (!(task = _sched_useTask(root)))
  454: 		return NULL;
  455: 
  456: 	task->task_func = func;
  457: 	TASK_TYPE(task) = taskALARM;
  458: 	TASK_ROOT(task) = root;
  459: 
  460: 	TASK_ARG(task) = arg;
  461: 	TASK_TS(task) = ts;
  462: 
  463: 	TASK_DATA(task) = opt_data;
  464: 	TASK_DATLEN(task) = opt_dlen;
  465: 
  466: 	if (root->root_hooks.hook_add.alarm)
  467: 		ptr = root->root_hooks.hook_add.alarm(task, NULL);
  468: 	else
  469: 		ptr = NULL;
  470: 
  471: 	if (!ptr) {
  472: #ifdef HAVE_LIBPTHREAD
  473: 		pthread_mutex_lock(&root->root_mtx[taskALARM]);
  474: #endif
  475: 		TAILQ_INSERT_TAIL(&root->root_alarm, TASK_ID(task), task_node);
  476: #ifdef HAVE_LIBPTHREAD
  477: 		pthread_mutex_unlock(&root->root_mtx[taskALARM]);
  478: #endif
  479: 	} else
  480: 		task = _sched_unuseTask(task);
  481: 
  482: 	return task;
  483: }
  484: 
  485: #ifdef EVFILT_AIO
  486: /*
  487:  * schedAIO() - Add AIO task to scheduler queue
  488:  *
  489:  * @root = root task
  490:  * @func = task execution function
  491:  * @arg = 1st func argument
  492:  * @acb = AIO cb structure address
  493:  * @opt_data = Optional data
  494:  * @opt_dlen = Optional data length
  495:  * return: NULL error or !=NULL new queued task
  496:  */
  497: sched_task_t *
  498: schedAIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  499: 		struct aiocb * __restrict acb, void *opt_data, size_t opt_dlen)
  500: {
  501: 	sched_task_t *task;
  502: 	void *ptr;
  503: 
  504: 	if (!root || !func || !acb || !opt_dlen)
  505: 		return NULL;
  506: 
  507: 	/* get new task */
  508: 	if (!(task = _sched_useTask(root)))
  509: 		return NULL;
  510: 
  511: 	task->task_func = func;
  512: 	TASK_TYPE(task) = taskAIO;
  513: 	TASK_ROOT(task) = root;
  514: 
  515: 	TASK_ARG(task) = arg;
  516: 	TASK_VAL(task) = (u_long) acb;
  517: 
  518: 	TASK_DATA(task) = opt_data;
  519: 	TASK_DATLEN(task) = opt_dlen;
  520: 
  521: 	if (root->root_hooks.hook_add.aio)
  522: 		ptr = root->root_hooks.hook_add.aio(task, NULL);
  523: 	else
  524: 		ptr = NULL;
  525: 
  526: 	if (!ptr) {
  527: #ifdef HAVE_LIBPTHREAD
  528: 		pthread_mutex_lock(&root->root_mtx[taskAIO]);
  529: #endif
  530: 		TAILQ_INSERT_TAIL(&root->root_aio, TASK_ID(task), task_node);
  531: #ifdef HAVE_LIBPTHREAD
  532: 		pthread_mutex_unlock(&root->root_mtx[taskAIO]);
  533: #endif
  534: 	} else
  535: 		task = _sched_unuseTask(task);
  536: 
  537: 	return task;
  538: }
  539: 
  540: /*
  541:  * schedAIORead() - Add AIO read task to scheduler queue
  542:  *
  543:  * @root = root task
  544:  * @func = task execution function
  545:  * @arg = 1st func argument
  546:  * @fd = file descriptor
  547:  * @buffer = Buffer
  548:  * @buflen = Buffer length
  549:  * @offset = Offset from start of file, if =-1 from current position
  550:  * return: NULL error or !=NULL new queued task
  551:  */
  552: inline sched_task_t *
  553: schedAIORead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  554: 		void *buffer, size_t buflen, off_t offset)
  555: {
  556: 	struct aiocb *acb;
  557: 	off_t off;
  558: 
  559: 	if (!root || !func || !buffer || !buflen)
  560: 		return NULL;
  561: 
  562: 	if (offset == (off_t) -1) {
  563: 		off = lseek(fd, 0, SEEK_CUR);
  564: 		if (off == -1) {
  565: 			LOGERR;
  566: 			return NULL;
  567: 		}
  568: 	} else
  569: 		off = offset;
  570: 
  571: 	if (!(acb = malloc(sizeof(struct aiocb)))) {
  572: 		LOGERR;
  573: 		return NULL;
  574: 	} else
  575: 		memset(acb, 0, sizeof(struct aiocb));
  576: 
  577: 	acb->aio_fildes = fd;
  578: 	acb->aio_nbytes = buflen;
  579: 	acb->aio_buf = buffer;
  580: 	acb->aio_offset = off;
  581: 	acb->aio_sigevent.sigev_notify = SIGEV_KEVENT;
  582: 	acb->aio_sigevent.sigev_notify_kqueue = root->root_kq;
  583: 	acb->aio_sigevent.sigev_value.sival_ptr = acb;
  584: 
  585: 	if (aio_read(acb)) {
  586: 		LOGERR;
  587: 		free(acb);
  588: 		return NULL;
  589: 	}
  590: 
  591: 	return schedAIO(root, func, arg, acb, buffer, buflen);
  592: }
  593: 
  594: /*
  595:  * schedAIOWrite() - Add AIO write task to scheduler queue
  596:  *
  597:  * @root = root task
  598:  * @func = task execution function
  599:  * @arg = 1st func argument
  600:  * @fd = file descriptor
  601:  * @buffer = Buffer
  602:  * @buflen = Buffer length
  603:  * @offset = Offset from start of file, if =-1 from current position
  604:  * return: NULL error or !=NULL new queued task
  605:  */
  606: inline sched_task_t *
  607: schedAIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  608: 		void *buffer, size_t buflen, off_t offset)
  609: {
  610: 	struct aiocb *acb;
  611: 	off_t off;
  612: 
  613: 	if (!root || !func || !buffer || !buflen)
  614: 		return NULL;
  615: 
  616: 	if (offset == (off_t) -1) {
  617: 		off = lseek(fd, 0, SEEK_CUR);
  618: 		if (off == -1) {
  619: 			LOGERR;
  620: 			return NULL;
  621: 		}
  622: 	} else
  623: 		off = offset;
  624: 
  625: 	if (!(acb = malloc(sizeof(struct aiocb)))) {
  626: 		LOGERR;
  627: 		return NULL;
  628: 	} else
  629: 		memset(acb, 0, sizeof(struct aiocb));
  630: 
  631: 	acb->aio_fildes = fd;
  632: 	acb->aio_nbytes = buflen;
  633: 	acb->aio_buf = buffer;
  634: 	acb->aio_offset = off;
  635: 	acb->aio_sigevent.sigev_notify = SIGEV_KEVENT;
  636: 	acb->aio_sigevent.sigev_notify_kqueue = root->root_kq;
  637: 	acb->aio_sigevent.sigev_value.sival_ptr = acb;
  638: 
  639: 	if (aio_write(acb)) {
  640: 		LOGERR;
  641: 		free(acb);
  642: 		return NULL;
  643: 	}
  644: 
  645: 	return schedAIO(root, func, arg, acb, buffer, buflen);
  646: }
  647: 
  648: #ifdef EVFILT_LIO
  649: /*
  650:  * schedLIORead() - Add list of AIO read tasks to scheduler queue
  651:  *
  652:  * @root = root task
  653:  * @func = task execution function
  654:  * @arg = 1st func argument
  655:  * @fd = file descriptor
  656:  * @bufs = Buffer's list
  657:  * @nbufs = Number of Buffers
  658:  * @offset = Offset from start of file, if =-1 from current position
  659:  * return: NULL error or !=NULL new queued task
  660:  */
  661: sched_task_t *
  662: schedLIORead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  663: 		struct iovec *bufs, size_t nbufs, off_t offset)
  664: {
  665: 	struct sigevent sig;
  666: 	struct aiocb **acb;
  667: 	off_t off;
  668: 	register int i;
  669: 
  670: 	if (!root || !func || !bufs || !nbufs)
  671: 		return NULL;
  672: 
  673: 	if (offset == (off_t) -1) {
  674: 		off = lseek(fd, 0, SEEK_CUR);
  675: 		if (off == -1) {
  676: 			LOGERR;
  677: 			return NULL;
  678: 		}
  679: 	} else
  680: 		off = offset;
  681: 
  682: 	if (!(acb = calloc(sizeof(void*), nbufs))) {
  683: 		LOGERR;
  684: 		return NULL;
  685: 	} else
  686: 		memset(acb, 0, sizeof(void*) * nbufs);
  687: 	for (i = 0; i < nbufs; off += bufs[i++].iov_len) {
  688: 		acb[i] = malloc(sizeof(struct aiocb));
  689: 		if (!acb[i]) {
  690: 			LOGERR;
  691: 			for (i = 0; i < nbufs; i++)
  692: 				if (acb[i])
  693: 					free(acb[i]);
  694: 				free(acb);
  695: 			return NULL;
  696: 		} else
  697: 			memset(acb[i], 0, sizeof(struct aiocb));
  698: 		acb[i]->aio_fildes = fd;
  699: 		acb[i]->aio_nbytes = bufs[i].iov_len;
  700: 		acb[i]->aio_buf = bufs[i].iov_base;
  701: 		acb[i]->aio_offset = off;
  702: 		acb[i]->aio_lio_opcode = LIO_READ;
  703: 	}
  704: 	memset(&sig, 0, sizeof sig);
  705: 	sig.sigev_notify = SIGEV_KEVENT;
  706: 	sig.sigev_notify_kqueue = root->root_kq;
  707: 	sig.sigev_value.sival_ptr = acb;
  708: 
  709: 	if (lio_listio(LIO_NOWAIT, acb, nbufs, &sig)) {
  710: 		LOGERR;
  711: 		return NULL;
  712: 	}
  713: 
  714: 	return schedAIO(root, func, arg, (void*) acb, bufs, nbufs);
  715: }
  716: 
  717: /*
  718:  * schedLIOWrite() - Add list of AIO write tasks to scheduler queue
  719:  *
  720:  * @root = root task
  721:  * @func = task execution function
  722:  * @arg = 1st func argument
  723:  * @fd = file descriptor
  724:  * @bufs = Buffer's list
  725:  * @nbufs = Number of Buffers
  726:  * @offset = Offset from start of file, if =-1 from current position
  727:  * return: NULL error or !=NULL new queued task
  728:  */
  729: inline sched_task_t *
  730: schedLIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, 
  731: 		struct iovec *bufs, size_t nbufs, off_t offset)
  732: {
  733: 	struct sigevent sig;
  734: 	struct aiocb **acb;
  735: 	off_t off;
  736: 	register int i;
  737: 
  738: 	if (!root || !func || !bufs || !nbufs)
  739: 		return NULL;
  740: 
  741: 	if (offset == (off_t) -1) {
  742: 		off = lseek(fd, 0, SEEK_CUR);
  743: 		if (off == -1) {
  744: 			LOGERR;
  745: 			return NULL;
  746: 		}
  747: 	} else
  748: 		off = offset;
  749: 
  750: 	if (!(acb = calloc(sizeof(void*), nbufs))) {
  751: 		LOGERR;
  752: 		return NULL;
  753: 	} else
  754: 		memset(acb, 0, sizeof(void*) * nbufs);
  755: 	for (i = 0; i < nbufs; off += bufs[i++].iov_len) {
  756: 		acb[i] = malloc(sizeof(struct aiocb));
  757: 		if (!acb[i]) {
  758: 			LOGERR;
  759: 			for (i = 0; i < nbufs; i++)
  760: 				if (acb[i])
  761: 					free(acb[i]);
  762: 				free(acb);
  763: 			return NULL;
  764: 		} else
  765: 			memset(acb[i], 0, sizeof(struct aiocb));
  766: 		acb[i]->aio_fildes = fd;
  767: 		acb[i]->aio_nbytes = bufs[i].iov_len;
  768: 		acb[i]->aio_buf = bufs[i].iov_base;
  769: 		acb[i]->aio_offset = off;
  770: 		acb[i]->aio_lio_opcode = LIO_WRITE;
  771: 	}
  772: 	memset(&sig, 0, sizeof sig);
  773: 	sig.sigev_notify = SIGEV_KEVENT;
  774: 	sig.sigev_notify_kqueue = root->root_kq;
  775: 	sig.sigev_value.sival_ptr = acb;
  776: 
  777: 	if (lio_listio(LIO_NOWAIT, acb, nbufs, &sig)) {
  778: 		LOGERR;
  779: 		return NULL;
  780: 	}
  781: 
  782: 	return schedAIO(root, func, arg, (void*) acb, bufs, nbufs);
  783: }
  784: #endif	/* EVFILT_LIO */
  785: #endif	/* EVFILT_AIO */
  786: 
  787: /*
  788:  * schedTimer() - Add TIMER task to scheduler queue
  789:  *
  790:  * @root = root task
  791:  * @func = task execution function
  792:  * @arg = 1st func argument
  793:  * @ts = timeout argument structure
  794:  * @opt_data = Optional data
  795:  * @opt_dlen = Optional data length
  796:  * return: NULL error or !=NULL new queued task
  797:  */
  798: sched_task_t *
  799: schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, struct timespec ts, 
  800: 		void *opt_data, size_t opt_dlen)
  801: {
  802: 	sched_task_t *task, *tmp, *t = NULL;
  803: 	void *ptr;
  804: 	struct timespec now;
  805: 
  806: 	if (!root || !func)
  807: 		return NULL;
  808: 
  809: 	/* get new task */
  810: 	if (!(task = _sched_useTask(root)))
  811: 		return NULL;
  812: 
  813: 	task->task_func = func;
  814: 	TASK_TYPE(task) = taskTIMER;
  815: 	TASK_ROOT(task) = root;
  816: 
  817: 	TASK_ARG(task) = arg;
  818: 
  819: 	TASK_DATA(task) = opt_data;
  820: 	TASK_DATLEN(task) = opt_dlen;
  821: 
  822: 	/* calculate timeval structure */
  823: 	clock_gettime(CLOCK_MONOTONIC, &now);
  824: 	now.tv_sec += ts.tv_sec;
  825: 	now.tv_nsec += ts.tv_nsec;
  826: 	if (now.tv_nsec >= 1000000000L) {
  827: 		now.tv_sec++;
  828: 		now.tv_nsec -= 1000000000L;
  829: 	} else if (now.tv_nsec < 0) {
  830: 		now.tv_sec--;
  831: 		now.tv_nsec += 1000000000L;
  832: 	}
  833: 	TASK_TS(task) = now;
  834: 
  835: 	if (root->root_hooks.hook_add.timer)
  836: 		ptr = root->root_hooks.hook_add.timer(task, NULL);
  837: 	else
  838: 		ptr = NULL;
  839: 
  840: 	if (!ptr) {
  841: #ifdef HAVE_LIBPTHREAD
  842: 		pthread_mutex_lock(&root->root_mtx[taskTIMER]);
  843: #endif
  844: #ifdef TIMER_WITHOUT_SORT
  845: 		TAILQ_INSERT_TAIL(&root->root_timer, TASK_ID(task), task_node);
  846: #else
  847: 		TAILQ_FOREACH_SAFE(t, &root->root_timer, task_node, tmp)
  848: 			if (sched_timespeccmp(&TASK_TS(task), &TASK_TS(t), -) < 1)
  849: 				break;
  850: 		if (!t)
  851: 			TAILQ_INSERT_TAIL(&root->root_timer, TASK_ID(task), task_node);
  852: 		else
  853: 			TAILQ_INSERT_BEFORE(t, TASK_ID(task), task_node);
  854: #endif
  855: #ifdef HAVE_LIBPTHREAD
  856: 		pthread_mutex_unlock(&root->root_mtx[taskTIMER]);
  857: #endif
  858: 	} else
  859: 		task = _sched_unuseTask(task);
  860: 
  861: 	return task;
  862: }
  863: 
  864: /*
  865:  * schedEvent() - Add EVENT task to scheduler queue
  866:  *
  867:  * @root = root task
  868:  * @func = task execution function
  869:  * @arg = 1st func argument
  870:  * @val = additional func argument
  871:  * @opt_data = Optional data
  872:  * @opt_dlen = Optional data length
  873:  * return: NULL error or !=NULL new queued task
  874:  */
  875: sched_task_t *
  876: schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long val, 
  877: 		void *opt_data, size_t opt_dlen)
  878: {
  879: 	sched_task_t *task;
  880: 	void *ptr;
  881: 
  882: 	if (!root || !func)
  883: 		return NULL;
  884: 
  885: 	/* get new task */
  886: 	if (!(task = _sched_useTask(root)))
  887: 		return NULL;
  888: 
  889: 	task->task_func = func;
  890: 	TASK_TYPE(task) = taskEVENT;
  891: 	TASK_ROOT(task) = root;
  892: 
  893: 	TASK_ARG(task) = arg;
  894: 	TASK_VAL(task) = val;
  895: 
  896: 	TASK_DATA(task) = opt_data;
  897: 	TASK_DATLEN(task) = opt_dlen;
  898: 
  899: 	if (root->root_hooks.hook_add.event)
  900: 		ptr = root->root_hooks.hook_add.event(task, NULL);
  901: 	else
  902: 		ptr = NULL;
  903: 
  904: 	if (!ptr) {
  905: #ifdef HAVE_LIBPTHREAD
  906: 		pthread_mutex_lock(&root->root_mtx[taskEVENT]);
  907: #endif
  908: 		TAILQ_INSERT_TAIL(&root->root_event, TASK_ID(task), task_node);
  909: #ifdef HAVE_LIBPTHREAD
  910: 		pthread_mutex_unlock(&root->root_mtx[taskEVENT]);
  911: #endif
  912: 	} else
  913: 		task = _sched_unuseTask(task);
  914: 
  915: 	return task;
  916: }
  917: 
  918: 
  919: /*
  920:  * schedEventLo() - Add EVENT_Lo task to scheduler queue
  921:  *
  922:  * @root = root task
  923:  * @func = task execution function
  924:  * @arg = 1st func argument
  925:  * @val = additional func argument
  926:  * @opt_data = Optional data
  927:  * @opt_dlen = Optional data length
  928:  * return: NULL error or !=NULL new queued task
  929:  */
  930: sched_task_t *
  931: schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long val, 
  932: 		void *opt_data, size_t opt_dlen)
  933: {
  934: 	sched_task_t *task;
  935: 	void *ptr;
  936: 
  937: 	if (!root || !func)
  938: 		return NULL;
  939: 
  940: 	/* get new task */
  941: 	if (!(task = _sched_useTask(root)))
  942: 		return NULL;
  943: 
  944: 	task->task_func = func;
  945: 	TASK_TYPE(task) = taskEVENT;
  946: 	TASK_ROOT(task) = root;
  947: 
  948: 	TASK_ARG(task) = arg;
  949: 	TASK_VAL(task) = val;
  950: 
  951: 	TASK_DATA(task) = opt_data;
  952: 	TASK_DATLEN(task) = opt_dlen;
  953: 
  954: 	if (root->root_hooks.hook_add.eventlo)
  955: 		ptr = root->root_hooks.hook_add.eventlo(task, NULL);
  956: 	else
  957: 		ptr = NULL;
  958: 
  959: 	if (!ptr) {
  960: #ifdef HAVE_LIBPTHREAD
  961: 		pthread_mutex_lock(&root->root_mtx[taskEVENTLO]);
  962: #endif
  963: 		TAILQ_INSERT_TAIL(&root->root_eventlo, TASK_ID(task), task_node);
  964: #ifdef HAVE_LIBPTHREAD
  965: 		pthread_mutex_unlock(&root->root_mtx[taskEVENTLO]);
  966: #endif
  967: 	} else
  968: 		task = _sched_unuseTask(task);
  969: 
  970: 	return task;
  971: }
  972: 
  973: /*
  974:  * schedSuspend() - Add Suspended task to scheduler queue
  975:  *
  976:  * @root = root task
  977:  * @func = task execution function
  978:  * @arg = 1st func argument
  979:  * @id = Trigger ID
  980:  * @opt_data = Optional data
  981:  * @opt_dlen = Optional data length
  982:  * return: NULL error or !=NULL new queued task
  983:  */
  984: sched_task_t *
  985: schedSuspend(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long id, 
  986: 		void *opt_data, size_t opt_dlen)
  987: {
  988: 	sched_task_t *task;
  989: 	void *ptr;
  990: 
  991: 	if (!root || !func)
  992: 		return NULL;
  993: 
  994: 	/* get new task */
  995: 	if (!(task = _sched_useTask(root)))
  996: 		return NULL;
  997: 
  998: 	task->task_func = func;
  999: 	TASK_TYPE(task) = taskSUSPEND;
 1000: 	TASK_ROOT(task) = root;
 1001: 
 1002: 	TASK_ARG(task) = arg;
 1003: 	TASK_VAL(task) = id;
 1004: 
 1005: 	TASK_DATA(task) = opt_data;
 1006: 	TASK_DATLEN(task) = opt_dlen;
 1007: 
 1008: 	if (root->root_hooks.hook_add.suspend)
 1009: 		ptr = root->root_hooks.hook_add.suspend(task, NULL);
 1010: 	else
 1011: 		ptr = NULL;
 1012: 
 1013: 	if (!ptr) {
 1014: #ifdef HAVE_LIBPTHREAD
 1015: 		pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
 1016: #endif
 1017: 		TAILQ_INSERT_TAIL(&root->root_suspend, TASK_ID(task), task_node);
 1018: #ifdef HAVE_LIBPTHREAD
 1019: 		pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
 1020: #endif
 1021: 	} else
 1022: 		task = _sched_unuseTask(task);
 1023: 
 1024: 	return task;
 1025: }
 1026: 
 1027: /*
 1028:  * schedCallOnce() - Call once from scheduler
 1029:  *
 1030:  * @root = root task
 1031:  * @func = task execution function
 1032:  * @arg = 1st func argument
 1033:  * @val = additional func argument
 1034:  * @opt_data = Optional data
 1035:  * @opt_dlen = Optional data length
 1036:  * return: return value from called func
 1037:  */
 1038: sched_task_t *
 1039: schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long val, 
 1040: 		void *opt_data, size_t opt_dlen)
 1041: {
 1042: 	sched_task_t *task;
 1043: 	void *ret;
 1044: 
 1045: 	if (!root || !func)
 1046: 		return NULL;
 1047: 
 1048: 	/* get new task */
 1049: 	if (!(task = _sched_useTask(root)))
 1050: 		return NULL;
 1051: 
 1052: 	task->task_func = func;
 1053: 	TASK_TYPE(task) = taskEVENT;
 1054: 	TASK_ROOT(task) = root;
 1055: 
 1056: 	TASK_ARG(task) = arg;
 1057: 	TASK_VAL(task) = val;
 1058: 
 1059: 	TASK_DATA(task) = opt_data;
 1060: 	TASK_DATLEN(task) = opt_dlen;
 1061: 
 1062: 	ret = schedCall(task);
 1063: 
 1064: 	_sched_unuseTask(task);
 1065: 	return ret;
 1066: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>