Loading...
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 | /* * Copyright (c) 2016 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <stddef.h> #include <kern/debug.h> #include <kern/locks.h> #include <kern/thread.h> #include <kern/thread_call.h> #include <net/nwk_wq.h> #include <sys/proc_internal.h> #include <sys/systm.h> #include <sys/mcache.h> MALLOC_DEFINE(M_NWKWQ, "nwkwq", "Network work-queue"); static TAILQ_HEAD(, nwk_wq_entry) nwk_wq_head; decl_lck_mtx_data(static, nwk_wq_lock); /* Lock group and attributes */ static lck_grp_attr_t *nwk_wq_lock_grp_attributes = NULL; static lck_grp_t *nwk_wq_lock_group = NULL; /* Lock and lock attributes */ static lck_attr_t *nwk_wq_lock_attributes = NULL; decl_lck_mtx_data(static, nwk_wq_lock); /* Wait channel for Network work queue */ static void *nwk_wq_waitch = NULL; static void nwk_wq_thread_func(void *, wait_result_t); static int nwk_wq_thread_cont(int err); static void nwk_wq_thread_func(void *v, wait_result_t w); void nwk_wq_init(void) { thread_t nwk_wq_thread = THREAD_NULL; TAILQ_INIT(&nwk_wq_head); nwk_wq_lock_grp_attributes = lck_grp_attr_alloc_init(); nwk_wq_lock_group = lck_grp_alloc_init("Network work queue lock", nwk_wq_lock_grp_attributes); nwk_wq_lock_attributes = lck_attr_alloc_init(); lck_mtx_init(&nwk_wq_lock, nwk_wq_lock_group, nwk_wq_lock_attributes); if (kernel_thread_start(nwk_wq_thread_func, NULL, &nwk_wq_thread) != KERN_SUCCESS) { panic_plain("%s: couldn't create network work queue thread", __func__); /* NOTREACHED */ } thread_deallocate(nwk_wq_thread); } static int nwk_wq_thread_cont(int err) { TAILQ_HEAD(, nwk_wq_entry) temp_nwk_wq_head; struct nwk_wq_entry *nwk_item; struct nwk_wq_entry *nwk_item_next; #pragma unused(err) for (;;) { nwk_item = NULL; nwk_item_next = NULL; TAILQ_INIT(&temp_nwk_wq_head); LCK_MTX_ASSERT(&nwk_wq_lock, LCK_MTX_ASSERT_OWNED); while (TAILQ_FIRST(&nwk_wq_head) == NULL) { (void) msleep0(&nwk_wq_waitch, &nwk_wq_lock, (PZERO - 1), "nwk_wq_thread_cont", 0, nwk_wq_thread_cont); /* NOTREACHED */ } TAILQ_SWAP(&temp_nwk_wq_head, &nwk_wq_head, nwk_wq_entry, nwk_wq_link); VERIFY(TAILQ_EMPTY(&nwk_wq_head)); lck_mtx_unlock(&nwk_wq_lock); VERIFY(TAILQ_FIRST(&temp_nwk_wq_head) != NULL); TAILQ_FOREACH_SAFE(nwk_item, &temp_nwk_wq_head, nwk_wq_link, nwk_item_next) { nwk_item->func(nwk_item->arg); if (nwk_item->is_arg_managed == FALSE) { FREE(nwk_item->arg, M_NWKWQ); } FREE(nwk_item, M_NWKWQ); } lck_mtx_lock(&nwk_wq_lock); } } __dead2 static void nwk_wq_thread_func(void *v, wait_result_t w) { #pragma unused(v, w) lck_mtx_lock(&nwk_wq_lock); (void) msleep0(&nwk_wq_waitch, &nwk_wq_lock, (PZERO - 1), "nwk_wq_thread_func", 0, nwk_wq_thread_cont); /* * msleep0() shouldn't have returned as PCATCH was not set; * therefore assert in this case. */ lck_mtx_unlock(&nwk_wq_lock); VERIFY(0); } void nwk_wq_enqueue(struct nwk_wq_entry *nwk_item) { lck_mtx_lock(&nwk_wq_lock); TAILQ_INSERT_TAIL(&nwk_wq_head, nwk_item, nwk_wq_link); lck_mtx_unlock(&nwk_wq_lock); wakeup((caddr_t)&nwk_wq_waitch); } |