xref: /netbsd-src/sys/kern/subr_lwp_specificdata.c (revision bb14ae406024c4ba2ebb3804a85ae1032f1d3bcf)
1*bb14ae40Sandvar /*	$NetBSD: subr_lwp_specificdata.c,v 1.5 2024/09/01 19:56:18 andvar Exp $	*/
2290fe400Spooka 
3290fe400Spooka /*-
4290fe400Spooka  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5290fe400Spooka  * All rights reserved.
6290fe400Spooka  *
7290fe400Spooka  * Redistribution and use in source and binary forms, with or without
8290fe400Spooka  * modification, are permitted provided that the following conditions
9290fe400Spooka  * are met:
10290fe400Spooka  * 1. Redistributions of source code must retain the above copyright
11290fe400Spooka  *    notice, this list of conditions and the following disclaimer.
12290fe400Spooka  * 2. Redistributions in binary form must reproduce the above copyright
13290fe400Spooka  *    notice, this list of conditions and the following disclaimer in the
14290fe400Spooka  *    documentation and/or other materials provided with the distribution.
15290fe400Spooka  *
16290fe400Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17290fe400Spooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18290fe400Spooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19290fe400Spooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20290fe400Spooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21290fe400Spooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22290fe400Spooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23290fe400Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24290fe400Spooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25290fe400Spooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26290fe400Spooka  * POSSIBILITY OF SUCH DAMAGE.
27290fe400Spooka  */
28290fe400Spooka 
29290fe400Spooka #define _LWP_API_PRIVATE
30290fe400Spooka 
31290fe400Spooka #include <sys/cdefs.h>
32*bb14ae40Sandvar __KERNEL_RCSID(0, "$NetBSD: subr_lwp_specificdata.c,v 1.5 2024/09/01 19:56:18 andvar Exp $");
33290fe400Spooka 
34290fe400Spooka #include <sys/param.h>
35290fe400Spooka #include <sys/lwp.h>
36290fe400Spooka #include <sys/specificdata.h>
37290fe400Spooka 
38290fe400Spooka static specificdata_domain_t lwp_specificdata_domain;
39290fe400Spooka 
40290fe400Spooka void
41302c1e42Schristos lwpinit_specificdata(void)
42290fe400Spooka {
43290fe400Spooka 
44290fe400Spooka 	lwp_specificdata_domain = specificdata_domain_create();
45290fe400Spooka 	KASSERT(lwp_specificdata_domain != NULL);
46290fe400Spooka }
47290fe400Spooka 
48290fe400Spooka /*
49290fe400Spooka  * lwp_specific_key_create --
50290fe400Spooka  *	Create a key for subsystem lwp-specific data.
51290fe400Spooka  */
52290fe400Spooka int
53290fe400Spooka lwp_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
54290fe400Spooka {
55290fe400Spooka 
56290fe400Spooka 	return (specificdata_key_create(lwp_specificdata_domain, keyp, dtor));
57290fe400Spooka }
58290fe400Spooka 
59290fe400Spooka /*
60290fe400Spooka  * lwp_specific_key_delete --
61290fe400Spooka  *	Delete a key for subsystem lwp-specific data.
62290fe400Spooka  */
63290fe400Spooka void
64290fe400Spooka lwp_specific_key_delete(specificdata_key_t key)
65290fe400Spooka {
66290fe400Spooka 
67290fe400Spooka 	specificdata_key_delete(lwp_specificdata_domain, key);
68290fe400Spooka }
69290fe400Spooka 
70290fe400Spooka /*
71290fe400Spooka  * lwp_initspecific --
72290fe400Spooka  *	Initialize an LWP's specificdata container.
73290fe400Spooka  */
74290fe400Spooka void
75290fe400Spooka lwp_initspecific(struct lwp *l)
76290fe400Spooka {
77dad6303aSmartin 	int error __diagused;
78290fe400Spooka 
79290fe400Spooka 	error = specificdata_init(lwp_specificdata_domain, &l->l_specdataref);
80290fe400Spooka 	KASSERT(error == 0);
81290fe400Spooka }
82290fe400Spooka 
83290fe400Spooka /*
84290fe400Spooka  * lwp_finispecific --
85290fe400Spooka  *	Finalize an LWP's specificdata container.
86290fe400Spooka  */
87290fe400Spooka void
88290fe400Spooka lwp_finispecific(struct lwp *l)
89290fe400Spooka {
90290fe400Spooka 
91290fe400Spooka 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
92290fe400Spooka }
93290fe400Spooka 
94290fe400Spooka /*
95290fe400Spooka  * lwp_getspecific --
96290fe400Spooka  *	Return lwp-specific data corresponding to the specified key.
97290fe400Spooka  *
98290fe400Spooka  *	Note: LWP specific data is NOT INTERLOCKED.  An LWP should access
99290fe400Spooka  *	only its OWN SPECIFIC DATA.  If it is necessary to access another
100*bb14ae40Sandvar  *	LWP's specific data, care must be taken to ensure that doing so
101290fe400Spooka  *	would not cause internal data structure inconsistency (i.e. caller
102290fe400Spooka  *	can guarantee that the target LWP is not inside an lwp_getspecific()
103290fe400Spooka  *	or lwp_setspecific() call).
104290fe400Spooka  */
105290fe400Spooka void *
106290fe400Spooka lwp_getspecific(specificdata_key_t key)
107290fe400Spooka {
108290fe400Spooka 
109290fe400Spooka 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
110290fe400Spooka 						  &curlwp->l_specdataref, key));
111290fe400Spooka }
112290fe400Spooka 
113290fe400Spooka void *
114290fe400Spooka _lwp_getspecific_by_lwp(struct lwp *l, specificdata_key_t key)
115290fe400Spooka {
116290fe400Spooka 
117290fe400Spooka 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
118290fe400Spooka 						  &l->l_specdataref, key));
119290fe400Spooka }
120290fe400Spooka 
121290fe400Spooka /*
122290fe400Spooka  * lwp_setspecific --
123290fe400Spooka  *	Set lwp-specific data corresponding to the specified key.
124290fe400Spooka  */
125290fe400Spooka void
126290fe400Spooka lwp_setspecific(specificdata_key_t key, void *data)
127290fe400Spooka {
128290fe400Spooka 
129290fe400Spooka 	specificdata_setspecific(lwp_specificdata_domain,
130290fe400Spooka 				 &curlwp->l_specdataref, key, data);
131290fe400Spooka }
1327fc219a5Sozaki-r 
1337fc219a5Sozaki-r void
1347fc219a5Sozaki-r lwp_setspecific_by_lwp(struct lwp *l, specificdata_key_t key, void *data)
1357fc219a5Sozaki-r {
1367fc219a5Sozaki-r 
1377fc219a5Sozaki-r 	specificdata_setspecific(lwp_specificdata_domain,
1387fc219a5Sozaki-r 				 &l->l_specdataref, key, data);
1397fc219a5Sozaki-r }
140