1 1.1 christos /* $NetBSD: task.c,v 1.1 2024/02/18 20:57:50 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * 6 1.1 christos * SPDX-License-Identifier: MPL-2.0 7 1.1 christos * 8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public 9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this 10 1.1 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 1.1 christos * 12 1.1 christos * See the COPYRIGHT file distributed with this work for additional 13 1.1 christos * information regarding copyright ownership. 14 1.1 christos */ 15 1.1 christos 16 1.1 christos /*! \file */ 17 1.1 christos 18 1.1 christos /* 19 1.1 christos * XXXRTH Need to document the states a task can be in, and the rules 20 1.1 christos * for changing states. 21 1.1 christos */ 22 1.1 christos 23 1.1 christos #include <stdbool.h> 24 1.1 christos #include <unistd.h> 25 1.1 christos 26 1.1 christos #include <isc/app.h> 27 1.1 christos #include <isc/atomic.h> 28 1.1 christos #include <isc/condition.h> 29 1.1 christos #include <isc/event.h> 30 1.1 christos #include <isc/log.h> 31 1.1 christos #include <isc/magic.h> 32 1.1 christos #include <isc/mem.h> 33 1.1 christos #include <isc/once.h> 34 1.1 christos #include <isc/platform.h> 35 1.1 christos #include <isc/print.h> 36 1.1 christos #include <isc/random.h> 37 1.1 christos #include <isc/refcount.h> 38 1.1 christos #include <isc/string.h> 39 1.1 christos #include <isc/task.h> 40 1.1 christos #include <isc/thread.h> 41 1.1 christos #include <isc/time.h> 42 1.1 christos #include <isc/util.h> 43 1.1 christos 44 1.1 christos #ifdef HAVE_LIBXML2 45 1.1 christos #include <libxml/xmlwriter.h> 46 1.1 christos #define ISC_XMLCHAR (const xmlChar *) 47 1.1 christos #endif /* HAVE_LIBXML2 */ 48 1.1 christos 49 1.1 christos #ifdef HAVE_JSON_C 50 1.1 christos #include <json_object.h> 51 1.1 christos #endif /* HAVE_JSON_C */ 52 1.1 christos 53 1.1 christos #include "task_p.h" 54 1.1 christos 55 1.1 christos /* 56 1.1 christos * Task manager is built around 'as little locking as possible' concept. 57 1.1 christos * Each thread has his own queue of tasks to be run, if a task is in running 58 1.1 christos * state it will stay on the runner it's currently on, if a task is in idle 59 1.1 christos * state it can be woken up on a specific runner with isc_task_sendto - that 60 1.1 christos * helps with data locality on CPU. 61 1.1 christos * 62 1.1 christos * To make load even some tasks (from task pools) are bound to specific 63 1.1 christos * queues using isc_task_create_bound. This way load balancing between 64 1.1 christos * CPUs/queues happens on the higher layer. 65 1.1 christos */ 66 1.1 christos 67 1.1 christos #ifdef ISC_TASK_TRACE 68 1.1 christos #define XTRACE(m) \ 69 1.1 christos fprintf(stderr, "task %p thread %zu: %s\n", task, isc_tid_v, (m)) 70 1.1 christos #define XTTRACE(t, m) \ 71 1.1 christos fprintf(stderr, "task %p thread %zu: %s\n", (t), isc_tid_v, (m)) 72 1.1 christos #define XTHREADTRACE(m) fprintf(stderr, "thread %zu: %s\n", isc_tid_v, (m)) 73 1.1 christos #else /* ifdef ISC_TASK_TRACE */ 74 1.1 christos #define XTRACE(m) 75 1.1 christos #define XTTRACE(t, m) 76 1.1 christos #define XTHREADTRACE(m) 77 1.1 christos #endif /* ifdef ISC_TASK_TRACE */ 78 1.1 christos 79 1.1 christos /*** 80 1.1 christos *** Types. 81 1.1 christos ***/ 82 1.1 christos 83 1.1 christos typedef enum { 84 1.1 christos task_state_idle, /* not doing anything, events queue empty */ 85 1.1 christos task_state_ready, /* waiting in worker's queue */ 86 1.1 christos task_state_paused, /* not running, paused */ 87 1.1 christos task_state_pausing, /* running, waiting to be paused */ 88 1.1 christos task_state_running, /* actively processing events */ 89 1.1 christos task_state_done /* shutting down, no events or references */ 90 1.1 christos } task_state_t; 91 1.1 christos 92 1.1 christos #if defined(HAVE_LIBXML2) || defined(HAVE_JSON_C) 93 1.1 christos static const char *statenames[] = { 94 1.1 christos "idle", "ready", "paused", "pausing", "running", "done", 95 1.1 christos }; 96 1.1 christos #endif /* if defined(HAVE_LIBXML2) || defined(HAVE_JSON_C) */ 97 1.1 christos 98 1.1 christos #define TASK_MAGIC ISC_MAGIC('T', 'A', 'S', 'K') 99 1.1 christos #define VALID_TASK(t) ISC_MAGIC_VALID(t, TASK_MAGIC) 100 1.1 christos 101 1.1 christos struct isc_task { 102 1.1 christos /* Not locked. */ 103 1.1 christos unsigned int magic; 104 1.1 christos isc_taskmgr_t *manager; 105 1.1 christos isc_mutex_t lock; 106 1.1 christos /* Locked by task lock. */ 107 1.1 christos int threadid; 108 1.1 christos task_state_t state; 109 1.1 christos int pause_cnt; 110 1.1 christos isc_refcount_t references; 111 1.1 christos isc_refcount_t running; 112 1.1 christos isc_eventlist_t events; 113 1.1 christos isc_eventlist_t on_shutdown; 114 1.1 christos unsigned int nevents; 115 1.1 christos unsigned int quantum; 116 1.1 christos isc_stdtime_t now; 117 1.1 christos isc_time_t tnow; 118 1.1 christos char name[16]; 119 1.1 christos void *tag; 120 1.1 christos bool bound; 121 1.1 christos /* Protected by atomics */ 122 1.1 christos atomic_bool shuttingdown; 123 1.1 christos atomic_bool privileged; 124 1.1 christos /* Locked by task manager lock. */ 125 1.1 christos LINK(isc_task_t) link; 126 1.1 christos }; 127 1.1 christos 128 1.1 christos #define TASK_SHUTTINGDOWN(t) (atomic_load_acquire(&(t)->shuttingdown)) 129 1.1 christos #define TASK_PRIVILEGED(t) (atomic_load_acquire(&(t)->privileged)) 130 1.1 christos 131 1.1 christos #define TASK_MANAGER_MAGIC ISC_MAGIC('T', 'S', 'K', 'M') 132 1.1 christos #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, TASK_MANAGER_MAGIC) 133 1.1 christos 134 1.1 christos struct isc_taskmgr { 135 1.1 christos /* Not locked. */ 136 1.1 christos unsigned int magic; 137 1.1 christos isc_refcount_t references; 138 1.1 christos isc_mem_t *mctx; 139 1.1 christos isc_mutex_t lock; 140 1.1 christos atomic_uint_fast32_t tasks_count; 141 1.1 christos isc_nm_t *netmgr; 142 1.1 christos 143 1.1 christos /* Locked by task manager lock. */ 144 1.1 christos unsigned int default_quantum; 145 1.1 christos LIST(isc_task_t) tasks; 146 1.1 christos atomic_uint_fast32_t mode; 147 1.1 christos atomic_bool exclusive_req; 148 1.1 christos bool exiting; 149 1.1 christos isc_task_t *excl; 150 1.1 christos }; 151 1.1 christos 152 1.1 christos #define DEFAULT_DEFAULT_QUANTUM 25 153 1.1 christos 154 1.1 christos /*% 155 1.1 christos * The following are intended for internal use (indicated by "isc__" 156 1.1 christos * prefix) but are not declared as static, allowing direct access from 157 1.1 christos * unit tests etc. 158 1.1 christos */ 159 1.1 christos 160 1.1 christos bool 161 1.1 christos isc_task_purgeevent(isc_task_t *task, isc_event_t *event); 162 1.1 christos void 163 1.1 christos isc_taskmgr_setexcltask(isc_taskmgr_t *mgr, isc_task_t *task); 164 1.1 christos isc_result_t 165 1.1 christos isc_taskmgr_excltask(isc_taskmgr_t *mgr, isc_task_t **taskp); 166 1.1 christos 167 1.1 christos /*** 168 1.1 christos *** Tasks. 169 1.1 christos ***/ 170 1.1 christos 171 1.1 christos static void 172 1.1 christos task_finished(isc_task_t *task) { 173 1.1 christos isc_taskmgr_t *manager = task->manager; 174 1.1 christos isc_mem_t *mctx = manager->mctx; 175 1.1 christos REQUIRE(EMPTY(task->events)); 176 1.1 christos REQUIRE(task->nevents == 0); 177 1.1 christos REQUIRE(EMPTY(task->on_shutdown)); 178 1.1 christos REQUIRE(task->state == task_state_done); 179 1.1 christos 180 1.1 christos XTRACE("task_finished"); 181 1.1 christos 182 1.1 christos isc_refcount_destroy(&task->running); 183 1.1 christos isc_refcount_destroy(&task->references); 184 1.1 christos 185 1.1 christos LOCK(&manager->lock); 186 1.1 christos UNLINK(manager->tasks, task, link); 187 1.1 christos atomic_fetch_sub(&manager->tasks_count, 1); 188 1.1 christos UNLOCK(&manager->lock); 189 1.1 christos 190 1.1 christos isc_mutex_destroy(&task->lock); 191 1.1 christos task->magic = 0; 192 1.1 christos isc_mem_put(mctx, task, sizeof(*task)); 193 1.1 christos 194 1.1 christos isc_taskmgr_detach(&manager); 195 1.1 christos } 196 1.1 christos 197 1.1 christos isc_result_t 198 1.1 christos isc_task_create(isc_taskmgr_t *manager, unsigned int quantum, 199 1.1 christos isc_task_t **taskp) { 200 1.1 christos return (isc_task_create_bound(manager, quantum, taskp, -1)); 201 1.1 christos } 202 1.1 christos 203 1.1 christos isc_result_t 204 1.1 christos isc_task_create_bound(isc_taskmgr_t *manager, unsigned int quantum, 205 1.1 christos isc_task_t **taskp, int threadid) { 206 1.1 christos isc_task_t *task = NULL; 207 1.1 christos bool exiting; 208 1.1 christos 209 1.1 christos REQUIRE(VALID_MANAGER(manager)); 210 1.1 christos REQUIRE(taskp != NULL && *taskp == NULL); 211 1.1 christos 212 1.1 christos XTRACE("isc_task_create"); 213 1.1 christos 214 1.1 christos task = isc_mem_get(manager->mctx, sizeof(*task)); 215 1.1 christos *task = (isc_task_t){ 0 }; 216 1.1 christos 217 1.1 christos isc_taskmgr_attach(manager, &task->manager); 218 1.1 christos 219 1.1 christos if (threadid == -1) { 220 1.1 christos /* 221 1.1 christos * Task is not pinned to a queue, it's threadid will be 222 1.1 christos * chosen when first task will be sent to it - either 223 1.1 christos * randomly or specified by isc_task_sendto. 224 1.1 christos */ 225 1.1 christos task->bound = false; 226 1.1 christos task->threadid = -1; 227 1.1 christos } else { 228 1.1 christos /* 229 1.1 christos * Task is pinned to a queue, it'll always be run 230 1.1 christos * by a specific thread. 231 1.1 christos */ 232 1.1 christos task->bound = true; 233 1.1 christos task->threadid = threadid; 234 1.1 christos } 235 1.1 christos 236 1.1 christos isc_mutex_init(&task->lock); 237 1.1 christos task->state = task_state_idle; 238 1.1 christos task->pause_cnt = 0; 239 1.1 christos 240 1.1 christos isc_refcount_init(&task->references, 1); 241 1.1 christos isc_refcount_init(&task->running, 0); 242 1.1 christos INIT_LIST(task->events); 243 1.1 christos INIT_LIST(task->on_shutdown); 244 1.1 christos task->nevents = 0; 245 1.1 christos task->quantum = (quantum > 0) ? quantum : manager->default_quantum; 246 1.1 christos atomic_init(&task->shuttingdown, false); 247 1.1 christos atomic_init(&task->privileged, false); 248 1.1 christos task->now = 0; 249 1.1 christos isc_time_settoepoch(&task->tnow); 250 1.1 christos memset(task->name, 0, sizeof(task->name)); 251 1.1 christos task->tag = NULL; 252 1.1 christos INIT_LINK(task, link); 253 1.1 christos task->magic = TASK_MAGIC; 254 1.1 christos 255 1.1 christos LOCK(&manager->lock); 256 1.1 christos exiting = manager->exiting; 257 1.1 christos if (!exiting) { 258 1.1 christos APPEND(manager->tasks, task, link); 259 1.1 christos atomic_fetch_add(&manager->tasks_count, 1); 260 1.1 christos } 261 1.1 christos UNLOCK(&manager->lock); 262 1.1 christos 263 1.1 christos if (exiting) { 264 1.1 christos isc_refcount_destroy(&task->running); 265 1.1 christos isc_refcount_decrement(&task->references); 266 1.1 christos isc_refcount_destroy(&task->references); 267 1.1 christos isc_mutex_destroy(&task->lock); 268 1.1 christos isc_taskmgr_detach(&task->manager); 269 1.1 christos isc_mem_put(manager->mctx, task, sizeof(*task)); 270 1.1 christos return (ISC_R_SHUTTINGDOWN); 271 1.1 christos } 272 1.1 christos 273 1.1 christos *taskp = task; 274 1.1 christos 275 1.1 christos return (ISC_R_SUCCESS); 276 1.1 christos } 277 1.1 christos 278 1.1 christos void 279 1.1 christos isc_task_attach(isc_task_t *source, isc_task_t **targetp) { 280 1.1 christos /* 281 1.1 christos * Attach *targetp to source. 282 1.1 christos */ 283 1.1 christos 284 1.1 christos REQUIRE(VALID_TASK(source)); 285 1.1 christos REQUIRE(targetp != NULL && *targetp == NULL); 286 1.1 christos 287 1.1 christos XTTRACE(source, "isc_task_attach"); 288 1.1 christos 289 1.1 christos isc_refcount_increment(&source->references); 290 1.1 christos 291 1.1 christos *targetp = source; 292 1.1 christos } 293 1.1 christos 294 1.1 christos static bool 295 1.1 christos task_shutdown(isc_task_t *task) { 296 1.1 christos bool was_idle = false; 297 1.1 christos isc_event_t *event, *prev; 298 1.1 christos 299 1.1 christos /* 300 1.1 christos * Caller must be holding the task's lock. 301 1.1 christos */ 302 1.1 christos 303 1.1 christos XTRACE("task_shutdown"); 304 1.1 christos 305 1.1 christos if (atomic_compare_exchange_strong(&task->shuttingdown, 306 1.1 christos &(bool){ false }, true)) 307 1.1 christos { 308 1.1 christos XTRACE("shutting down"); 309 1.1 christos if (task->state == task_state_idle) { 310 1.1 christos INSIST(EMPTY(task->events)); 311 1.1 christos task->state = task_state_ready; 312 1.1 christos was_idle = true; 313 1.1 christos } 314 1.1 christos INSIST(task->state == task_state_ready || 315 1.1 christos task->state == task_state_paused || 316 1.1 christos task->state == task_state_pausing || 317 1.1 christos task->state == task_state_running); 318 1.1 christos 319 1.1 christos /* 320 1.1 christos * Note that we post shutdown events LIFO. 321 1.1 christos */ 322 1.1 christos for (event = TAIL(task->on_shutdown); event != NULL; 323 1.1 christos event = prev) 324 1.1 christos { 325 1.1 christos prev = PREV(event, ev_link); 326 1.1 christos DEQUEUE(task->on_shutdown, event, ev_link); 327 1.1 christos ENQUEUE(task->events, event, ev_link); 328 1.1 christos task->nevents++; 329 1.1 christos } 330 1.1 christos } 331 1.1 christos 332 1.1 christos return (was_idle); 333 1.1 christos } 334 1.1 christos 335 1.1 christos /* 336 1.1 christos * Moves a task onto the appropriate run queue. 337 1.1 christos * 338 1.1 christos * Caller must NOT hold queue lock. 339 1.1 christos */ 340 1.1 christos static void 341 1.1 christos task_ready(isc_task_t *task) { 342 1.1 christos isc_taskmgr_t *manager = task->manager; 343 1.1 christos REQUIRE(VALID_MANAGER(manager)); 344 1.1 christos 345 1.1 christos XTRACE("task_ready"); 346 1.1 christos 347 1.1 christos isc_refcount_increment0(&task->running); 348 1.1 christos LOCK(&task->lock); 349 1.1 christos isc_nm_task_enqueue(manager->netmgr, task, task->threadid); 350 1.1 christos UNLOCK(&task->lock); 351 1.1 christos } 352 1.1 christos 353 1.1 christos void 354 1.1 christos isc_task_ready(isc_task_t *task) { 355 1.1 christos task_ready(task); 356 1.1 christos } 357 1.1 christos 358 1.1 christos static bool 359 1.1 christos task_detach(isc_task_t *task) { 360 1.1 christos /* 361 1.1 christos * Caller must be holding the task lock. 362 1.1 christos */ 363 1.1 christos 364 1.1 christos XTRACE("detach"); 365 1.1 christos 366 1.1 christos if (isc_refcount_decrement(&task->references) == 1 && 367 1.1 christos task->state == task_state_idle) 368 1.1 christos { 369 1.1 christos INSIST(EMPTY(task->events)); 370 1.1 christos /* 371 1.1 christos * There are no references to this task, and no 372 1.1 christos * pending events. We could try to optimize and 373 1.1 christos * either initiate shutdown or clean up the task, 374 1.1 christos * depending on its state, but it's easier to just 375 1.1 christos * make the task ready and allow run() or the event 376 1.1 christos * loop to deal with shutting down and termination. 377 1.1 christos */ 378 1.1 christos task->state = task_state_ready; 379 1.1 christos return (true); 380 1.1 christos } 381 1.1 christos 382 1.1 christos return (false); 383 1.1 christos } 384 1.1 christos 385 1.1 christos void 386 1.1 christos isc_task_detach(isc_task_t **taskp) { 387 1.1 christos isc_task_t *task; 388 1.1 christos bool was_idle; 389 1.1 christos 390 1.1 christos /* 391 1.1 christos * Detach *taskp from its task. 392 1.1 christos */ 393 1.1 christos 394 1.1 christos REQUIRE(taskp != NULL); 395 1.1 christos task = *taskp; 396 1.1 christos REQUIRE(VALID_TASK(task)); 397 1.1 christos 398 1.1 christos XTRACE("isc_task_detach"); 399 1.1 christos 400 1.1 christos LOCK(&task->lock); 401 1.1 christos was_idle = task_detach(task); 402 1.1 christos UNLOCK(&task->lock); 403 1.1 christos 404 1.1 christos if (was_idle) { 405 1.1 christos task_ready(task); 406 1.1 christos } 407 1.1 christos 408 1.1 christos *taskp = NULL; 409 1.1 christos } 410 1.1 christos 411 1.1 christos static bool 412 1.1 christos task_send(isc_task_t *task, isc_event_t **eventp, int c) { 413 1.1 christos bool was_idle = false; 414 1.1 christos isc_event_t *event; 415 1.1 christos 416 1.1 christos /* 417 1.1 christos * Caller must be holding the task lock. 418 1.1 christos */ 419 1.1 christos 420 1.1 christos REQUIRE(eventp != NULL); 421 1.1 christos event = *eventp; 422 1.1 christos *eventp = NULL; 423 1.1 christos REQUIRE(event != NULL); 424 1.1 christos REQUIRE(event->ev_type > 0); 425 1.1 christos REQUIRE(task->state != task_state_done); 426 1.1 christos REQUIRE(!ISC_LINK_LINKED(event, ev_ratelink)); 427 1.1 christos 428 1.1 christos XTRACE("task_send"); 429 1.1 christos 430 1.1 christos if (task->bound) { 431 1.1 christos c = task->threadid; 432 1.1 christos } else if (c < 0) { 433 1.1 christos c = -1; 434 1.1 christos } 435 1.1 christos 436 1.1 christos if (task->state == task_state_idle) { 437 1.1 christos was_idle = true; 438 1.1 christos task->threadid = c; 439 1.1 christos INSIST(EMPTY(task->events)); 440 1.1 christos task->state = task_state_ready; 441 1.1 christos } 442 1.1 christos INSIST(task->state == task_state_ready || 443 1.1 christos task->state == task_state_running || 444 1.1 christos task->state == task_state_paused || 445 1.1 christos task->state == task_state_pausing); 446 1.1 christos ENQUEUE(task->events, event, ev_link); 447 1.1 christos task->nevents++; 448 1.1 christos 449 1.1 christos return (was_idle); 450 1.1 christos } 451 1.1 christos 452 1.1 christos void 453 1.1 christos isc_task_send(isc_task_t *task, isc_event_t **eventp) { 454 1.1 christos isc_task_sendto(task, eventp, -1); 455 1.1 christos } 456 1.1 christos 457 1.1 christos void 458 1.1 christos isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) { 459 1.1 christos isc_task_sendtoanddetach(taskp, eventp, -1); 460 1.1 christos } 461 1.1 christos 462 1.1 christos void 463 1.1 christos isc_task_sendto(isc_task_t *task, isc_event_t **eventp, int c) { 464 1.1 christos bool was_idle; 465 1.1 christos 466 1.1 christos /* 467 1.1 christos * Send '*event' to 'task'. 468 1.1 christos */ 469 1.1 christos 470 1.1 christos REQUIRE(VALID_TASK(task)); 471 1.1 christos XTRACE("isc_task_send"); 472 1.1 christos 473 1.1 christos /* 474 1.1 christos * We're trying hard to hold locks for as short a time as possible. 475 1.1 christos * We're also trying to hold as few locks as possible. This is why 476 1.1 christos * some processing is deferred until after the lock is released. 477 1.1 christos */ 478 1.1 christos LOCK(&task->lock); 479 1.1 christos was_idle = task_send(task, eventp, c); 480 1.1 christos UNLOCK(&task->lock); 481 1.1 christos 482 1.1 christos if (was_idle) { 483 1.1 christos /* 484 1.1 christos * We need to add this task to the ready queue. 485 1.1 christos * 486 1.1 christos * We've waited until now to do it because making a task 487 1.1 christos * ready requires locking the manager. If we tried to do 488 1.1 christos * this while holding the task lock, we could deadlock. 489 1.1 christos * 490 1.1 christos * We've changed the state to ready, so no one else will 491 1.1 christos * be trying to add this task to the ready queue. The 492 1.1 christos * only way to leave the ready state is by executing the 493 1.1 christos * task. It thus doesn't matter if events are added, 494 1.1 christos * removed, or a shutdown is started in the interval 495 1.1 christos * between the time we released the task lock, and the time 496 1.1 christos * we add the task to the ready queue. 497 1.1 christos */ 498 1.1 christos task_ready(task); 499 1.1 christos } 500 1.1 christos } 501 1.1 christos 502 1.1 christos void 503 1.1 christos isc_task_sendtoanddetach(isc_task_t **taskp, isc_event_t **eventp, int c) { 504 1.1 christos bool idle1, idle2; 505 1.1 christos isc_task_t *task; 506 1.1 christos 507 1.1 christos /* 508 1.1 christos * Send '*event' to '*taskp' and then detach '*taskp' from its 509 1.1 christos * task. 510 1.1 christos */ 511 1.1 christos 512 1.1 christos REQUIRE(taskp != NULL); 513 1.1 christos task = *taskp; 514 1.1 christos REQUIRE(VALID_TASK(task)); 515 1.1 christos XTRACE("isc_task_sendanddetach"); 516 1.1 christos 517 1.1 christos LOCK(&task->lock); 518 1.1 christos idle1 = task_send(task, eventp, c); 519 1.1 christos idle2 = task_detach(task); 520 1.1 christos UNLOCK(&task->lock); 521 1.1 christos 522 1.1 christos /* 523 1.1 christos * If idle1, then idle2 shouldn't be true as well since we're holding 524 1.1 christos * the task lock, and thus the task cannot switch from ready back to 525 1.1 christos * idle. 526 1.1 christos */ 527 1.1 christos INSIST(!(idle1 && idle2)); 528 1.1 christos 529 1.1 christos if (idle1 || idle2) { 530 1.1 christos task_ready(task); 531 1.1 christos } 532 1.1 christos 533 1.1 christos *taskp = NULL; 534 1.1 christos } 535 1.1 christos 536 1.1 christos #define PURGE_OK(event) (((event)->ev_attributes & ISC_EVENTATTR_NOPURGE) == 0) 537 1.1 christos 538 1.1 christos static unsigned int 539 1.1 christos dequeue_events(isc_task_t *task, void *sender, isc_eventtype_t first, 540 1.1 christos isc_eventtype_t last, void *tag, isc_eventlist_t *events, 541 1.1 christos bool purging) { 542 1.1 christos isc_event_t *event, *next_event; 543 1.1 christos unsigned int count = 0; 544 1.1 christos 545 1.1 christos REQUIRE(VALID_TASK(task)); 546 1.1 christos REQUIRE(last >= first); 547 1.1 christos 548 1.1 christos XTRACE("dequeue_events"); 549 1.1 christos 550 1.1 christos /* 551 1.1 christos * Events matching 'sender', whose type is >= first and <= last, and 552 1.1 christos * whose tag is 'tag' will be dequeued. If 'purging', matching events 553 1.1 christos * which are marked as unpurgable will not be dequeued. 554 1.1 christos * 555 1.1 christos * sender == NULL means "any sender", and tag == NULL means "any tag". 556 1.1 christos */ 557 1.1 christos 558 1.1 christos LOCK(&task->lock); 559 1.1 christos 560 1.1 christos for (event = HEAD(task->events); event != NULL; event = next_event) { 561 1.1 christos next_event = NEXT(event, ev_link); 562 1.1 christos if (event->ev_type >= first && event->ev_type <= last && 563 1.1 christos (sender == NULL || event->ev_sender == sender) && 564 1.1 christos (tag == NULL || event->ev_tag == tag) && 565 1.1 christos (!purging || PURGE_OK(event))) 566 1.1 christos { 567 1.1 christos DEQUEUE(task->events, event, ev_link); 568 1.1 christos task->nevents--; 569 1.1 christos ENQUEUE(*events, event, ev_link); 570 1.1 christos count++; 571 1.1 christos } 572 1.1 christos } 573 1.1 christos 574 1.1 christos UNLOCK(&task->lock); 575 1.1 christos 576 1.1 christos return (count); 577 1.1 christos } 578 1.1 christos 579 1.1 christos unsigned int 580 1.1 christos isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first, 581 1.1 christos isc_eventtype_t last, void *tag) { 582 1.1 christos unsigned int count; 583 1.1 christos isc_eventlist_t events; 584 1.1 christos isc_event_t *event, *next_event; 585 1.1 christos REQUIRE(VALID_TASK(task)); 586 1.1 christos 587 1.1 christos /* 588 1.1 christos * Purge events from a task's event queue. 589 1.1 christos */ 590 1.1 christos 591 1.1 christos XTRACE("isc_task_purgerange"); 592 1.1 christos 593 1.1 christos ISC_LIST_INIT(events); 594 1.1 christos 595 1.1 christos count = dequeue_events(task, sender, first, last, tag, &events, true); 596 1.1 christos 597 1.1 christos for (event = HEAD(events); event != NULL; event = next_event) { 598 1.1 christos next_event = NEXT(event, ev_link); 599 1.1 christos ISC_LIST_UNLINK(events, event, ev_link); 600 1.1 christos isc_event_free(&event); 601 1.1 christos } 602 1.1 christos 603 1.1 christos /* 604 1.1 christos * Note that purging never changes the state of the task. 605 1.1 christos */ 606 1.1 christos 607 1.1 christos return (count); 608 1.1 christos } 609 1.1 christos 610 1.1 christos unsigned int 611 1.1 christos isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, 612 1.1 christos void *tag) { 613 1.1 christos /* 614 1.1 christos * Purge events from a task's event queue. 615 1.1 christos */ 616 1.1 christos REQUIRE(VALID_TASK(task)); 617 1.1 christos 618 1.1 christos XTRACE("isc_task_purge"); 619 1.1 christos 620 1.1 christos return (isc_task_purgerange(task, sender, type, type, tag)); 621 1.1 christos } 622 1.1 christos 623 1.1 christos bool 624 1.1 christos isc_task_purgeevent(isc_task_t *task, isc_event_t *event) { 625 1.1 christos bool found = false; 626 1.1 christos 627 1.1 christos /* 628 1.1 christos * Purge 'event' from a task's event queue. 629 1.1 christos */ 630 1.1 christos 631 1.1 christos REQUIRE(VALID_TASK(task)); 632 1.1 christos 633 1.1 christos /* 634 1.1 christos * If 'event' is on the task's event queue, it will be purged, 635 1.1 christos * unless it is marked as unpurgeable. 'event' does not have to be 636 1.1 christos * on the task's event queue; in fact, it can even be an invalid 637 1.1 christos * pointer. Purging only occurs if the event is actually on the task's 638 1.1 christos * event queue. 639 1.1 christos * 640 1.1 christos * Purging never changes the state of the task. 641 1.1 christos */ 642 1.1 christos 643 1.1 christos LOCK(&task->lock); 644 1.1 christos if (ISC_LINK_LINKED(event, ev_link)) { 645 1.1 christos DEQUEUE(task->events, event, ev_link); 646 1.1 christos task->nevents--; 647 1.1 christos found = true; 648 1.1 christos } 649 1.1 christos UNLOCK(&task->lock); 650 1.1 christos 651 1.1 christos if (!found) { 652 1.1 christos return (false); 653 1.1 christos } 654 1.1 christos 655 1.1 christos isc_event_free(&event); 656 1.1 christos 657 1.1 christos return (true); 658 1.1 christos } 659 1.1 christos 660 1.1 christos unsigned int 661 1.1 christos isc_task_unsendrange(isc_task_t *task, void *sender, isc_eventtype_t first, 662 1.1 christos isc_eventtype_t last, void *tag, isc_eventlist_t *events) { 663 1.1 christos /* 664 1.1 christos * Remove events from a task's event queue. 665 1.1 christos */ 666 1.1 christos REQUIRE(VALID_TASK(task)); 667 1.1 christos 668 1.1 christos XTRACE("isc_task_unsendrange"); 669 1.1 christos 670 1.1 christos return (dequeue_events(task, sender, first, last, tag, events, false)); 671 1.1 christos } 672 1.1 christos 673 1.1 christos unsigned int 674 1.1 christos isc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type, void *tag, 675 1.1 christos isc_eventlist_t *events) { 676 1.1 christos /* 677 1.1 christos * Remove events from a task's event queue. 678 1.1 christos */ 679 1.1 christos 680 1.1 christos XTRACE("isc_task_unsend"); 681 1.1 christos 682 1.1 christos return (dequeue_events(task, sender, type, type, tag, events, false)); 683 1.1 christos } 684 1.1 christos 685 1.1 christos isc_result_t 686 1.1 christos isc_task_onshutdown(isc_task_t *task, isc_taskaction_t action, void *arg) { 687 1.1 christos bool disallowed = false; 688 1.1 christos isc_result_t result = ISC_R_SUCCESS; 689 1.1 christos isc_event_t *event; 690 1.1 christos 691 1.1 christos /* 692 1.1 christos * Send a shutdown event with action 'action' and argument 'arg' when 693 1.1 christos * 'task' is shutdown. 694 1.1 christos */ 695 1.1 christos 696 1.1 christos REQUIRE(VALID_TASK(task)); 697 1.1 christos REQUIRE(action != NULL); 698 1.1 christos 699 1.1 christos event = isc_event_allocate(task->manager->mctx, NULL, 700 1.1 christos ISC_TASKEVENT_SHUTDOWN, action, arg, 701 1.1 christos sizeof(*event)); 702 1.1 christos 703 1.1 christos if (TASK_SHUTTINGDOWN(task)) { 704 1.1 christos disallowed = true; 705 1.1 christos result = ISC_R_SHUTTINGDOWN; 706 1.1 christos } else { 707 1.1 christos LOCK(&task->lock); 708 1.1 christos ENQUEUE(task->on_shutdown, event, ev_link); 709 1.1 christos UNLOCK(&task->lock); 710 1.1 christos } 711 1.1 christos 712 1.1 christos if (disallowed) { 713 1.1 christos isc_mem_put(task->manager->mctx, event, sizeof(*event)); 714 1.1 christos } 715 1.1 christos 716 1.1 christos return (result); 717 1.1 christos } 718 1.1 christos 719 1.1 christos void 720 1.1 christos isc_task_shutdown(isc_task_t *task) { 721 1.1 christos bool was_idle; 722 1.1 christos 723 1.1 christos /* 724 1.1 christos * Shutdown 'task'. 725 1.1 christos */ 726 1.1 christos 727 1.1 christos REQUIRE(VALID_TASK(task)); 728 1.1 christos 729 1.1 christos LOCK(&task->lock); 730 1.1 christos was_idle = task_shutdown(task); 731 1.1 christos UNLOCK(&task->lock); 732 1.1 christos 733 1.1 christos if (was_idle) { 734 1.1 christos task_ready(task); 735 1.1 christos } 736 1.1 christos } 737 1.1 christos 738 1.1 christos void 739 1.1 christos isc_task_destroy(isc_task_t **taskp) { 740 1.1 christos /* 741 1.1 christos * Destroy '*taskp'. 742 1.1 christos */ 743 1.1 christos 744 1.1 christos REQUIRE(taskp != NULL); 745 1.1 christos 746 1.1 christos isc_task_shutdown(*taskp); 747 1.1 christos isc_task_detach(taskp); 748 1.1 christos } 749 1.1 christos 750 1.1 christos void 751 1.1 christos isc_task_setname(isc_task_t *task, const char *name, void *tag) { 752 1.1 christos /* 753 1.1 christos * Name 'task'. 754 1.1 christos */ 755 1.1 christos 756 1.1 christos REQUIRE(VALID_TASK(task)); 757 1.1 christos 758 1.1 christos LOCK(&task->lock); 759 1.1 christos strlcpy(task->name, name, sizeof(task->name)); 760 1.1 christos task->tag = tag; 761 1.1 christos UNLOCK(&task->lock); 762 1.1 christos } 763 1.1 christos 764 1.1 christos const char * 765 1.1 christos isc_task_getname(isc_task_t *task) { 766 1.1 christos REQUIRE(VALID_TASK(task)); 767 1.1 christos 768 1.1 christos return (task->name); 769 1.1 christos } 770 1.1 christos 771 1.1 christos void * 772 1.1 christos isc_task_gettag(isc_task_t *task) { 773 1.1 christos REQUIRE(VALID_TASK(task)); 774 1.1 christos 775 1.1 christos return (task->tag); 776 1.1 christos } 777 1.1 christos 778 1.1 christos void 779 1.1 christos isc_task_getcurrenttime(isc_task_t *task, isc_stdtime_t *t) { 780 1.1 christos REQUIRE(VALID_TASK(task)); 781 1.1 christos REQUIRE(t != NULL); 782 1.1 christos 783 1.1 christos LOCK(&task->lock); 784 1.1 christos *t = task->now; 785 1.1 christos UNLOCK(&task->lock); 786 1.1 christos } 787 1.1 christos 788 1.1 christos void 789 1.1 christos isc_task_getcurrenttimex(isc_task_t *task, isc_time_t *t) { 790 1.1 christos REQUIRE(VALID_TASK(task)); 791 1.1 christos REQUIRE(t != NULL); 792 1.1 christos 793 1.1 christos LOCK(&task->lock); 794 1.1 christos *t = task->tnow; 795 1.1 christos UNLOCK(&task->lock); 796 1.1 christos } 797 1.1 christos 798 1.1 christos isc_nm_t * 799 1.1 christos isc_task_getnetmgr(isc_task_t *task) { 800 1.1 christos REQUIRE(VALID_TASK(task)); 801 1.1 christos 802 1.1 christos return (task->manager->netmgr); 803 1.1 christos } 804 1.1 christos 805 1.1 christos void 806 1.1 christos isc_task_setquantum(isc_task_t *task, unsigned int quantum) { 807 1.1 christos REQUIRE(VALID_TASK(task)); 808 1.1 christos 809 1.1 christos LOCK(&task->lock); 810 1.1 christos task->quantum = (quantum > 0) ? quantum 811 1.1 christos : task->manager->default_quantum; 812 1.1 christos UNLOCK(&task->lock); 813 1.1 christos } 814 1.1 christos 815 1.1 christos /*** 816 1.1 christos *** Task Manager. 817 1.1 christos ***/ 818 1.1 christos 819 1.1 christos static isc_result_t 820 1.1 christos task_run(isc_task_t *task) { 821 1.1 christos unsigned int dispatch_count = 0; 822 1.1 christos bool finished = false; 823 1.1 christos isc_event_t *event = NULL; 824 1.1 christos isc_result_t result = ISC_R_SUCCESS; 825 1.1 christos uint32_t quantum; 826 1.1 christos 827 1.1 christos REQUIRE(VALID_TASK(task)); 828 1.1 christos 829 1.1 christos LOCK(&task->lock); 830 1.1 christos quantum = task->quantum; 831 1.1 christos 832 1.1 christos /* 833 1.1 christos * It is possible because that we have a paused task in the queue - it 834 1.1 christos * might have been paused in the meantime and we never hold both queue 835 1.1 christos * and task lock to avoid deadlocks, just bail then. 836 1.1 christos */ 837 1.1 christos if (task->state != task_state_ready) { 838 1.1 christos goto done; 839 1.1 christos } 840 1.1 christos 841 1.1 christos INSIST(task->state == task_state_ready); 842 1.1 christos task->state = task_state_running; 843 1.1 christos XTRACE("running"); 844 1.1 christos XTRACE(task->name); 845 1.1 christos TIME_NOW(&task->tnow); 846 1.1 christos task->now = isc_time_seconds(&task->tnow); 847 1.1 christos 848 1.1 christos while (true) { 849 1.1 christos if (!EMPTY(task->events)) { 850 1.1 christos event = HEAD(task->events); 851 1.1 christos DEQUEUE(task->events, event, ev_link); 852 1.1 christos task->nevents--; 853 1.1 christos 854 1.1 christos /* 855 1.1 christos * Execute the event action. 856 1.1 christos */ 857 1.1 christos XTRACE("execute action"); 858 1.1 christos XTRACE(task->name); 859 1.1 christos if (event->ev_action != NULL) { 860 1.1 christos UNLOCK(&task->lock); 861 1.1 christos (event->ev_action)(task, event); 862 1.1 christos LOCK(&task->lock); 863 1.1 christos } 864 1.1 christos XTRACE("execution complete"); 865 1.1 christos dispatch_count++; 866 1.1 christos } 867 1.1 christos 868 1.1 christos if (isc_refcount_current(&task->references) == 0 && 869 1.1 christos EMPTY(task->events) && !TASK_SHUTTINGDOWN(task)) 870 1.1 christos { 871 1.1 christos /* 872 1.1 christos * There are no references and no pending events for 873 1.1 christos * this task, which means it will not become runnable 874 1.1 christos * again via an external action (such as sending an 875 1.1 christos * event or detaching). 876 1.1 christos * 877 1.1 christos * We initiate shutdown to prevent it from becoming a 878 1.1 christos * zombie. 879 1.1 christos * 880 1.1 christos * We do this here instead of in the "if 881 1.1 christos * EMPTY(task->events)" block below because: 882 1.1 christos * 883 1.1 christos * If we post no shutdown events, we want the task 884 1.1 christos * to finish. 885 1.1 christos * 886 1.1 christos * If we did post shutdown events, will still want 887 1.1 christos * the task's quantum to be applied. 888 1.1 christos */ 889 1.1 christos INSIST(!task_shutdown(task)); 890 1.1 christos } 891 1.1 christos 892 1.1 christos if (EMPTY(task->events)) { 893 1.1 christos /* 894 1.1 christos * Nothing else to do for this task right now. 895 1.1 christos */ 896 1.1 christos XTRACE("empty"); 897 1.1 christos if (isc_refcount_current(&task->references) == 0 && 898 1.1 christos TASK_SHUTTINGDOWN(task)) 899 1.1 christos { 900 1.1 christos /* 901 1.1 christos * The task is done. 902 1.1 christos */ 903 1.1 christos XTRACE("done"); 904 1.1 christos task->state = task_state_done; 905 1.1 christos } else { 906 1.1 christos if (task->state == task_state_running) { 907 1.1 christos XTRACE("idling"); 908 1.1 christos task->state = task_state_idle; 909 1.1 christos } else if (task->state == task_state_pausing) { 910 1.1 christos XTRACE("pausing"); 911 1.1 christos task->state = task_state_paused; 912 1.1 christos } 913 1.1 christos } 914 1.1 christos break; 915 1.1 christos } else if (task->state == task_state_pausing) { 916 1.1 christos /* 917 1.1 christos * We got a pause request on this task, stop working on 918 1.1 christos * it and switch the state to paused. 919 1.1 christos */ 920 1.1 christos XTRACE("pausing"); 921 1.1 christos task->state = task_state_paused; 922 1.1 christos break; 923 1.1 christos } else if (dispatch_count >= quantum) { 924 1.1 christos /* 925 1.1 christos * Our quantum has expired, but there is more work to be 926 1.1 christos * done. We'll requeue it to the ready queue later. 927 1.1 christos * 928 1.1 christos * We don't check quantum until dispatching at least one 929 1.1 christos * event, so the minimum quantum is one. 930 1.1 christos */ 931 1.1 christos XTRACE("quantum"); 932 1.1 christos task->state = task_state_ready; 933 1.1 christos result = ISC_R_QUOTA; 934 1.1 christos break; 935 1.1 christos } 936 1.1 christos } 937 1.1 christos 938 1.1 christos done: 939 1.1 christos if (isc_refcount_decrement(&task->running) == 1 && 940 1.1 christos task->state == task_state_done) 941 1.1 christos { 942 1.1 christos finished = true; 943 1.1 christos } 944 1.1 christos UNLOCK(&task->lock); 945 1.1 christos 946 1.1 christos if (finished) { 947 1.1 christos task_finished(task); 948 1.1 christos } 949 1.1 christos 950 1.1 christos return (result); 951 1.1 christos } 952 1.1 christos 953 1.1 christos isc_result_t 954 1.1 christos isc_task_run(isc_task_t *task) { 955 1.1 christos return (task_run(task)); 956 1.1 christos } 957 1.1 christos 958 1.1 christos static void 959 1.1 christos manager_free(isc_taskmgr_t *manager) { 960 1.1 christos isc_refcount_destroy(&manager->references); 961 1.1 christos isc_nm_detach(&manager->netmgr); 962 1.1 christos 963 1.1 christos isc_mutex_destroy(&manager->lock); 964 1.1 christos manager->magic = 0; 965 1.1 christos isc_mem_putanddetach(&manager->mctx, manager, sizeof(*manager)); 966 1.1 christos } 967 1.1 christos 968 1.1 christos void 969 1.1 christos isc_taskmgr_attach(isc_taskmgr_t *source, isc_taskmgr_t **targetp) { 970 1.1 christos REQUIRE(VALID_MANAGER(source)); 971 1.1 christos REQUIRE(targetp != NULL && *targetp == NULL); 972 1.1 christos 973 1.1 christos isc_refcount_increment(&source->references); 974 1.1 christos 975 1.1 christos *targetp = source; 976 1.1 christos } 977 1.1 christos 978 1.1 christos void 979 1.1 christos isc_taskmgr_detach(isc_taskmgr_t **managerp) { 980 1.1 christos REQUIRE(managerp != NULL); 981 1.1 christos REQUIRE(VALID_MANAGER(*managerp)); 982 1.1 christos 983 1.1 christos isc_taskmgr_t *manager = *managerp; 984 1.1 christos *managerp = NULL; 985 1.1 christos 986 1.1 christos if (isc_refcount_decrement(&manager->references) == 1) { 987 1.1 christos manager_free(manager); 988 1.1 christos } 989 1.1 christos } 990 1.1 christos 991 1.1 christos isc_result_t 992 1.1 christos isc__taskmgr_create(isc_mem_t *mctx, unsigned int default_quantum, isc_nm_t *nm, 993 1.1 christos isc_taskmgr_t **managerp) { 994 1.1 christos isc_taskmgr_t *manager; 995 1.1 christos 996 1.1 christos /* 997 1.1 christos * Create a new task manager. 998 1.1 christos */ 999 1.1 christos 1000 1.1 christos REQUIRE(managerp != NULL && *managerp == NULL); 1001 1.1 christos REQUIRE(nm != NULL); 1002 1.1 christos 1003 1.1 christos manager = isc_mem_get(mctx, sizeof(*manager)); 1004 1.1 christos *manager = (isc_taskmgr_t){ .magic = TASK_MANAGER_MAGIC }; 1005 1.1 christos 1006 1.1 christos isc_mutex_init(&manager->lock); 1007 1.1 christos 1008 1.1 christos if (default_quantum == 0) { 1009 1.1 christos default_quantum = DEFAULT_DEFAULT_QUANTUM; 1010 1.1 christos } 1011 1.1 christos manager->default_quantum = default_quantum; 1012 1.1 christos 1013 1.1 christos if (nm != NULL) { 1014 1.1 christos isc_nm_attach(nm, &manager->netmgr); 1015 1.1 christos } 1016 1.1 christos 1017 1.1 christos INIT_LIST(manager->tasks); 1018 1.1 christos atomic_init(&manager->mode, isc_taskmgrmode_normal); 1019 1.1 christos atomic_init(&manager->exclusive_req, false); 1020 1.1 christos atomic_init(&manager->tasks_count, 0); 1021 1.1 christos 1022 1.1 christos isc_mem_attach(mctx, &manager->mctx); 1023 1.1 christos 1024 1.1 christos isc_refcount_init(&manager->references, 1); 1025 1.1 christos 1026 1.1 christos *managerp = manager; 1027 1.1 christos 1028 1.1 christos return (ISC_R_SUCCESS); 1029 1.1 christos } 1030 1.1 christos 1031 1.1 christos void 1032 1.1 christos isc__taskmgr_shutdown(isc_taskmgr_t *manager) { 1033 1.1 christos isc_task_t *task; 1034 1.1 christos 1035 1.1 christos REQUIRE(VALID_MANAGER(manager)); 1036 1.1 christos 1037 1.1 christos XTHREADTRACE("isc_taskmgr_shutdown"); 1038 1.1 christos /* 1039 1.1 christos * Only one non-worker thread may ever call this routine. 1040 1.1 christos * If a worker thread wants to initiate shutdown of the 1041 1.1 christos * task manager, it should ask some non-worker thread to call 1042 1.1 christos * isc_taskmgr_destroy(), e.g. by signalling a condition variable 1043 1.1 christos * that the startup thread is sleeping on. 1044 1.1 christos */ 1045 1.1 christos 1046 1.1 christos /* 1047 1.1 christos * Unlike elsewhere, we're going to hold this lock a long time. 1048 1.1 christos * We need to do so, because otherwise the list of tasks could 1049 1.1 christos * change while we were traversing it. 1050 1.1 christos * 1051 1.1 christos * This is also the only function where we will hold both the 1052 1.1 christos * task manager lock and a task lock at the same time. 1053 1.1 christos */ 1054 1.1 christos LOCK(&manager->lock); 1055 1.1 christos if (manager->excl != NULL) { 1056 1.1 christos isc_task_detach((isc_task_t **)&manager->excl); 1057 1.1 christos } 1058 1.1 christos 1059 1.1 christos /* 1060 1.1 christos * Make sure we only get called once. 1061 1.1 christos */ 1062 1.1 christos INSIST(manager->exiting == false); 1063 1.1 christos manager->exiting = true; 1064 1.1 christos 1065 1.1 christos /* 1066 1.1 christos * Post shutdown event(s) to every task (if they haven't already been 1067 1.1 christos * posted). 1068 1.1 christos */ 1069 1.1 christos for (task = HEAD(manager->tasks); task != NULL; task = NEXT(task, link)) 1070 1.1 christos { 1071 1.1 christos bool was_idle; 1072 1.1 christos 1073 1.1 christos LOCK(&task->lock); 1074 1.1 christos was_idle = task_shutdown(task); 1075 1.1 christos if (was_idle) { 1076 1.1 christos task->threadid = 0; 1077 1.1 christos } 1078 1.1 christos UNLOCK(&task->lock); 1079 1.1 christos 1080 1.1 christos if (was_idle) { 1081 1.1 christos task_ready(task); 1082 1.1 christos } 1083 1.1 christos } 1084 1.1 christos 1085 1.1 christos UNLOCK(&manager->lock); 1086 1.1 christos } 1087 1.1 christos 1088 1.1 christos void 1089 1.1 christos isc__taskmgr_destroy(isc_taskmgr_t **managerp) { 1090 1.1 christos REQUIRE(managerp != NULL && VALID_MANAGER(*managerp)); 1091 1.1 christos XTHREADTRACE("isc_taskmgr_destroy"); 1092 1.1 christos 1093 1.1 christos #ifdef ISC_TASK_TRACE 1094 1.1 christos int counter = 0; 1095 1.1 christos while (isc_refcount_current(&(*managerp)->references) > 1 && 1096 1.1 christos counter++ < 1000) 1097 1.1 christos { 1098 1.1 christos usleep(10 * 1000); 1099 1.1 christos } 1100 1.1 christos INSIST(counter < 1000); 1101 1.1 christos #else 1102 1.1 christos while (isc_refcount_current(&(*managerp)->references) > 1) { 1103 1.1 christos usleep(10 * 1000); 1104 1.1 christos } 1105 1.1 christos #endif 1106 1.1 christos 1107 1.1 christos isc_taskmgr_detach(managerp); 1108 1.1 christos } 1109 1.1 christos 1110 1.1 christos void 1111 1.1 christos isc_taskmgr_setexcltask(isc_taskmgr_t *mgr, isc_task_t *task) { 1112 1.1 christos REQUIRE(VALID_MANAGER(mgr)); 1113 1.1 christos REQUIRE(VALID_TASK(task)); 1114 1.1 christos 1115 1.1 christos LOCK(&task->lock); 1116 1.1 christos REQUIRE(task->threadid == 0); 1117 1.1 christos UNLOCK(&task->lock); 1118 1.1 christos 1119 1.1 christos LOCK(&mgr->lock); 1120 1.1 christos if (mgr->excl != NULL) { 1121 1.1 christos isc_task_detach(&mgr->excl); 1122 1.1 christos } 1123 1.1 christos isc_task_attach(task, &mgr->excl); 1124 1.1 christos UNLOCK(&mgr->lock); 1125 1.1 christos } 1126 1.1 christos 1127 1.1 christos isc_result_t 1128 1.1 christos isc_taskmgr_excltask(isc_taskmgr_t *mgr, isc_task_t **taskp) { 1129 1.1 christos isc_result_t result; 1130 1.1 christos 1131 1.1 christos REQUIRE(VALID_MANAGER(mgr)); 1132 1.1 christos REQUIRE(taskp != NULL && *taskp == NULL); 1133 1.1 christos 1134 1.1 christos LOCK(&mgr->lock); 1135 1.1 christos if (mgr->excl != NULL) { 1136 1.1 christos isc_task_attach(mgr->excl, taskp); 1137 1.1 christos result = ISC_R_SUCCESS; 1138 1.1 christos } else if (mgr->exiting) { 1139 1.1 christos result = ISC_R_SHUTTINGDOWN; 1140 1.1 christos } else { 1141 1.1 christos result = ISC_R_NOTFOUND; 1142 1.1 christos } 1143 1.1 christos UNLOCK(&mgr->lock); 1144 1.1 christos 1145 1.1 christos return (result); 1146 1.1 christos } 1147 1.1 christos 1148 1.1 christos isc_result_t 1149 1.1 christos isc_task_beginexclusive(isc_task_t *task) { 1150 1.1 christos isc_taskmgr_t *manager; 1151 1.1 christos 1152 1.1 christos REQUIRE(VALID_TASK(task)); 1153 1.1 christos 1154 1.1 christos manager = task->manager; 1155 1.1 christos 1156 1.1 christos REQUIRE(task->state == task_state_running); 1157 1.1 christos 1158 1.1 christos LOCK(&manager->lock); 1159 1.1 christos REQUIRE(task == manager->excl || 1160 1.1 christos (manager->exiting && manager->excl == NULL)); 1161 1.1 christos UNLOCK(&manager->lock); 1162 1.1 christos 1163 1.1 christos if (!atomic_compare_exchange_strong(&manager->exclusive_req, 1164 1.1 christos &(bool){ false }, true)) 1165 1.1 christos { 1166 1.1 christos return (ISC_R_LOCKBUSY); 1167 1.1 christos } 1168 1.1 christos 1169 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) { 1170 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 1171 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1), 1172 1.1 christos "exclusive task mode: %s", "starting"); 1173 1.1 christos } 1174 1.1 christos 1175 1.1 christos isc_nm_pause(manager->netmgr); 1176 1.1 christos 1177 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) { 1178 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 1179 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1), 1180 1.1 christos "exclusive task mode: %s", "started"); 1181 1.1 christos } 1182 1.1 christos 1183 1.1 christos return (ISC_R_SUCCESS); 1184 1.1 christos } 1185 1.1 christos 1186 1.1 christos void 1187 1.1 christos isc_task_endexclusive(isc_task_t *task) { 1188 1.1 christos isc_taskmgr_t *manager; 1189 1.1 christos 1190 1.1 christos REQUIRE(VALID_TASK(task)); 1191 1.1 christos REQUIRE(task->state == task_state_running); 1192 1.1 christos manager = task->manager; 1193 1.1 christos 1194 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) { 1195 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 1196 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1), 1197 1.1 christos "exclusive task mode: %s", "ending"); 1198 1.1 christos } 1199 1.1 christos 1200 1.1 christos isc_nm_resume(manager->netmgr); 1201 1.1 christos 1202 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) { 1203 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, 1204 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1), 1205 1.1 christos "exclusive task mode: %s", "ended"); 1206 1.1 christos } 1207 1.1 christos 1208 1.1 christos REQUIRE(atomic_compare_exchange_strong(&manager->exclusive_req, 1209 1.1 christos &(bool){ true }, false)); 1210 1.1 christos } 1211 1.1 christos 1212 1.1 christos void 1213 1.1 christos isc_task_pause(isc_task_t *task) { 1214 1.1 christos REQUIRE(VALID_TASK(task)); 1215 1.1 christos 1216 1.1 christos LOCK(&task->lock); 1217 1.1 christos task->pause_cnt++; 1218 1.1 christos if (task->pause_cnt > 1) { 1219 1.1 christos /* 1220 1.1 christos * Someone already paused this task, just increase 1221 1.1 christos * the number of pausing clients. 1222 1.1 christos */ 1223 1.1 christos UNLOCK(&task->lock); 1224 1.1 christos return; 1225 1.1 christos } 1226 1.1 christos 1227 1.1 christos INSIST(task->state == task_state_idle || 1228 1.1 christos task->state == task_state_ready || 1229 1.1 christos task->state == task_state_running); 1230 1.1 christos if (task->state == task_state_running) { 1231 1.1 christos task->state = task_state_pausing; 1232 1.1 christos } else { 1233 1.1 christos task->state = task_state_paused; 1234 1.1 christos } 1235 1.1 christos UNLOCK(&task->lock); 1236 1.1 christos } 1237 1.1 christos 1238 1.1 christos void 1239 1.1 christos isc_task_unpause(isc_task_t *task) { 1240 1.1 christos bool was_idle = false; 1241 1.1 christos 1242 1.1 christos REQUIRE(VALID_TASK(task)); 1243 1.1 christos 1244 1.1 christos LOCK(&task->lock); 1245 1.1 christos task->pause_cnt--; 1246 1.1 christos INSIST(task->pause_cnt >= 0); 1247 1.1 christos if (task->pause_cnt > 0) { 1248 1.1 christos UNLOCK(&task->lock); 1249 1.1 christos return; 1250 1.1 christos } 1251 1.1 christos 1252 1.1 christos INSIST(task->state == task_state_paused || 1253 1.1 christos task->state == task_state_pausing); 1254 1.1 christos /* If the task was pausing we can't reschedule it */ 1255 1.1 christos if (task->state == task_state_pausing) { 1256 1.1 christos task->state = task_state_running; 1257 1.1 christos } else { 1258 1.1 christos task->state = task_state_idle; 1259 1.1 christos } 1260 1.1 christos if (task->state == task_state_idle && !EMPTY(task->events)) { 1261 1.1 christos task->state = task_state_ready; 1262 1.1 christos was_idle = true; 1263 1.1 christos } 1264 1.1 christos UNLOCK(&task->lock); 1265 1.1 christos 1266 1.1 christos if (was_idle) { 1267 1.1 christos task_ready(task); 1268 1.1 christos } 1269 1.1 christos } 1270 1.1 christos 1271 1.1 christos void 1272 1.1 christos isc_taskmgr_setmode(isc_taskmgr_t *manager, isc_taskmgrmode_t mode) { 1273 1.1 christos atomic_store(&manager->mode, mode); 1274 1.1 christos } 1275 1.1 christos 1276 1.1 christos isc_taskmgrmode_t 1277 1.1 christos isc_taskmgr_mode(isc_taskmgr_t *manager) { 1278 1.1 christos return (atomic_load(&manager->mode)); 1279 1.1 christos } 1280 1.1 christos 1281 1.1 christos void 1282 1.1 christos isc_task_setprivilege(isc_task_t *task, bool priv) { 1283 1.1 christos REQUIRE(VALID_TASK(task)); 1284 1.1 christos 1285 1.1 christos atomic_store_release(&task->privileged, priv); 1286 1.1 christos } 1287 1.1 christos 1288 1.1 christos bool 1289 1.1 christos isc_task_getprivilege(isc_task_t *task) { 1290 1.1 christos REQUIRE(VALID_TASK(task)); 1291 1.1 christos 1292 1.1 christos return (TASK_PRIVILEGED(task)); 1293 1.1 christos } 1294 1.1 christos 1295 1.1 christos bool 1296 1.1 christos isc_task_privileged(isc_task_t *task) { 1297 1.1 christos REQUIRE(VALID_TASK(task)); 1298 1.1 christos 1299 1.1 christos return (isc_taskmgr_mode(task->manager) && TASK_PRIVILEGED(task)); 1300 1.1 christos } 1301 1.1 christos 1302 1.1 christos bool 1303 1.1 christos isc_task_exiting(isc_task_t *task) { 1304 1.1 christos REQUIRE(VALID_TASK(task)); 1305 1.1 christos 1306 1.1 christos return (TASK_SHUTTINGDOWN(task)); 1307 1.1 christos } 1308 1.1 christos 1309 1.1 christos #ifdef HAVE_LIBXML2 1310 1.1 christos #define TRY0(a) \ 1311 1.1 christos do { \ 1312 1.1 christos xmlrc = (a); \ 1313 1.1 christos if (xmlrc < 0) \ 1314 1.1 christos goto error; \ 1315 1.1 christos } while (0) 1316 1.1 christos int 1317 1.1 christos isc_taskmgr_renderxml(isc_taskmgr_t *mgr, void *writer0) { 1318 1.1 christos isc_task_t *task = NULL; 1319 1.1 christos int xmlrc; 1320 1.1 christos xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0; 1321 1.1 christos 1322 1.1 christos LOCK(&mgr->lock); 1323 1.1 christos 1324 1.1 christos /* 1325 1.1 christos * Write out the thread-model, and some details about each depending 1326 1.1 christos * on which type is enabled. 1327 1.1 christos */ 1328 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "thread-model")); 1329 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "type")); 1330 1.1 christos TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR "threaded")); 1331 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* type */ 1332 1.1 christos 1333 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "default-quantum")); 1334 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%d", 1335 1.1 christos mgr->default_quantum)); 1336 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* default-quantum */ 1337 1.1 christos 1338 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* thread-model */ 1339 1.1 christos 1340 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "tasks")); 1341 1.1 christos task = ISC_LIST_HEAD(mgr->tasks); 1342 1.1 christos while (task != NULL) { 1343 1.1 christos LOCK(&task->lock); 1344 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "task")); 1345 1.1 christos 1346 1.1 christos if (task->name[0] != 0) { 1347 1.1 christos TRY0(xmlTextWriterStartElement(writer, 1348 1.1 christos ISC_XMLCHAR "name")); 1349 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%s", 1350 1.1 christos task->name)); 1351 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* name */ 1352 1.1 christos } 1353 1.1 christos 1354 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "reference" 1355 1.1 christos "s")); 1356 1.1 christos TRY0(xmlTextWriterWriteFormatString( 1357 1.1 christos writer, "%" PRIuFAST32, 1358 1.1 christos isc_refcount_current(&task->references))); 1359 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* references */ 1360 1.1 christos 1361 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id")); 1362 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%p", task)); 1363 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* id */ 1364 1.1 christos 1365 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "state")); 1366 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%s", 1367 1.1 christos statenames[task->state])); 1368 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* state */ 1369 1.1 christos 1370 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "quantum")); 1371 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%d", 1372 1.1 christos task->quantum)); 1373 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* quantum */ 1374 1.1 christos 1375 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "events")); 1376 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%d", 1377 1.1 christos task->nevents)); 1378 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* events */ 1379 1.1 christos 1380 1.1 christos TRY0(xmlTextWriterEndElement(writer)); 1381 1.1 christos 1382 1.1 christos UNLOCK(&task->lock); 1383 1.1 christos task = ISC_LIST_NEXT(task, link); 1384 1.1 christos } 1385 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* tasks */ 1386 1.1 christos 1387 1.1 christos error: 1388 1.1 christos if (task != NULL) { 1389 1.1 christos UNLOCK(&task->lock); 1390 1.1 christos } 1391 1.1 christos UNLOCK(&mgr->lock); 1392 1.1 christos 1393 1.1 christos return (xmlrc); 1394 1.1 christos } 1395 1.1 christos #endif /* HAVE_LIBXML2 */ 1396 1.1 christos 1397 1.1 christos #ifdef HAVE_JSON_C 1398 1.1 christos #define CHECKMEM(m) \ 1399 1.1 christos do { \ 1400 1.1 christos if (m == NULL) { \ 1401 1.1 christos result = ISC_R_NOMEMORY; \ 1402 1.1 christos goto error; \ 1403 1.1 christos } \ 1404 1.1 christos } while (0) 1405 1.1 christos 1406 1.1 christos isc_result_t 1407 1.1 christos isc_taskmgr_renderjson(isc_taskmgr_t *mgr, void *tasks0) { 1408 1.1 christos isc_result_t result = ISC_R_SUCCESS; 1409 1.1 christos isc_task_t *task = NULL; 1410 1.1 christos json_object *obj = NULL, *array = NULL, *taskobj = NULL; 1411 1.1 christos json_object *tasks = (json_object *)tasks0; 1412 1.1 christos 1413 1.1 christos LOCK(&mgr->lock); 1414 1.1 christos 1415 1.1 christos /* 1416 1.1 christos * Write out the thread-model, and some details about each depending 1417 1.1 christos * on which type is enabled. 1418 1.1 christos */ 1419 1.1 christos obj = json_object_new_string("threaded"); 1420 1.1 christos CHECKMEM(obj); 1421 1.1 christos json_object_object_add(tasks, "thread-model", obj); 1422 1.1 christos 1423 1.1 christos obj = json_object_new_int(mgr->default_quantum); 1424 1.1 christos CHECKMEM(obj); 1425 1.1 christos json_object_object_add(tasks, "default-quantum", obj); 1426 1.1 christos 1427 1.1 christos array = json_object_new_array(); 1428 1.1 christos CHECKMEM(array); 1429 1.1 christos 1430 1.1 christos for (task = ISC_LIST_HEAD(mgr->tasks); task != NULL; 1431 1.1 christos task = ISC_LIST_NEXT(task, link)) 1432 1.1 christos { 1433 1.1 christos char buf[255]; 1434 1.1 christos 1435 1.1 christos LOCK(&task->lock); 1436 1.1 christos 1437 1.1 christos taskobj = json_object_new_object(); 1438 1.1 christos CHECKMEM(taskobj); 1439 1.1 christos json_object_array_add(array, taskobj); 1440 1.1 christos 1441 1.1 christos snprintf(buf, sizeof(buf), "%p", task); 1442 1.1 christos obj = json_object_new_string(buf); 1443 1.1 christos CHECKMEM(obj); 1444 1.1 christos json_object_object_add(taskobj, "id", obj); 1445 1.1 christos 1446 1.1 christos if (task->name[0] != 0) { 1447 1.1 christos obj = json_object_new_string(task->name); 1448 1.1 christos CHECKMEM(obj); 1449 1.1 christos json_object_object_add(taskobj, "name", obj); 1450 1.1 christos } 1451 1.1 christos 1452 1.1 christos obj = json_object_new_int( 1453 1.1 christos isc_refcount_current(&task->references)); 1454 1.1 christos CHECKMEM(obj); 1455 1.1 christos json_object_object_add(taskobj, "references", obj); 1456 1.1 christos 1457 1.1 christos obj = json_object_new_string(statenames[task->state]); 1458 1.1 christos CHECKMEM(obj); 1459 1.1 christos json_object_object_add(taskobj, "state", obj); 1460 1.1 christos 1461 1.1 christos obj = json_object_new_int(task->quantum); 1462 1.1 christos CHECKMEM(obj); 1463 1.1 christos json_object_object_add(taskobj, "quantum", obj); 1464 1.1 christos 1465 1.1 christos obj = json_object_new_int(task->nevents); 1466 1.1 christos CHECKMEM(obj); 1467 1.1 christos json_object_object_add(taskobj, "events", obj); 1468 1.1 christos 1469 1.1 christos UNLOCK(&task->lock); 1470 1.1 christos } 1471 1.1 christos 1472 1.1 christos json_object_object_add(tasks, "tasks", array); 1473 1.1 christos array = NULL; 1474 1.1 christos result = ISC_R_SUCCESS; 1475 1.1 christos 1476 1.1 christos error: 1477 1.1 christos if (array != NULL) { 1478 1.1 christos json_object_put(array); 1479 1.1 christos } 1480 1.1 christos 1481 1.1 christos if (task != NULL) { 1482 1.1 christos UNLOCK(&task->lock); 1483 1.1 christos } 1484 1.1 christos UNLOCK(&mgr->lock); 1485 1.1 christos 1486 1.1 christos return (result); 1487 1.1 christos } 1488 1.1 christos #endif /* ifdef HAVE_JSON_C */ 1489