Restructure autoconf around ./config directory
[zfs.git] / zfs / lib / libumem / umem_update_thread.c
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Portions Copyright 2006 OmniTI, Inc.
28  */
29
30 /* #pragma ident        "@(#)umem_update_thread.c       1.2     05/06/08 SMI" */
31
32 #include "config.h"
33 #include "umem_base.h"
34 #include "vmem_base.h"
35
36 #include <signal.h>
37
38 /*
39  * we use the _ version, since we don't want to be cancelled.
40  */
41 extern int _cond_timedwait(cond_t *cv, mutex_t *mutex, const timespec_t *delay);
42
43 /*ARGSUSED*/
44 static THR_RETURN
45 THR_API umem_update_thread(void *arg)
46 {
47         struct timeval now;
48         int in_update = 0;
49
50         (void) mutex_lock(&umem_update_lock);
51
52         ASSERT(umem_update_thr == thr_self());
53         ASSERT(umem_st_update_thr == 0);
54
55         for (;;) {
56                 umem_process_updates();
57
58                 if (in_update) {
59                         in_update = 0;
60                         /*
61                          * we wait until now to set the next update time
62                          * so that the updates are self-throttling
63                          */
64                         (void) gettimeofday(&umem_update_next, NULL);
65                         umem_update_next.tv_sec += umem_reap_interval;
66                 }
67
68                 switch (umem_reaping) {
69                 case UMEM_REAP_DONE:
70                 case UMEM_REAP_ADDING:
71                         break;
72
73                 case UMEM_REAP_ACTIVE:
74                         umem_reap_next = gethrtime() +
75                             (hrtime_t)umem_reap_interval * NANOSEC;
76                         umem_reaping = UMEM_REAP_DONE;
77                         break;
78
79                 default:
80                         ASSERT(umem_reaping == UMEM_REAP_DONE ||
81                             umem_reaping == UMEM_REAP_ADDING ||
82                             umem_reaping == UMEM_REAP_ACTIVE);
83                         break;
84                 }
85
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)) {
90                         /*
91                          * Time to run an update
92                          */
93                         (void) mutex_unlock(&umem_update_lock);
94
95                         vmem_update(NULL);
96                         /*
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.
100                          */
101                         umem_cache_applyall(umem_cache_update);
102
103                         (void) mutex_lock(&umem_update_lock);
104                         in_update = 1;
105                         continue;       /* start processing immediately */
106                 }
107
108                 /*
109                  * if there is no work to do, we wait until it is time for
110                  * next update, or someone wakes us.
111                  */
112                 if (umem_null_cache.cache_unext == &umem_null_cache) {
113                         timespec_t abs_time;
114                         abs_time.tv_sec = umem_update_next.tv_sec;
115                         abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
116
117                         (void) _cond_timedwait(&umem_update_cv,
118                             &umem_update_lock, &abs_time);
119                 }
120         }
121         /* LINTED no return statement */
122 }
123
124 int
125 umem_create_update_thread(void)
126 {
127 #ifndef _WIN32
128         sigset_t sigmask, oldmask;
129 #endif
130
131         ASSERT(MUTEX_HELD(&umem_update_lock));
132         ASSERT(umem_update_thr == 0);
133
134 #ifndef _WIN32
135         /*
136          * The update thread handles no signals
137          */
138         (void) sigfillset(&sigmask);
139         (void) thr_sigsetmask(SIG_BLOCK, &sigmask, &oldmask);
140 #endif
141         if (thr_create(NULL, 0, umem_update_thread, NULL,
142             THR_BOUND | THR_DAEMON | THR_DETACHED, &umem_update_thr) == 0) {
143 #ifndef _WIN32
144                 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
145 #endif
146                 return (1);
147         }
148         umem_update_thr = 0;
149 #ifndef _WIN32
150         (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
151 #endif
152         return (0);
153 }