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_update_thread.c 1.2 05/06/08 SMI" */
33 #include "umem_base.h"
34 #include "vmem_base.h"
39 * we use the _ version, since we don't want to be cancelled.
41 extern int _cond_timedwait(cond_t *cv, mutex_t *mutex, const timespec_t *delay);
45 THR_API umem_update_thread(void *arg)
50 (void) mutex_lock(&umem_update_lock);
52 ASSERT(umem_update_thr == thr_self());
53 ASSERT(umem_st_update_thr == 0);
56 umem_process_updates();
61 * we wait until now to set the next update time
62 * so that the updates are self-throttling
64 (void) gettimeofday(&umem_update_next, NULL);
65 umem_update_next.tv_sec += umem_reap_interval;
68 switch (umem_reaping) {
70 case UMEM_REAP_ADDING:
73 case UMEM_REAP_ACTIVE:
74 umem_reap_next = gethrtime() +
75 (hrtime_t)umem_reap_interval * NANOSEC;
76 umem_reaping = UMEM_REAP_DONE;
80 ASSERT(umem_reaping == UMEM_REAP_DONE ||
81 umem_reaping == UMEM_REAP_ADDING ||
82 umem_reaping == UMEM_REAP_ACTIVE);
86 (void) gettimeofday(&now, NULL);
87 if (now.tv_sec > umem_update_next.tv_sec ||
88 (now.tv_sec == umem_update_next.tv_sec &&
89 now.tv_usec >= umem_update_next.tv_usec)) {
91 * Time to run an update
93 (void) mutex_unlock(&umem_update_lock);
97 * umem_cache_update can use umem_add_update to
98 * request further work. The update is not complete
99 * until all such work is finished.
101 umem_cache_applyall(umem_cache_update);
103 (void) mutex_lock(&umem_update_lock);
105 continue; /* start processing immediately */
109 * if there is no work to do, we wait until it is time for
110 * next update, or someone wakes us.
112 if (umem_null_cache.cache_unext == &umem_null_cache) {
114 abs_time.tv_sec = umem_update_next.tv_sec;
115 abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
117 (void) _cond_timedwait(&umem_update_cv,
118 &umem_update_lock, &abs_time);
121 /* LINTED no return statement */
125 umem_create_update_thread(void)
128 sigset_t sigmask, oldmask;
131 ASSERT(MUTEX_HELD(&umem_update_lock));
132 ASSERT(umem_update_thr == 0);
136 * The update thread handles no signals
138 (void) sigfillset(&sigmask);
139 (void) thr_sigsetmask(SIG_BLOCK, &sigmask, &oldmask);
141 if (thr_create(NULL, 0, umem_update_thread, NULL,
142 THR_BOUND | THR_DAEMON | THR_DETACHED, &umem_update_thr) == 0) {
144 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
150 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);