2006-03-10 02:45:59 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
2010-06-26 16:02:39 +00:00
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
2006-03-10 02:45:59 +00:00
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
2012-03-17 15:32:48 +00:00
|
|
|
|
2006-03-10 02:45:59 +00:00
|
|
|
/*
|
2010-06-26 16:02:39 +00:00
|
|
|
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
2006-03-10 02:45:59 +00:00
|
|
|
* Use is subject to license terms.
|
2012-03-17 15:32:48 +00:00
|
|
|
*
|
|
|
|
* Portions Copyright 2006-2008 Message Systems, Inc. All rights reserved.
|
2006-03-10 02:45:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* #pragma ident "@(#)umem_update_thread.c 1.2 05/06/08 SMI" */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "umem_base.h"
|
|
|
|
#include "vmem_base.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
2010-06-26 16:02:39 +00:00
|
|
|
struct umem_suspend_signal_object {
|
|
|
|
/* locked by creating thread; unlocked when umem_update_thread
|
|
|
|
* can proceed */
|
|
|
|
pthread_mutex_t mtx;
|
|
|
|
/* lock associated with the condition variable */
|
|
|
|
pthread_mutex_t cmtx;
|
|
|
|
/* condition variable is signalled by umem_update_thread when
|
|
|
|
* it has obtained the mtx; it is then safe for the creating
|
|
|
|
* thread to clean up its stack (on which this object resides) */
|
|
|
|
pthread_cond_t cond;
|
|
|
|
int flag;
|
|
|
|
};
|
2006-03-10 02:45:59 +00:00
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static THR_RETURN
|
|
|
|
THR_API umem_update_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
int in_update = 0;
|
2010-06-26 16:02:39 +00:00
|
|
|
struct umem_suspend_signal_object *obj = arg;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&obj->mtx);
|
|
|
|
obj->flag = 1;
|
|
|
|
pthread_cond_signal(&obj->cond);
|
|
|
|
obj = NULL;
|
2006-03-10 02:45:59 +00:00
|
|
|
|
|
|
|
(void) mutex_lock(&umem_update_lock);
|
|
|
|
|
|
|
|
ASSERT(umem_update_thr == thr_self());
|
|
|
|
ASSERT(umem_st_update_thr == 0);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
umem_process_updates();
|
|
|
|
|
|
|
|
if (in_update) {
|
|
|
|
in_update = 0;
|
|
|
|
/*
|
|
|
|
* we wait until now to set the next update time
|
|
|
|
* so that the updates are self-throttling
|
|
|
|
*/
|
|
|
|
(void) gettimeofday(&umem_update_next, NULL);
|
|
|
|
umem_update_next.tv_sec += umem_reap_interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (umem_reaping) {
|
|
|
|
case UMEM_REAP_DONE:
|
|
|
|
case UMEM_REAP_ADDING:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UMEM_REAP_ACTIVE:
|
|
|
|
umem_reap_next = gethrtime() +
|
|
|
|
(hrtime_t)umem_reap_interval * NANOSEC;
|
|
|
|
umem_reaping = UMEM_REAP_DONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ASSERT(umem_reaping == UMEM_REAP_DONE ||
|
|
|
|
umem_reaping == UMEM_REAP_ADDING ||
|
|
|
|
umem_reaping == UMEM_REAP_ACTIVE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) gettimeofday(&now, NULL);
|
|
|
|
if (now.tv_sec > umem_update_next.tv_sec ||
|
|
|
|
(now.tv_sec == umem_update_next.tv_sec &&
|
|
|
|
now.tv_usec >= umem_update_next.tv_usec)) {
|
|
|
|
/*
|
|
|
|
* Time to run an update
|
|
|
|
*/
|
|
|
|
(void) mutex_unlock(&umem_update_lock);
|
|
|
|
|
|
|
|
vmem_update(NULL);
|
|
|
|
/*
|
|
|
|
* umem_cache_update can use umem_add_update to
|
|
|
|
* request further work. The update is not complete
|
|
|
|
* until all such work is finished.
|
|
|
|
*/
|
|
|
|
umem_cache_applyall(umem_cache_update);
|
|
|
|
|
|
|
|
(void) mutex_lock(&umem_update_lock);
|
|
|
|
in_update = 1;
|
|
|
|
continue; /* start processing immediately */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if there is no work to do, we wait until it is time for
|
|
|
|
* next update, or someone wakes us.
|
|
|
|
*/
|
|
|
|
if (umem_null_cache.cache_unext == &umem_null_cache) {
|
2010-06-26 16:02:39 +00:00
|
|
|
int cancel_state;
|
2006-03-10 02:45:59 +00:00
|
|
|
timespec_t abs_time;
|
|
|
|
abs_time.tv_sec = umem_update_next.tv_sec;
|
|
|
|
abs_time.tv_nsec = umem_update_next.tv_usec * 1000;
|
|
|
|
|
2010-06-26 16:02:39 +00:00
|
|
|
(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
|
|
|
|
&cancel_state);
|
|
|
|
(void) cond_timedwait(&umem_update_cv,
|
2006-03-10 02:45:59 +00:00
|
|
|
&umem_update_lock, &abs_time);
|
2010-06-26 16:02:39 +00:00
|
|
|
(void) pthread_setcancelstate(cancel_state, NULL);
|
2006-03-10 02:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* LINTED no return statement */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
umem_create_update_thread(void)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
sigset_t sigmask, oldmask;
|
|
|
|
#endif
|
2010-06-26 16:02:39 +00:00
|
|
|
pthread_t newthread;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
struct umem_suspend_signal_object obj;
|
|
|
|
int cancel_state;
|
2006-03-10 02:45:59 +00:00
|
|
|
|
|
|
|
ASSERT(MUTEX_HELD(&umem_update_lock));
|
|
|
|
ASSERT(umem_update_thr == 0);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
/*
|
|
|
|
* The update thread handles no signals
|
|
|
|
*/
|
|
|
|
(void) sigfillset(&sigmask);
|
2010-06-26 16:02:39 +00:00
|
|
|
(void) pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
|
2006-03-10 02:45:59 +00:00
|
|
|
#endif
|
2010-06-26 16:02:39 +00:00
|
|
|
/*
|
|
|
|
* drop the umem_update_lock; we cannot hold locks acquired in
|
|
|
|
* pre-fork handler while calling thr_create or thr_continue().
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void) mutex_unlock(&umem_update_lock);
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
|
|
|
pthread_mutex_init(&obj.mtx, NULL);
|
|
|
|
pthread_mutex_init(&obj.cmtx, NULL);
|
|
|
|
pthread_cond_init(&obj.cond, NULL);
|
|
|
|
obj.flag = 0;
|
|
|
|
pthread_mutex_lock(&obj.mtx);
|
|
|
|
|
|
|
|
if (pthread_create(&newthread, &attr, umem_update_thread, &obj) == 0) {
|
2006-03-10 02:45:59 +00:00
|
|
|
#ifndef _WIN32
|
2010-06-26 16:02:39 +00:00
|
|
|
(void) pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
|
2006-03-10 02:45:59 +00:00
|
|
|
#endif
|
2010-06-26 16:02:39 +00:00
|
|
|
(void) mutex_lock(&umem_update_lock);
|
|
|
|
/*
|
|
|
|
* due to the locking in umem_reap(), only one thread can
|
|
|
|
* ever call umem_create_update_thread() at a time. This
|
|
|
|
* must be the case for this code to work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(umem_update_thr == 0);
|
|
|
|
umem_update_thr = newthread;
|
|
|
|
(void) mutex_unlock(&umem_update_lock);
|
|
|
|
|
|
|
|
/* tell the thread to continue */
|
|
|
|
pthread_mutex_unlock(&obj.mtx);
|
|
|
|
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
|
|
|
|
/* wait for it to be done with obj */
|
|
|
|
pthread_mutex_lock(&obj.cmtx);
|
|
|
|
do {
|
|
|
|
if (obj.flag) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ASSERT(pthread_cond_wait(&obj.cond, &obj.cmtx) == 0);
|
|
|
|
} while (1);
|
|
|
|
pthread_setcancelstate(cancel_state, NULL);
|
|
|
|
pthread_mutex_destroy(&obj.mtx);
|
|
|
|
pthread_mutex_destroy(&obj.cmtx);
|
|
|
|
pthread_cond_destroy(&obj.cond);
|
|
|
|
|
|
|
|
(void) mutex_lock(&umem_update_lock);
|
|
|
|
|
2006-03-10 02:45:59 +00:00
|
|
|
return (1);
|
2010-06-26 16:02:39 +00:00
|
|
|
} else { /* thr_create failed */
|
|
|
|
(void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
(void) mutex_lock(&umem_update_lock);
|
|
|
|
pthread_mutex_destroy(&obj.mtx);
|
|
|
|
pthread_mutex_destroy(&obj.cmtx);
|
|
|
|
pthread_cond_destroy(&obj.cond);
|
2006-03-10 02:45:59 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|