8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libumem / common / umem_update_thread.c
blob5b393cfb13ba0e20b191b31e79fa97bedb8a9666
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 #include "umem_base.h"
28 #include "vmem_base.h"
30 #include <sys/ccompile.h>
32 #include <signal.h>
34 /*ARGSUSED*/
35 static void * __NORETURN
36 umem_update_thread(void *arg)
38 struct timeval now;
39 int in_update = 0;
41 (void) mutex_lock(&umem_update_lock);
43 ASSERT(umem_update_thr == thr_self());
44 ASSERT(umem_st_update_thr == 0);
46 for (;;) {
47 umem_process_updates();
49 if (in_update) {
50 in_update = 0;
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) {
60 case UMEM_REAP_DONE:
61 case UMEM_REAP_ADDING:
62 break;
64 case UMEM_REAP_ACTIVE:
65 umem_reap_next = gethrtime() +
66 (hrtime_t)umem_reap_interval * NANOSEC;
67 umem_reaping = UMEM_REAP_DONE;
68 break;
70 default:
71 ASSERT(umem_reaping == UMEM_REAP_DONE ||
72 umem_reaping == UMEM_REAP_ADDING ||
73 umem_reaping == UMEM_REAP_ACTIVE);
74 break;
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);
86 vmem_update(NULL);
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);
95 in_update = 1;
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) {
104 int cancel_state;
105 timespec_t abs_time;
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,
110 &cancel_state);
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;
123 thread_t newthread;
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,
143 &newthread) == 0) {
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);
159 return (1);
160 } else { /* thr_create failed */
161 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
162 (void) mutex_lock(&umem_update_lock);
164 return (0);