xref: /illumos-gate/usr/src/lib/libc/port/threads/tmem.c (revision 4f364e7c)
1*4f364e7cSRobert Mustacchi /*
2*4f364e7cSRobert Mustacchi  * CDDL HEADER START
3*4f364e7cSRobert Mustacchi  *
4*4f364e7cSRobert Mustacchi  * The contents of this file are subject to the terms of the
5*4f364e7cSRobert Mustacchi  * Common Development and Distribution License (the "License").
6*4f364e7cSRobert Mustacchi  * You may not use this file except in compliance with the License.
7*4f364e7cSRobert Mustacchi  *
8*4f364e7cSRobert Mustacchi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*4f364e7cSRobert Mustacchi  * or http://www.opensolaris.org/os/licensing.
10*4f364e7cSRobert Mustacchi  * See the License for the specific language governing permissions
11*4f364e7cSRobert Mustacchi  * and limitations under the License.
12*4f364e7cSRobert Mustacchi  *
13*4f364e7cSRobert Mustacchi  * When distributing Covered Code, include this CDDL HEADER in each
14*4f364e7cSRobert Mustacchi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*4f364e7cSRobert Mustacchi  * If applicable, add the following below this CDDL HEADER, with the
16*4f364e7cSRobert Mustacchi  * fields enclosed by brackets "[]" replaced with your own identifying
17*4f364e7cSRobert Mustacchi  * information: Portions Copyright [yyyy] [name of copyright owner]
18*4f364e7cSRobert Mustacchi  *
19*4f364e7cSRobert Mustacchi  * CDDL HEADER END
20*4f364e7cSRobert Mustacchi  */
21*4f364e7cSRobert Mustacchi 
22*4f364e7cSRobert Mustacchi /*
23*4f364e7cSRobert Mustacchi  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
24*4f364e7cSRobert Mustacchi  */
25*4f364e7cSRobert Mustacchi 
26*4f364e7cSRobert Mustacchi #include "lint.h"
27*4f364e7cSRobert Mustacchi #include "thr_uberdata.h"
28*4f364e7cSRobert Mustacchi 
29*4f364e7cSRobert Mustacchi /*
30*4f364e7cSRobert Mustacchi  * This file implements the private interface with libumem for per-thread
31*4f364e7cSRobert Mustacchi  * caching umem (ptcumem). For the full details on how tcumem works and how
32*4f364e7cSRobert Mustacchi  * these functions work, see section 8.4 of the big theory statement in
33*4f364e7cSRobert Mustacchi  * lib/libumem/common/umem.c.
34*4f364e7cSRobert Mustacchi  */
35*4f364e7cSRobert Mustacchi static tmem_func_t tmem_cleanup = NULL;
36*4f364e7cSRobert Mustacchi 
37*4f364e7cSRobert Mustacchi uintptr_t
_tmem_get_base(void)38*4f364e7cSRobert Mustacchi _tmem_get_base(void)
39*4f364e7cSRobert Mustacchi {
40*4f364e7cSRobert Mustacchi 	return ((uintptr_t)&curthread->ul_tmem - (uintptr_t)curthread);
41*4f364e7cSRobert Mustacchi }
42*4f364e7cSRobert Mustacchi 
43*4f364e7cSRobert Mustacchi int
_tmem_get_nentries(void)44*4f364e7cSRobert Mustacchi _tmem_get_nentries(void)
45*4f364e7cSRobert Mustacchi {
46*4f364e7cSRobert Mustacchi 	return (NTMEMBASE);
47*4f364e7cSRobert Mustacchi }
48*4f364e7cSRobert Mustacchi 
49*4f364e7cSRobert Mustacchi void
_tmem_set_cleanup(tmem_func_t f)50*4f364e7cSRobert Mustacchi _tmem_set_cleanup(tmem_func_t f)
51*4f364e7cSRobert Mustacchi {
52*4f364e7cSRobert Mustacchi 	tmem_cleanup = f;
53*4f364e7cSRobert Mustacchi }
54*4f364e7cSRobert Mustacchi 
55*4f364e7cSRobert Mustacchi /*
56*4f364e7cSRobert Mustacchi  * This is called by _thrp_exit() to clean up any per-thread allocations that
57*4f364e7cSRobert Mustacchi  * are still hanging around and haven't been cleaned up.
58*4f364e7cSRobert Mustacchi  */
59*4f364e7cSRobert Mustacchi void
tmem_exit(void)60*4f364e7cSRobert Mustacchi tmem_exit(void)
61*4f364e7cSRobert Mustacchi {
62*4f364e7cSRobert Mustacchi 	int ii;
63*4f364e7cSRobert Mustacchi 	void *buf, *next;
64*4f364e7cSRobert Mustacchi 	tumem_t *tp = &curthread->ul_tmem;
65*4f364e7cSRobert Mustacchi 
66*4f364e7cSRobert Mustacchi 
67*4f364e7cSRobert Mustacchi 	if (tp->tm_size == 0)
68*4f364e7cSRobert Mustacchi 		return;
69*4f364e7cSRobert Mustacchi 
70*4f364e7cSRobert Mustacchi 	/*
71*4f364e7cSRobert Mustacchi 	 * Since we have something stored here, we need to ensure we declared a
72*4f364e7cSRobert Mustacchi 	 * clean up handler. If we haven't that's broken and our single private
73*4f364e7cSRobert Mustacchi 	 * consumer should be shot.
74*4f364e7cSRobert Mustacchi 	 */
75*4f364e7cSRobert Mustacchi 	if (tmem_cleanup == NULL)
76*4f364e7cSRobert Mustacchi 		abort();
77*4f364e7cSRobert Mustacchi 	for (ii = 0; ii < NTMEMBASE; ii++) {
78*4f364e7cSRobert Mustacchi 		buf = tp->tm_roots[ii];
79*4f364e7cSRobert Mustacchi 		while (buf != NULL) {
80*4f364e7cSRobert Mustacchi 			next = *(void **)buf;
81*4f364e7cSRobert Mustacchi 			tmem_cleanup(buf, ii);
82*4f364e7cSRobert Mustacchi 			buf = next;
83*4f364e7cSRobert Mustacchi 		}
84*4f364e7cSRobert Mustacchi 	}
85*4f364e7cSRobert Mustacchi }
86