4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Portions Copyright 2006 OmniTI, Inc.
30 /* #pragma ident "@(#)umem_fork.c 1.3 05/06/08 SMI" */
33 /* #include "mtlib.h" */
34 #include "umem_base.h"
35 #include "vmem_base.h"
41 * The following functions are for pre- and post-fork1(2) handling.
45 umem_lockup_cache(umem_cache_t *cp)
48 int ncpus = cp->cache_cpu_mask + 1;
50 for (idx = 0; idx < ncpus; idx++)
51 (void) mutex_lock(&cp->cache_cpu[idx].cc_lock);
53 (void) mutex_lock(&cp->cache_depot_lock);
54 (void) mutex_lock(&cp->cache_lock);
58 umem_release_cache(umem_cache_t *cp)
61 int ncpus = cp->cache_cpu_mask + 1;
63 (void) mutex_unlock(&cp->cache_lock);
64 (void) mutex_unlock(&cp->cache_depot_lock);
66 for (idx = 0; idx < ncpus; idx++)
67 (void) mutex_unlock(&cp->cache_cpu[idx].cc_lock);
71 umem_lockup_log_header(umem_log_header_t *lhp)
76 for (idx = 0; idx < umem_max_ncpus; idx++)
77 (void) mutex_lock(&lhp->lh_cpu[idx].clh_lock);
79 (void) mutex_lock(&lhp->lh_lock);
83 umem_release_log_header(umem_log_header_t *lhp)
89 (void) mutex_unlock(&lhp->lh_lock);
91 for (idx = 0; idx < umem_max_ncpus; idx++)
92 (void) mutex_unlock(&lhp->lh_cpu[idx].clh_lock);
100 (void) mutex_lock(&umem_init_lock);
102 * If another thread is busy initializing the library, we must
103 * wait for it to complete (by calling umem_init()) before allowing
104 * the fork() to proceed.
106 if (umem_ready == UMEM_READY_INITING && umem_init_thr != thr_self()) {
107 (void) mutex_unlock(&umem_init_lock);
109 (void) mutex_lock(&umem_init_lock);
111 (void) mutex_lock(&umem_cache_lock);
112 (void) mutex_lock(&umem_update_lock);
113 (void) mutex_lock(&umem_flags_lock);
115 umem_lockup_cache(&umem_null_cache);
116 for (cp = umem_null_cache.cache_prev; cp != &umem_null_cache;
118 umem_lockup_cache(cp);
120 umem_lockup_log_header(umem_transaction_log);
121 umem_lockup_log_header(umem_content_log);
122 umem_lockup_log_header(umem_failure_log);
123 umem_lockup_log_header(umem_slab_log);
125 (void) cond_broadcast(&umem_update_cv);
139 umem_release_log_header(umem_slab_log);
140 umem_release_log_header(umem_failure_log);
141 umem_release_log_header(umem_content_log);
142 umem_release_log_header(umem_transaction_log);
144 for (cp = umem_null_cache.cache_next; cp != &umem_null_cache;
146 umem_release_cache(cp);
147 umem_release_cache(&umem_null_cache);
149 (void) mutex_unlock(&umem_flags_lock);
150 (void) mutex_unlock(&umem_update_lock);
151 (void) mutex_unlock(&umem_cache_lock);
152 (void) mutex_unlock(&umem_init_lock);
156 umem_release_child(void)
161 * Clean up the update state
165 if (umem_st_update_thr != thr_self()) {
166 umem_st_update_thr = 0;
167 umem_reaping = UMEM_REAP_DONE;
169 for (cp = umem_null_cache.cache_next; cp != &umem_null_cache;
170 cp = cp->cache_next) {
171 if (cp->cache_uflags & UMU_NOTIFY)
172 cp->cache_uflags &= ~UMU_NOTIFY;
175 * If the cache is active, we just re-add it to
176 * the update list. This will re-do any active
177 * updates on the cache, but that won't break
180 * The worst that can happen is a cache has
181 * its magazines rescaled twice, instead of once.
183 if (cp->cache_uflags & UMU_ACTIVE) {
184 umem_cache_t *cnext, *cprev;
186 ASSERT(cp->cache_unext == NULL &&
187 cp->cache_uprev == NULL);
189 cp->cache_uflags &= ~UMU_ACTIVE;
190 cp->cache_unext = cnext = &umem_null_cache;
191 cp->cache_uprev = cprev =
192 umem_null_cache.cache_uprev;
193 cnext->cache_uprev = cp;
194 cprev->cache_unext = cp;
204 umem_forkhandler_init(void)
208 * There is no way to unregister these atfork functions,
209 * but we don't need to. The dynamic linker and libc take
210 * care of unregistering them if/when the library is unloaded.
212 (void) pthread_atfork(umem_lockup, umem_release, umem_release_child);