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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#include <Inventor/C/threads/sched.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
/* FIXME: This class has changed somewhat since Coin-2. Tag this so
we can find it when writing Coin-2 -> Coin-3 API change documentation.
kintel 20061124. */
/* FIXME: Evaluate if some of the new functionality can be ported back
to Coin-2. kintel 20061124. */
#ifndef HAVE_THREADS
/* FIXME: instead of disallowing the use of these completely when
thread support is not available (that's why there are asserts
within them), could we perhaps implement in such a manner that they
still work, but within only the calling thread?
20051202 mortene. */
cc_sched * cc_sched_construct(int numthreads) { assert(FALSE); return NULL; }
void cc_sched_destruct(cc_sched * sched) { assert(FALSE); }
void cc_sched_set_num_threads(cc_sched * sched, int num) { assert(FALSE); }
int cc_sched_get_num_threads(cc_sched * sched) { assert(FALSE); return 0; }
uint32_t cc_sched_schedule(cc_sched * sched,
cc_sched_f * workfunc, void * closure,
float priority) { assert(FALSE); }
void cc_sched_wait_all(cc_sched * sched) { assert(FALSE); }
SbBool cc_sched_unschedule(cc_sched * sched,
uint32_t schedid) { assert(FALSE); }
void cc_sched_set_num_allowed(cc_sched * sched,
int num) { assert(FALSE); }
void cc_sched_change_priority(cc_sched * sched,
uint32_t schedid,
float priority) { assert(FALSE); }
#else /* HAVE_THREADS */
#include <stdlib.h>
#include <Inventor/C/errors/debugerror.h>
#include <Inventor/C/threads/mutex.h>
#include <Inventor/C/threads/thread.h>
#include <Inventor/C/threads/wpool.h>
#include "threads/schedp.h"
/* ********************************************************************** */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* private methods */
static void sched_worker_entry_point(void * userdata);
typedef struct {
cc_sched_f * workfunc;
void * closure;
float priority;
uint32_t schedid;
} sched_item;
static int
sched_item_compare(void * o1, void * o2)
{
sched_item * i1 = (sched_item *) o1;
sched_item * i2 = (sched_item *) o2;
float diff = i1->priority - i2->priority;
if (diff > 0) return 1;
else if (diff < 0) return -1;
return 0;
}
/* assumes mutex is locked */
static SbBool
sched_try_trigger(cc_sched * sched)
{
if (cc_wpool_try_begin(sched->pool, 1)) {
cc_wpool_start_worker(sched->pool, sched_worker_entry_point, sched);
cc_wpool_end(sched->pool);
return TRUE;
}
return FALSE;
}
void
sched_worker_entry_point(void * userdata)
{
sched_item * item;
cc_sched * sched = (cc_sched *)userdata;
cc_mutex_lock(sched->mutex);
while (!cc_heap_empty(sched->itemheap) &&
(sched->numallowed != 0 || sched->iswaitingall)) {
item = (sched_item *)cc_heap_extract_top(sched->itemheap);
cc_dict_remove(sched->schedid_dict, item->schedid);
cc_mutex_unlock(sched->mutex);
item->workfunc(item->closure);
cc_mutex_lock(sched->mutex);
cc_memalloc_deallocate(sched->itemalloc, (void *)item);
if (sched->numallowed > 0) sched->numallowed--;
}
cc_mutex_unlock(sched->mutex);
}
/* ********************************************************************** */
/* public api */
/*!
Construct a scheduler that uses \a numthreads threads.
*/
cc_sched *
cc_sched_construct(int numthreads)
{
cc_sched * sched = (cc_sched *) malloc(sizeof(cc_sched));
assert(sched);
sched->pool = cc_wpool_construct(numthreads);
sched->mutex = cc_mutex_construct();
sched->itemheap = cc_heap_construct(64, sched_item_compare, TRUE);
sched->itemalloc = cc_memalloc_construct(sizeof(sched_item));
sched->schedid_dict = cc_dict_construct(64, 0.75f);
sched->schedid_counter = 1;
sched->iswaitingall = FALSE;
sched->numallowed = -1; /* Unlimited */
return sched;
}
/*!
Destruct the scheduler.
This method will block until all currently executing jobs have finished.
Any remaining scheduled jobs will be cancelled.
Note that this differs from Coin-2. To emulate Coin-2 behavior, call
cc_sched_wait_all() before calling this method.
*/
void
cc_sched_destruct(cc_sched * sched)
{
cc_sched_set_num_allowed(sched, 0); // Exit inner scheduler loop faster
cc_wpool_wait_all(sched->pool); // Make sure all worker threads are finished
cc_dict_destruct(sched->schedid_dict);
cc_heap_destruct(sched->itemheap);
cc_memalloc_destruct(sched->itemalloc);
cc_mutex_destruct(sched->mutex);
cc_wpool_destruct(sched->pool);
free(sched);
}
/*!
Set/change the number of threads used by the scheduler.
*/
void
cc_sched_set_num_threads(cc_sched * sched, int num)
{
cc_sched_wait_all(sched);
cc_wpool_set_num_workers(sched->pool, num);
}
/*!
Returns the number of threads used by the scheduler.
*/
int
cc_sched_get_num_threads(cc_sched * sched)
{
return cc_wpool_get_num_workers(sched->pool);
}
/*!
Schedule a new job. A thread calls \a workfunc with the \a closure
argument when a thread becomes available. larger \a priority values will
be scheduled first.
Returns a schedid that can be used to unschedule the job. schedid is
guaranteed to be != 0.
Note that jobs are automatically unscheduled when triggered, just before
calling the work function.
*/
uint32_t
cc_sched_schedule(cc_sched * sched,
cc_sched_f * workfunc, void * closure,
float priority)
{
sched_item * item;
cc_mutex_lock(sched->mutex);
item = (sched_item *)cc_memalloc_allocate(sched->itemalloc);
item->workfunc = workfunc;
item->closure = closure;
item->priority = priority;
item->schedid = sched->schedid_counter++;
// avoid schedid == 0
if (item->schedid == 0) {
item->schedid = sched->schedid_counter++;
}
cc_heap_add(sched->itemheap, (void *)item);
cc_dict_put(sched->schedid_dict, item->schedid, (void *)item);
if (cc_dict_get_num_elements(sched->schedid_dict) == 1) {
sched_try_trigger(sched);
}
cc_mutex_unlock(sched->mutex);
return item->schedid;
}
/*!
Attempt to unschedule a job. \a schedid must be an id returned
from cc_sched_schedule().
Note that jobs are automatically unscheduled when triggered, just before
calling the work function.
Returns TRUE if job was successfully removed, FALSE if job wasn't found
in the internal dict.
*/
SbBool
cc_sched_unschedule(cc_sched * sched, uint32_t schedid)
{
SbBool didremove = FALSE;
void * item = NULL;
cc_mutex_lock(sched->mutex);
if (cc_dict_get(sched->schedid_dict, schedid, &item)) {
cc_heap_remove(sched->itemheap, item);
cc_dict_remove(sched->schedid_dict, schedid);
cc_memalloc_deallocate(sched->itemalloc, item);
didremove = TRUE;
}
cc_mutex_unlock(sched->mutex);
return didremove;
}
/*!
Returns the number of remaining scheduled jobs, not counting jobs that
are currently being executed (i.e. are in their workfuncs).
*/
int
cc_sched_get_num_remaining(cc_sched * sched)
{
int num;
cc_mutex_lock(sched->mutex);
num = (int)cc_heap_elements(sched->itemheap);
cc_mutex_unlock(sched->mutex);
return num;
}
/*!
Wait for all scheduled jobs to finish.
*/
void
cc_sched_wait_all(cc_sched * sched)
{
cc_mutex_lock(sched->mutex);
sched->iswaitingall = TRUE;
/* Make sure all workers are doing something */
while (!cc_heap_empty(sched->itemheap) && sched_try_trigger(sched)) { }
cc_mutex_unlock(sched->mutex);
cc_wpool_wait_all(sched->pool);
cc_mutex_lock(sched->mutex);
sched->iswaitingall = FALSE;
cc_mutex_unlock(sched->mutex);
}
/*!
Sets the number of allowed jobs per "batch". The scheduler will execute
at max the \e num jobs. To continue executing jobs, call this function again.
This is typically done to limit the number of I/O or CPU intensive jobs to
a few per frame to avoid starving the main thread.
If \e num is -1 (the default), the number of jobs per batch is not limited.
*/
void
cc_sched_set_num_allowed(cc_sched * sched, int num)
{
cc_mutex_lock(sched->mutex);
sched->numallowed = num;
sched_try_trigger(sched);
cc_mutex_unlock(sched->mutex);
}
/*!
Changes the priority of the given scheduled item.
*/
void
cc_sched_change_priority(cc_sched * sched,
uint32_t schedid, float priority)
{
void * item;
cc_mutex_lock(sched->mutex);
if (cc_dict_get(sched->schedid_dict, schedid, &item)) {
cc_heap_remove(sched->itemheap, item);
((sched_item *)item)->priority = priority;
cc_heap_add(sched->itemheap, item);
}
cc_mutex_unlock(sched->mutex);
}
/* ********************************************************************** */
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* HAVE_THREADS */
|