2 * Copyright (c) 2010, Mark Heily <mark@heily.com>
3 * Copyright (c) 2009, Stacey Son <sson@freebsd.org>
4 * Copyright (c) 2000-2008, Apple Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 unsigned int PWQ_ACTIVE_CPU = 0;
33 int DEBUG_WORKQUEUE = 0;
34 char *WORKQUEUE_DEBUG_IDENT = "WQ";
37 valid_workq(pthread_workqueue_t workq)
39 if (workq->sig == PTHREAD_WORKQUEUE_SIG)
45 int VISIBLE CONSTRUCTOR
46 pthread_workqueue_init_np(void)
51 DEBUG_WORKQUEUE = (getenv("PWQ_DEBUG") == NULL) ? 0 : 1;
53 PWQ_RT_THREADS = (getenv("PWQ_RT_THREADS") == NULL) ? 0 : 1;
54 PWQ_ACTIVE_CPU = (getenv("PWQ_ACTIVE_CPU") == NULL) ? 0 : atoi(getenv("PWQ_ACTIVE_CPU"));
56 if (getenv("PWQ_SPIN_USEC") != NULL)
57 PWQ_SPIN_USEC = atoi(getenv("PWQ_SPIN_USEC"));
59 if (getenv("PWQ_SPIN_THREADS") != NULL)
60 PWQ_SPIN_THREADS = atoi(getenv("PWQ_SPIN_THREADS"));
65 if (manager_init() < 0)
68 dbg_puts("pthread_workqueue library initialized");
73 pthread_workqueue_create_np(pthread_workqueue_t *workqp,
74 const pthread_workqueue_attr_t * attr)
76 pthread_workqueue_t workq;
78 if ((attr != NULL) && ((attr->sig != PTHREAD_WORKQUEUE_ATTR_SIG) ||
79 (attr->queueprio < 0) || (attr->queueprio >= WORKQ_NUM_PRIOQUEUE)))
81 if ((workq = calloc(1, sizeof(*workq))) == NULL)
83 workq->sig = PTHREAD_WORKQUEUE_SIG;
85 STAILQ_INIT(&workq->item_listhead);
86 pthread_spin_init(&workq->mtx, PTHREAD_PROCESS_PRIVATE);
88 workq->queueprio = WORKQ_DEFAULT_PRIOQUEUE;
89 workq->overcommit = 0;
91 workq->queueprio = attr->queueprio;
92 workq->overcommit = attr->overcommit;
95 manager_workqueue_create(workq);
97 dbg_printf("created queue %p", (void *) workq);
104 pthread_workqueue_additem_np(pthread_workqueue_t workq,
105 void (*workitem_func)(void *), void * workitem_arg,
106 pthread_workitem_handle_t * itemhandlep,
107 unsigned int *gencountp)
111 if (valid_workq(workq) == 0)
114 witem = witem_alloc(workitem_func, workitem_arg);
116 if (itemhandlep != NULL)
117 *itemhandlep = (pthread_workitem_handle_t *) witem;
118 if (gencountp != NULL)
119 *gencountp = witem->gencount;
121 manager_workqueue_additem(workq, witem);
123 dbg_printf("added item %p to queue %p", (void *) witem, (void *) workq);
129 pthread_workqueue_attr_init_np(pthread_workqueue_attr_t *attr)
131 attr->queueprio = WORKQ_DEFAULT_PRIOQUEUE;
132 attr->sig = PTHREAD_WORKQUEUE_ATTR_SIG;
133 attr->overcommit = 0;
138 pthread_workqueue_attr_destroy_np(pthread_workqueue_attr_t *attr)
140 if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG)
143 return (EINVAL); /* Not an attribute struct. */
147 pthread_workqueue_attr_getovercommit_np(
148 const pthread_workqueue_attr_t *attr, int *ocommp)
150 if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
151 *ocommp = attr->overcommit;
154 return (EINVAL); /* Not an attribute struct. */
158 pthread_workqueue_attr_setovercommit_np(pthread_workqueue_attr_t *attr,
161 if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
162 attr->overcommit = ocomm;
169 pthread_workqueue_attr_getqueuepriority_np(
170 pthread_workqueue_attr_t *attr, int *qpriop)
172 if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
173 *qpriop = attr->queueprio;
180 pthread_workqueue_attr_setqueuepriority_np(
181 pthread_workqueue_attr_t *attr, int qprio)
183 if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
185 case WORKQ_HIGH_PRIOQUEUE:
186 case WORKQ_DEFAULT_PRIOQUEUE:
187 case WORKQ_LOW_PRIOQUEUE:
188 attr->queueprio = qprio;
197 unsigned long VISIBLE
198 pthread_workqueue_peek_np(const char *key)
200 return manager_peek(key);