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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include "umem_base.h"
28 #include "vmem_base.h"
30 #include <sys/ccompile.h>
35 static void * __NORETURN
36 umem_update_thread(void *arg
)
41 (void) mutex_lock(&umem_update_lock
);
43 ASSERT(umem_update_thr
== thr_self());
44 ASSERT(umem_st_update_thr
== 0);
47 umem_process_updates();
52 * we wait until now to set the next update time
53 * so that the updates are self-throttling
55 (void) gettimeofday(&umem_update_next
, NULL
);
56 umem_update_next
.tv_sec
+= umem_reap_interval
;
59 switch (umem_reaping
) {
61 case UMEM_REAP_ADDING
:
64 case UMEM_REAP_ACTIVE
:
65 umem_reap_next
= gethrtime() +
66 (hrtime_t
)umem_reap_interval
* NANOSEC
;
67 umem_reaping
= UMEM_REAP_DONE
;
71 ASSERT(umem_reaping
== UMEM_REAP_DONE
||
72 umem_reaping
== UMEM_REAP_ADDING
||
73 umem_reaping
== UMEM_REAP_ACTIVE
);
77 (void) gettimeofday(&now
, NULL
);
78 if (now
.tv_sec
> umem_update_next
.tv_sec
||
79 (now
.tv_sec
== umem_update_next
.tv_sec
&&
80 now
.tv_usec
>= umem_update_next
.tv_usec
)) {
82 * Time to run an update
84 (void) mutex_unlock(&umem_update_lock
);
88 * umem_cache_update can use umem_add_update to
89 * request further work. The update is not complete
90 * until all such work is finished.
92 umem_cache_applyall(umem_cache_update
);
94 (void) mutex_lock(&umem_update_lock
);
96 continue; /* start processing immediately */
100 * if there is no work to do, we wait until it is time for
101 * next update, or someone wakes us.
103 if (umem_null_cache
.cache_unext
== &umem_null_cache
) {
106 abs_time
.tv_sec
= umem_update_next
.tv_sec
;
107 abs_time
.tv_nsec
= umem_update_next
.tv_usec
* 1000;
109 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
,
111 (void) cond_timedwait(&umem_update_cv
,
112 &umem_update_lock
, &abs_time
);
113 (void) pthread_setcancelstate(cancel_state
, NULL
);
116 /* LINTED no return statement */
120 umem_create_update_thread(void)
122 sigset_t sigmask
, oldmask
;
125 ASSERT(MUTEX_HELD(&umem_update_lock
));
126 ASSERT(umem_update_thr
== 0);
129 * The update thread handles no signals
131 (void) sigfillset(&sigmask
);
132 (void) thr_sigsetmask(SIG_BLOCK
, &sigmask
, &oldmask
);
135 * drop the umem_update_lock; we cannot hold locks acquired in
136 * pre-fork handler while calling thr_create or thr_continue().
139 (void) mutex_unlock(&umem_update_lock
);
141 if (thr_create(NULL
, NULL
, umem_update_thread
, NULL
,
142 THR_BOUND
| THR_DAEMON
| THR_DETACHED
| THR_SUSPENDED
,
144 (void) thr_sigsetmask(SIG_SETMASK
, &oldmask
, NULL
);
146 (void) mutex_lock(&umem_update_lock
);
148 * due to the locking in umem_reap(), only one thread can
149 * ever call umem_create_update_thread() at a time. This
150 * must be the case for this code to work.
153 ASSERT(umem_update_thr
== 0);
154 umem_update_thr
= newthread
;
155 (void) mutex_unlock(&umem_update_lock
);
156 (void) thr_continue(newthread
);
157 (void) mutex_lock(&umem_update_lock
);
160 } else { /* thr_create failed */
161 (void) thr_sigsetmask(SIG_SETMASK
, &oldmask
, NULL
);
162 (void) mutex_lock(&umem_update_lock
);