1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*-
* Copyright (c) 2010, Mark Heily <mark@heily.com>
* Copyright (c) 2009, Stacey Son <sson@freebsd.org>
* Copyright (c) 2000-2008, Apple Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "private.h"
unsigned int PWQ_ACTIVE_CPU = 0;
int DEBUG_WORKQUEUE = 0;
char *WORKQUEUE_DEBUG_IDENT = "WQ";
static int
valid_workq(pthread_workqueue_t workq)
{
if (workq->sig == PTHREAD_WORKQUEUE_SIG)
return (1);
else
return (0);
}
int VISIBLE CONSTRUCTOR
pthread_workqueue_init_np(void)
{
#ifdef NDEBUG
DEBUG_WORKQUEUE = 0;
#else
DEBUG_WORKQUEUE = (getenv("PWQ_DEBUG") == NULL) ? 0 : 1;
# ifndef _WIN32
PWQ_RT_THREADS = (getenv("PWQ_RT_THREADS") == NULL) ? 0 : 1;
PWQ_ACTIVE_CPU = (getenv("PWQ_ACTIVE_CPU") == NULL) ? 0 : atoi(getenv("PWQ_ACTIVE_CPU"));
if (getenv("PWQ_SPIN_USEC") != NULL)
PWQ_SPIN_USEC = atoi(getenv("PWQ_SPIN_USEC"));
if (getenv("PWQ_SPIN_THREADS") != NULL)
PWQ_SPIN_THREADS = atoi(getenv("PWQ_SPIN_THREADS"));
# endif
#endif
if (manager_init() < 0)
return (-1);
dbg_puts("pthread_workqueue library initialized");
return (0);
}
int VISIBLE
pthread_workqueue_create_np(pthread_workqueue_t *workqp,
const pthread_workqueue_attr_t * attr)
{
pthread_workqueue_t workq;
if ((attr != NULL) && ((attr->sig != PTHREAD_WORKQUEUE_ATTR_SIG) ||
(attr->queueprio < 0) || (attr->queueprio >= WORKQ_NUM_PRIOQUEUE)))
return (EINVAL);
if ((workq = calloc(1, sizeof(*workq))) == NULL)
return (ENOMEM);
workq->sig = PTHREAD_WORKQUEUE_SIG;
workq->flags = 0;
STAILQ_INIT(&workq->item_listhead);
pthread_spin_init(&workq->mtx, PTHREAD_PROCESS_PRIVATE);
if (attr == NULL) {
workq->queueprio = WORKQ_DEFAULT_PRIOQUEUE;
workq->overcommit = 0;
} else {
workq->queueprio = attr->queueprio;
workq->overcommit = attr->overcommit;
}
manager_workqueue_create(workq);
dbg_printf("created queue %p", (void *) workq);
*workqp = workq;
return (0);
}
int VISIBLE
pthread_workqueue_additem_np(pthread_workqueue_t workq,
void (*workitem_func)(void *), void * workitem_arg,
pthread_workitem_handle_t * itemhandlep,
unsigned int *gencountp)
{
struct work *witem;
if (valid_workq(workq) == 0)
return (EINVAL);
witem = witem_alloc(workitem_func, workitem_arg);
if (itemhandlep != NULL)
*itemhandlep = (pthread_workitem_handle_t *) witem;
if (gencountp != NULL)
*gencountp = witem->gencount;
manager_workqueue_additem(workq, witem);
dbg_printf("added item %p to queue %p", (void *) witem, (void *) workq);
return (0);
}
int VISIBLE
pthread_workqueue_attr_init_np(pthread_workqueue_attr_t *attr)
{
attr->queueprio = WORKQ_DEFAULT_PRIOQUEUE;
attr->sig = PTHREAD_WORKQUEUE_ATTR_SIG;
attr->overcommit = 0;
return (0);
}
int VISIBLE
pthread_workqueue_attr_destroy_np(pthread_workqueue_attr_t *attr)
{
if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG)
return (0);
else
return (EINVAL); /* Not an attribute struct. */
}
int VISIBLE
pthread_workqueue_attr_getovercommit_np(
const pthread_workqueue_attr_t *attr, int *ocommp)
{
if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
*ocommp = attr->overcommit;
return (0);
} else
return (EINVAL); /* Not an attribute struct. */
}
int VISIBLE
pthread_workqueue_attr_setovercommit_np(pthread_workqueue_attr_t *attr,
int ocomm)
{
if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
attr->overcommit = ocomm;
return (0);
} else
return (EINVAL);
}
int VISIBLE
pthread_workqueue_attr_getqueuepriority_np(
pthread_workqueue_attr_t *attr, int *qpriop)
{
if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
*qpriop = attr->queueprio;
return (0);
} else
return (EINVAL);
}
int VISIBLE
pthread_workqueue_attr_setqueuepriority_np(
pthread_workqueue_attr_t *attr, int qprio)
{
if (attr->sig == PTHREAD_WORKQUEUE_ATTR_SIG) {
switch(qprio) {
case WORKQ_HIGH_PRIOQUEUE:
case WORKQ_DEFAULT_PRIOQUEUE:
case WORKQ_LOW_PRIOQUEUE:
attr->queueprio = qprio;
return (0);
default:
return (EINVAL);
}
} else
return (EINVAL);
}
unsigned long VISIBLE
pthread_workqueue_peek_np(const char *key)
{
return manager_peek(key);
}
|