xref: /xnu-8019.80.24/bsd/net/nwk_wq.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <stddef.h>
29 #include <kern/debug.h>
30 #include <kern/locks.h>
31 #include <kern/thread.h>
32 #include <kern/thread_call.h>
33 #include <net/nwk_wq.h>
34 #include <sys/proc_internal.h>
35 #include <sys/systm.h>
36 #include <sys/mcache.h>
37 
38 MALLOC_DEFINE(M_NWKWQ, "nwkwq", "Network work-queue");
39 
40 static TAILQ_HEAD(, nwk_wq_entry) nwk_wq_head =
41     TAILQ_HEAD_INITIALIZER(nwk_wq_head);
42 static LCK_GRP_DECLARE(nwk_wq_lock_group, "Network work queue lock");
43 static LCK_MTX_DECLARE(nwk_wq_lock, &nwk_wq_lock_group);
44 
45 /* Wait channel for Network work queue */
46 static void *nwk_wq_waitch = NULL;
47 static void nwk_wq_thread_func(void *, wait_result_t);
48 
49 static int nwk_wq_thread_cont(int err);
50 static void nwk_wq_thread_func(void *v, wait_result_t w);
51 
52 void
nwk_wq_init(void)53 nwk_wq_init(void)
54 {
55 	thread_t nwk_wq_thread = THREAD_NULL;
56 
57 	if (kernel_thread_start(nwk_wq_thread_func,
58 	    NULL, &nwk_wq_thread) != KERN_SUCCESS) {
59 		panic_plain("%s: couldn't create network work queue thread", __func__);
60 		/* NOTREACHED */
61 	}
62 	thread_deallocate(nwk_wq_thread);
63 }
64 
65 static int
nwk_wq_thread_cont(int err)66 nwk_wq_thread_cont(int err)
67 {
68 	TAILQ_HEAD(, nwk_wq_entry) temp_nwk_wq_head;
69 	struct nwk_wq_entry *nwk_item;
70 	struct nwk_wq_entry *nwk_item_next;
71 
72 #pragma unused(err)
73 	for (;;) {
74 		nwk_item = NULL;
75 		nwk_item_next = NULL;
76 		TAILQ_INIT(&temp_nwk_wq_head);
77 
78 		LCK_MTX_ASSERT(&nwk_wq_lock, LCK_MTX_ASSERT_OWNED);
79 		while (TAILQ_FIRST(&nwk_wq_head) == NULL) {
80 			(void) msleep0(&nwk_wq_waitch, &nwk_wq_lock,
81 			    (PZERO - 1), "nwk_wq_thread_cont", 0,
82 			    nwk_wq_thread_cont);
83 			/* NOTREACHED */
84 		}
85 
86 		TAILQ_SWAP(&temp_nwk_wq_head, &nwk_wq_head, nwk_wq_entry, nwk_wq_link);
87 		VERIFY(TAILQ_EMPTY(&nwk_wq_head));
88 		lck_mtx_unlock(&nwk_wq_lock);
89 
90 		VERIFY(TAILQ_FIRST(&temp_nwk_wq_head) != NULL);
91 		TAILQ_FOREACH_SAFE(nwk_item, &temp_nwk_wq_head, nwk_wq_link, nwk_item_next) {
92 			nwk_item->func(nwk_item->arg);
93 			if (nwk_item->is_arg_managed == FALSE) {
94 				FREE(nwk_item->arg, M_NWKWQ);
95 			}
96 			FREE(nwk_item, M_NWKWQ);
97 		}
98 		lck_mtx_lock(&nwk_wq_lock);
99 	}
100 }
101 
102 __dead2
103 static void
nwk_wq_thread_func(void * v,wait_result_t w)104 nwk_wq_thread_func(void *v, wait_result_t w)
105 {
106 #pragma unused(v, w)
107 	lck_mtx_lock(&nwk_wq_lock);
108 	(void) msleep0(&nwk_wq_waitch, &nwk_wq_lock,
109 	    (PZERO - 1), "nwk_wq_thread_func", 0, nwk_wq_thread_cont);
110 	/*
111 	 * msleep0() shouldn't have returned as PCATCH was not set;
112 	 * therefore assert in this case.
113 	 */
114 	lck_mtx_unlock(&nwk_wq_lock);
115 	VERIFY(0);
116 }
117 
118 void
nwk_wq_enqueue(struct nwk_wq_entry * nwk_item)119 nwk_wq_enqueue(struct nwk_wq_entry *nwk_item)
120 {
121 	lck_mtx_lock(&nwk_wq_lock);
122 	TAILQ_INSERT_TAIL(&nwk_wq_head, nwk_item, nwk_wq_link);
123 	lck_mtx_unlock(&nwk_wq_lock);
124 	wakeup((caddr_t)&nwk_wq_waitch);
125 }
126