root/trunk/umem_update_thread.c

Revision 59, 5.8 kB (checked in by wez, 4 years ago)

Sync with the current sources from onnv-gate.

The solaris sources use a non-portable create-thread-suspended flag when
spawning the update thread; I've thrown together a pthreads portable
equivalent. This has not had any real level of testing.

These changes include a lock around the underlying brk() call; the lack of lock
in earlier revisions of this repo may be the reason that
UMEM_OPTIONS=backend=sbrk was flaky.

  • Property svn:eol-style set to native
Line 
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Portions Copyright 2006-2008 Message Systems, Inc.
27  */
28
29 /* #pragma ident        "@(#)umem_update_thread.c       1.2     05/06/08 SMI" */
30
31 #include "config.h"
32 #include "umem_base.h"
33 #include "vmem_base.h"
34
35 #include <signal.h>
36
37 struct umem_suspend_signal_object {
38         /* locked by creating thread; unlocked when umem_update_thread
39          * can proceed */
40         pthread_mutex_t mtx;
41         /* lock associated with the condition variable */
42         pthread_mutex_t cmtx;
43         /* condition variable is signalled by umem_update_thread when
44          * it has obtained the mtx; it is then safe for the creating
45          * thread to clean up its stack (on which this object resides) */
46         pthread_cond_t cond;
47         int flag;
48 };
49
50 /*ARGSUSED*/
51 static THR_RETURN
52 THR_API umem_update_thread(void *arg)
53 {
54         struct timeval now;
55         int in_update = 0;
56         struct umem_suspend_signal_object *obj = arg;
57
58         pthread_mutex_lock(&obj->mtx);
59         obj->flag = 1;
60         pthread_cond_signal(&obj->cond);
61         obj = NULL;
62
63         (void) mutex_lock(&umem_update_lock);
64
65         ASSERT(umem_update_thr == thr_self());
66         ASSERT(umem_st_update_thr == 0);
67
68         for (;;) {
69                 umem_process_updates();
70
71                 if (in_update) {
72                         in_update = 0;
73                         /*
74                          * we wait until now to set the next update time
75                          * so that the updates are self-throttling
76                          */
77                         (void) gettimeofday(&umem_update_next, NULL);
78                         umem_update_next.tv_sec += umem_reap_interval;
79                 }
80
81                 switch (umem_reaping) {
82                 case UMEM_REAP_DONE:
83                 case UMEM_REAP_ADDING:
84                         break;
85
86                 case UMEM_REAP_ACTIVE:
87                         umem_reap_next = gethrtime() +
88                             (hrtime_t)umem_reap_interval * NANOSEC;
89                         umem_reaping = UMEM_REAP_DONE;
90                         break;
91
92                 default:
93                         ASSERT(umem_reaping == UMEM_REAP_DONE ||
94                             umem_reaping == UMEM_REAP_ADDING ||
95                             umem_reaping == UMEM_REAP_ACTIVE);
96                         break;
97                 }
98
99                 (void) gettimeofday(&now, NULL);
100                 if (now.tv_sec > umem_update_next.tv_sec ||
101                     (now.tv_sec == umem_update_next.tv_sec &&
102                     now.tv_usec >= umem_update_next.tv_usec)) {
103                         /*
104                          * Time to run an update
105                          */
106                         (void) mutex_unlock(&umem_update_lock);
107
108                         vmem_update(NULL);
109                         /*
110                          * umem_cache_update can use umem_add_update to
111                          * request further work.  The update is not complete
112                          * until all such work is finished.
113                          */
114                         umem_cache_applyall(umem_cache_update);
115
116                         (void) mutex_lock(&umem_update_lock);
117                         in_update = 1;
118                         continue;       /* start processing immediately */
119                 }
120
121                 /*
122                  * if there is no work to do, we wait until it is time for
123                  * next update, or someone wakes us.
124                  */
125                 if (umem_null_cache.cache_unext == &umem_null_cache) {
126                         int cancel_state;
127                         timespec_t abs_time;
128                         abs_time.tv_sec = umem_update_next.tv_sec;
129                         abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
130
131                         (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
132                             &cancel_state);
133                         (void) cond_timedwait(&umem_update_cv,
134                             &umem_update_lock, &abs_time);
135                         (void) pthread_setcancelstate(cancel_state, NULL);
136                 }
137         }
138         /* LINTED no return statement */
139 }
140
141 int
142 umem_create_update_thread(void)
143 {
144 #ifndef _WIN32
145         sigset_t sigmask, oldmask;
146 #endif
147         pthread_t newthread;
148         pthread_attr_t attr;
149         struct umem_suspend_signal_object obj;
150         int cancel_state;
151
152         ASSERT(MUTEX_HELD(&umem_update_lock));
153         ASSERT(umem_update_thr == 0);
154
155 #ifndef _WIN32
156         /*
157          * The update thread handles no signals
158          */
159         (void) sigfillset(&sigmask);
160         (void) pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
161 #endif
162         /*
163          * drop the umem_update_lock; we cannot hold locks acquired in
164          * pre-fork handler while calling thr_create or thr_continue().
165          */
166
167         (void) mutex_unlock(&umem_update_lock);
168
169         pthread_attr_init(&attr);
170         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
171
172         pthread_mutex_init(&obj.mtx, NULL);
173         pthread_mutex_init(&obj.cmtx, NULL);
174         pthread_cond_init(&obj.cond, NULL);
175         obj.flag = 0;
176         pthread_mutex_lock(&obj.mtx);
177
178         if (pthread_create(&newthread, &attr, umem_update_thread, &obj) == 0) {
179 #ifndef _WIN32
180                 (void) pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
181 #endif
182                 (void) mutex_lock(&umem_update_lock);
183                 /*
184                  * due to the locking in umem_reap(), only one thread can
185                  * ever call umem_create_update_thread() at a time.  This
186                  * must be the case for this code to work.
187                  */
188
189                 ASSERT(umem_update_thr == 0);
190                 umem_update_thr = newthread;
191                 (void) mutex_unlock(&umem_update_lock);
192
193                 /* tell the thread to continue */
194                 pthread_mutex_unlock(&obj.mtx);
195
196                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
197                 /* wait for it to be done with obj */
198                 pthread_mutex_lock(&obj.cmtx);
199                 do {
200                         if (obj.flag) {
201                                 break;
202                         }
203                         ASSERT(pthread_cond_wait(&obj.cond, &obj.cmtx) == 0);
204                 } while (1);
205                 pthread_setcancelstate(cancel_state, NULL);
206                 pthread_mutex_destroy(&obj.mtx);
207                 pthread_mutex_destroy(&obj.cmtx);
208                 pthread_cond_destroy(&obj.cond);
209
210                 (void) mutex_lock(&umem_update_lock);
211
212                 return (1);
213         } else { /* thr_create failed */
214                 (void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
215                 (void) mutex_lock(&umem_update_lock);
216                 pthread_mutex_destroy(&obj.mtx);
217                 pthread_mutex_destroy(&obj.cmtx);
218                 pthread_cond_destroy(&obj.cond);
219         }
220         return (0);
221 }
Note: See TracBrowser for help on using the browser.