dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libumem / common / umem_fork.c
blob4cbe81dfe9d419126d13e03b876380e185f8dd13
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "umem_base.h"
30 #include "vmem_base.h"
32 #include <unistd.h>
35 * The following functions are for pre- and post-fork1(2) handling. See
36 * "Lock Ordering" in lib/libumem/common/umem.c for the lock ordering used.
39 static void
40 umem_lockup_cache(umem_cache_t *cp)
42 int idx;
43 int ncpus = cp->cache_cpu_mask + 1;
45 for (idx = 0; idx < ncpus; idx++)
46 (void) mutex_lock(&cp->cache_cpu[idx].cc_lock);
48 (void) mutex_lock(&cp->cache_depot_lock);
49 (void) mutex_lock(&cp->cache_lock);
52 static void
53 umem_release_cache(umem_cache_t *cp)
55 int idx;
56 int ncpus = cp->cache_cpu_mask + 1;
58 (void) mutex_unlock(&cp->cache_lock);
59 (void) mutex_unlock(&cp->cache_depot_lock);
61 for (idx = 0; idx < ncpus; idx++)
62 (void) mutex_unlock(&cp->cache_cpu[idx].cc_lock);
65 static void
66 umem_lockup_log_header(umem_log_header_t *lhp)
68 int idx;
69 if (lhp == NULL)
70 return;
71 for (idx = 0; idx < umem_max_ncpus; idx++)
72 (void) mutex_lock(&lhp->lh_cpu[idx].clh_lock);
74 (void) mutex_lock(&lhp->lh_lock);
77 static void
78 umem_release_log_header(umem_log_header_t *lhp)
80 int idx;
81 if (lhp == NULL)
82 return;
84 (void) mutex_unlock(&lhp->lh_lock);
86 for (idx = 0; idx < umem_max_ncpus; idx++)
87 (void) mutex_unlock(&lhp->lh_cpu[idx].clh_lock);
90 static void
91 umem_lockup(void)
93 umem_cache_t *cp;
95 (void) mutex_lock(&umem_init_lock);
97 * If another thread is busy initializing the library, we must
98 * wait for it to complete (by calling umem_init()) before allowing
99 * the fork() to proceed.
101 if (umem_ready == UMEM_READY_INITING && umem_init_thr != thr_self()) {
102 (void) mutex_unlock(&umem_init_lock);
103 (void) umem_init();
104 (void) mutex_lock(&umem_init_lock);
107 vmem_lockup();
108 vmem_sbrk_lockup();
110 (void) mutex_lock(&umem_cache_lock);
111 (void) mutex_lock(&umem_update_lock);
112 (void) mutex_lock(&umem_flags_lock);
114 umem_lockup_cache(&umem_null_cache);
115 for (cp = umem_null_cache.cache_prev; cp != &umem_null_cache;
116 cp = cp->cache_prev)
117 umem_lockup_cache(cp);
119 umem_lockup_log_header(umem_transaction_log);
120 umem_lockup_log_header(umem_content_log);
121 umem_lockup_log_header(umem_failure_log);
122 umem_lockup_log_header(umem_slab_log);
124 (void) cond_broadcast(&umem_update_cv);
128 static void
129 umem_do_release(int as_child)
131 umem_cache_t *cp;
132 int cleanup_update = 0;
135 * Clean up the update state if we are the child process and
136 * another thread was processing updates.
138 if (as_child) {
139 if (umem_update_thr != thr_self()) {
140 umem_update_thr = 0;
141 cleanup_update = 1;
143 if (umem_st_update_thr != thr_self()) {
144 umem_st_update_thr = 0;
145 cleanup_update = 1;
149 if (cleanup_update) {
150 umem_reaping = UMEM_REAP_DONE;
152 for (cp = umem_null_cache.cache_next; cp != &umem_null_cache;
153 cp = cp->cache_next) {
154 if (cp->cache_uflags & UMU_NOTIFY)
155 cp->cache_uflags &= ~UMU_NOTIFY;
158 * If the cache is active, we just re-add it to
159 * the update list. This will re-do any active
160 * updates on the cache, but that won't break
161 * anything.
163 * The worst that can happen is a cache has
164 * its magazines rescaled twice, instead of once.
166 if (cp->cache_uflags & UMU_ACTIVE) {
167 umem_cache_t *cnext, *cprev;
169 ASSERT(cp->cache_unext == NULL &&
170 cp->cache_uprev == NULL);
172 cp->cache_uflags &= ~UMU_ACTIVE;
173 cp->cache_unext = cnext = &umem_null_cache;
174 cp->cache_uprev = cprev =
175 umem_null_cache.cache_uprev;
176 cnext->cache_uprev = cp;
177 cprev->cache_unext = cp;
182 umem_release_log_header(umem_slab_log);
183 umem_release_log_header(umem_failure_log);
184 umem_release_log_header(umem_content_log);
185 umem_release_log_header(umem_transaction_log);
187 for (cp = umem_null_cache.cache_next; cp != &umem_null_cache;
188 cp = cp->cache_next)
189 umem_release_cache(cp);
190 umem_release_cache(&umem_null_cache);
192 (void) mutex_unlock(&umem_flags_lock);
193 (void) mutex_unlock(&umem_update_lock);
194 (void) mutex_unlock(&umem_cache_lock);
196 vmem_sbrk_release();
197 vmem_release();
199 (void) mutex_unlock(&umem_init_lock);
202 static void
203 umem_release(void)
205 umem_do_release(0);
208 static void
209 umem_release_child(void)
211 umem_do_release(1);
214 void
215 umem_forkhandler_init(void)
218 * There is no way to unregister these atfork functions,
219 * but we don't need to. The dynamic linker and libc take
220 * care of unregistering them if/when the library is unloaded.
222 (void) pthread_atfork(umem_lockup, umem_release, umem_release_child);