10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56422Sqiao * Common Development and Distribution License (the "License").
66422Sqiao * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216622Sraf
220Sstevel@tonic-gate /*
23*9334SMadhavan.Venkataraman@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/proc.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/debug.h>
300Sstevel@tonic-gate #include <sys/mutex.h>
316622Sraf #include <sys/atomic.h>
320Sstevel@tonic-gate #include <sys/timer.h>
330Sstevel@tonic-gate #include <sys/lwp_timer_impl.h>
348048SMadhavan.Venkataraman@Sun.COM #include <sys/callo.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate * lwp_timer_timeout() is called from a timeout set up in lwp_cond_wait(),
380Sstevel@tonic-gate * lwp_mutex_timedlock(), lwp_sema_timedwait() or lwp_rwlock_lock().
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * It recomputes the time remaining until the absolute time when the
410Sstevel@tonic-gate * wait is supposed to timeout and either calls realtime_timeout()
420Sstevel@tonic-gate * to reschedule itself or calls setrun() on the sleeping thread.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * This is done to ensure that the waiting thread does not wake up
450Sstevel@tonic-gate * due to timer expiration until the absolute future time of the
460Sstevel@tonic-gate * timeout has been reached. Until that time, the thread must
470Sstevel@tonic-gate * remain on its sleep queue.
480Sstevel@tonic-gate *
490Sstevel@tonic-gate * An lwp_timer_t structure is used to pass information
500Sstevel@tonic-gate * about the sleeping thread to the timeout function.
510Sstevel@tonic-gate */
520Sstevel@tonic-gate
530Sstevel@tonic-gate static void
lwp_timer_timeout(void * arg)540Sstevel@tonic-gate lwp_timer_timeout(void *arg)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate lwp_timer_t *lwptp = arg;
570Sstevel@tonic-gate kthread_t *t = lwptp->lwpt_thread;
58*9334SMadhavan.Venkataraman@Sun.COM timespec_t now, delta;
590Sstevel@tonic-gate
600Sstevel@tonic-gate mutex_enter(&t->t_delay_lock);
610Sstevel@tonic-gate gethrestime(&now);
620Sstevel@tonic-gate /*
634123Sdm120769 * Requeue the timeout if no one has reset the system time
644123Sdm120769 * and if the absolute future time has not been reached.
650Sstevel@tonic-gate */
664123Sdm120769 if (lwptp->lwpt_timecheck == timechanged &&
670Sstevel@tonic-gate (lwptp->lwpt_rqtime.tv_sec > now.tv_sec ||
680Sstevel@tonic-gate (lwptp->lwpt_rqtime.tv_sec == now.tv_sec &&
690Sstevel@tonic-gate lwptp->lwpt_rqtime.tv_nsec > now.tv_nsec))) {
706622Sraf lwptp->lwpt_imm_timeout = 0;
71*9334SMadhavan.Venkataraman@Sun.COM delta = lwptp->lwpt_rqtime;
72*9334SMadhavan.Venkataraman@Sun.COM timespecsub(&delta, &now);
738048SMadhavan.Venkataraman@Sun.COM lwptp->lwpt_id = timeout_generic(CALLOUT_REALTIME,
74*9334SMadhavan.Venkataraman@Sun.COM lwp_timer_timeout, lwptp, ts2hrt(&delta), nsec_per_tick,
75*9334SMadhavan.Venkataraman@Sun.COM (CALLOUT_FLAG_HRESTIME | CALLOUT_FLAG_ROUNDUP));
760Sstevel@tonic-gate } else {
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * Set the thread running only if it is asleep on
790Sstevel@tonic-gate * its lwpchan sleep queue (not if it is asleep on
800Sstevel@tonic-gate * the t_delay_lock mutex).
810Sstevel@tonic-gate */
820Sstevel@tonic-gate thread_lock(t);
836622Sraf /* do this for the benefit of upi mutexes */
846622Sraf (void) atomic_cas_uint(&lwptp->lwpt_imm_timeout, 0, 1);
850Sstevel@tonic-gate if (t->t_state == TS_SLEEP &&
860Sstevel@tonic-gate (t->t_flag & T_WAKEABLE) &&
870Sstevel@tonic-gate t->t_wchan0 != NULL)
880Sstevel@tonic-gate setrun_locked(t);
890Sstevel@tonic-gate thread_unlock(t);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate mutex_exit(&t->t_delay_lock);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate int
lwp_timer_copyin(lwp_timer_t * lwptp,timespec_t * tsp)950Sstevel@tonic-gate lwp_timer_copyin(lwp_timer_t *lwptp, timespec_t *tsp)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate timespec_t now;
980Sstevel@tonic-gate int error = 0;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate if (tsp == NULL) /* not really an error, just need to bzero() */
1010Sstevel@tonic-gate goto err;
1024123Sdm120769 lwptp->lwpt_timecheck = timechanged; /* do this before gethrestime() */
1030Sstevel@tonic-gate gethrestime(&now); /* do this before copyin() */
1040Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_NATIVE) {
1050Sstevel@tonic-gate if (copyin(tsp, &lwptp->lwpt_rqtime, sizeof (timespec_t))) {
1060Sstevel@tonic-gate error = EFAULT;
1070Sstevel@tonic-gate goto err;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate } else {
1100Sstevel@tonic-gate timespec32_t ts32;
1110Sstevel@tonic-gate if (copyin(tsp, &ts32, sizeof (timespec32_t))) {
1120Sstevel@tonic-gate error = EFAULT;
1130Sstevel@tonic-gate goto err;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&lwptp->lwpt_rqtime, &ts32);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate if (itimerspecfix(&lwptp->lwpt_rqtime)) {
1180Sstevel@tonic-gate error = EINVAL;
1190Sstevel@tonic-gate goto err;
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * Unless the requested timeout is zero,
1230Sstevel@tonic-gate * get the precise future (absolute) time at
1240Sstevel@tonic-gate * which we are to time out and return ETIME.
1250Sstevel@tonic-gate * We must not return ETIME before that time.
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate if (lwptp->lwpt_rqtime.tv_sec == 0 && lwptp->lwpt_rqtime.tv_nsec == 0) {
1280Sstevel@tonic-gate bzero(lwptp, sizeof (lwp_timer_t));
1290Sstevel@tonic-gate lwptp->lwpt_imm_timeout = 1;
1300Sstevel@tonic-gate } else {
1310Sstevel@tonic-gate lwptp->lwpt_thread = curthread;
1320Sstevel@tonic-gate lwptp->lwpt_tsp = tsp;
1330Sstevel@tonic-gate lwptp->lwpt_time_error = 0;
1340Sstevel@tonic-gate lwptp->lwpt_id = 0;
1350Sstevel@tonic-gate lwptp->lwpt_imm_timeout = 0;
1360Sstevel@tonic-gate timespecadd(&lwptp->lwpt_rqtime, &now);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate return (0);
1390Sstevel@tonic-gate err:
1400Sstevel@tonic-gate bzero(lwptp, sizeof (lwp_timer_t));
1410Sstevel@tonic-gate lwptp->lwpt_time_error = error;
1420Sstevel@tonic-gate return (error);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate int
lwp_timer_enqueue(lwp_timer_t * lwptp)1460Sstevel@tonic-gate lwp_timer_enqueue(lwp_timer_t *lwptp)
1470Sstevel@tonic-gate {
148*9334SMadhavan.Venkataraman@Sun.COM timespec_t now, delta;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate ASSERT(lwptp->lwpt_thread == curthread);
1510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&curthread->t_delay_lock));
1520Sstevel@tonic-gate gethrestime(&now);
1534123Sdm120769 if (lwptp->lwpt_timecheck == timechanged &&
1540Sstevel@tonic-gate (lwptp->lwpt_rqtime.tv_sec > now.tv_sec ||
1550Sstevel@tonic-gate (lwptp->lwpt_rqtime.tv_sec == now.tv_sec &&
1560Sstevel@tonic-gate lwptp->lwpt_rqtime.tv_nsec > now.tv_nsec))) {
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * Queue the timeout.
1590Sstevel@tonic-gate */
1606622Sraf lwptp->lwpt_imm_timeout = 0;
161*9334SMadhavan.Venkataraman@Sun.COM delta = lwptp->lwpt_rqtime;
162*9334SMadhavan.Venkataraman@Sun.COM timespecsub(&delta, &now);
1638048SMadhavan.Venkataraman@Sun.COM lwptp->lwpt_id = timeout_generic(CALLOUT_REALTIME,
164*9334SMadhavan.Venkataraman@Sun.COM lwp_timer_timeout, lwptp, ts2hrt(&delta), nsec_per_tick,
165*9334SMadhavan.Venkataraman@Sun.COM (CALLOUT_FLAG_HRESTIME | CALLOUT_FLAG_ROUNDUP));
1660Sstevel@tonic-gate return (0);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Time has already run out or someone reset the system time;
1710Sstevel@tonic-gate * just cause an immediate timeout.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate lwptp->lwpt_imm_timeout = 1;
1740Sstevel@tonic-gate return (1);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate clock_t
lwp_timer_dequeue(lwp_timer_t * lwptp)1780Sstevel@tonic-gate lwp_timer_dequeue(lwp_timer_t *lwptp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate kthread_t *t = curthread;
1810Sstevel@tonic-gate clock_t tim = -1;
1828048SMadhavan.Venkataraman@Sun.COM callout_id_t tmp_id;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate mutex_enter(&t->t_delay_lock);
1850Sstevel@tonic-gate while ((tmp_id = lwptp->lwpt_id) != 0) {
1860Sstevel@tonic-gate lwptp->lwpt_id = 0;
1870Sstevel@tonic-gate mutex_exit(&t->t_delay_lock);
1888048SMadhavan.Venkataraman@Sun.COM tim = untimeout_default(tmp_id, 0);
1890Sstevel@tonic-gate mutex_enter(&t->t_delay_lock);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate mutex_exit(&t->t_delay_lock);
1920Sstevel@tonic-gate return (tim);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate int
lwp_timer_copyout(lwp_timer_t * lwptp,int error)1960Sstevel@tonic-gate lwp_timer_copyout(lwp_timer_t *lwptp, int error)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate timespec_t rmtime;
1990Sstevel@tonic-gate timespec_t now;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (lwptp->lwpt_tsp == NULL) /* nothing to do */
2020Sstevel@tonic-gate return (error);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate rmtime.tv_sec = rmtime.tv_nsec = 0;
2050Sstevel@tonic-gate if (error != ETIME) {
2060Sstevel@tonic-gate gethrestime(&now);
2070Sstevel@tonic-gate if ((now.tv_sec < lwptp->lwpt_rqtime.tv_sec) ||
2080Sstevel@tonic-gate ((now.tv_sec == lwptp->lwpt_rqtime.tv_sec) &&
2090Sstevel@tonic-gate (now.tv_nsec < lwptp->lwpt_rqtime.tv_nsec))) {
2100Sstevel@tonic-gate rmtime = lwptp->lwpt_rqtime;
2110Sstevel@tonic-gate timespecsub(&rmtime, &now);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate if (curproc->p_model == DATAMODEL_NATIVE) {
2150Sstevel@tonic-gate if (copyout(&rmtime, lwptp->lwpt_tsp, sizeof (timespec_t)))
2160Sstevel@tonic-gate error = EFAULT;
2170Sstevel@tonic-gate } else {
2180Sstevel@tonic-gate timespec32_t rmtime32;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
2210Sstevel@tonic-gate if (copyout(&rmtime32, lwptp->lwpt_tsp, sizeof (timespec32_t)))
2220Sstevel@tonic-gate error = EFAULT;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate return (error);
2260Sstevel@tonic-gate }
227