2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include <time.h>
2N/A#include <sys/types.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <errno.h>
2N/A#include "sigev_thread.h"
2N/A
2N/A/*
2N/A * System call wrappers found elsewhere in libc (common/sys/__clock_timer.s).
2N/A */
2N/Aextern int __clock_getres(clockid_t, timespec_t *);
2N/Aextern int __clock_gettime(clockid_t, timespec_t *);
2N/Aextern int __clock_settime(clockid_t, const timespec_t *);
2N/Aextern int __timer_create(clockid_t, struct sigevent *, timer_t *);
2N/Aextern int __timer_delete(timer_t);
2N/Aextern int __timer_getoverrun(timer_t);
2N/Aextern int __timer_gettime(timer_t, itimerspec_t *);
2N/Aextern int __timer_settime(timer_t, int, const itimerspec_t *, itimerspec_t *);
2N/A
2N/A/*
2N/A * Array of pointers to tcd's, indexed by timer id.
2N/A * No more than 'timer_max' timers can be created by any process.
2N/A */
2N/Aint timer_max = 0;
2N/Athread_communication_data_t **timer_tcd;
2N/Astatic pthread_once_t timer_once = PTHREAD_ONCE_INIT;
2N/A
2N/Astatic void
2N/Atimer_init(void)
2N/A{
2N/A timer_max = (int)_sysconf(_SC_TIMER_MAX);
2N/A timer_tcd = malloc(timer_max * sizeof (*timer_tcd));
2N/A (void) memset(timer_tcd, 0, timer_max * sizeof (*timer_tcd));
2N/A}
2N/A
2N/Aint
2N/Aclock_getres(clockid_t clock_id, timespec_t *res)
2N/A{
2N/A return (__clock_getres(clock_id, res));
2N/A}
2N/A
2N/Aint
2N/Aclock_gettime(clockid_t clock_id, timespec_t *tp)
2N/A{
2N/A return (__clock_gettime(clock_id, tp));
2N/A}
2N/A
2N/Aint
2N/Aclock_settime(clockid_t clock_id, const timespec_t *tp)
2N/A{
2N/A return (__clock_settime(clock_id, tp));
2N/A}
2N/A
2N/Aint
2N/Atimer_create(clockid_t clock_id, struct sigevent *sigevp, timer_t *timerid)
2N/A{
2N/A struct sigevent sigevent;
2N/A port_notify_t port_notify;
2N/A thread_communication_data_t *tcdp;
2N/A int sigev_thread = 0;
2N/A int rc;
2N/A
2N/A (void) pthread_once(&timer_once, timer_init);
2N/A
2N/A if (sigevp != NULL &&
2N/A sigevp->sigev_notify == SIGEV_THREAD &&
2N/A sigevp->sigev_notify_function != NULL) {
2N/A sigev_thread = 1;
2N/A tcdp = setup_sigev_handler(sigevp, TIMER);
2N/A if (tcdp == NULL)
2N/A return (-1);
2N/A /* copy the sigevent structure so we can modify it */
2N/A sigevent = *sigevp;
2N/A sigevp = &sigevent;
2N/A port_notify.portnfy_port = tcdp->tcd_port;
2N/A port_notify.portnfy_user = NULL;
2N/A sigevp->sigev_value.sival_ptr = &port_notify;
2N/A }
2N/A
2N/A rc = __timer_create(clock_id, sigevp, timerid);
2N/A
2N/A if (sigev_thread) {
2N/A if (rc == 0) {
2N/A if ((rc = launch_spawner(tcdp)) != 0)
2N/A (void) __timer_delete(*timerid);
2N/A else
2N/A timer_tcd[*timerid] = tcdp;
2N/A }
2N/A if (rc != 0)
2N/A free_sigev_handler(tcdp);
2N/A }
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/Aint
2N/Atimer_delete(timer_t timerid)
2N/A{
2N/A int rc;
2N/A
2N/A if ((rc = del_sigev_timer(timerid)) == 0)
2N/A return (__timer_delete(timerid));
2N/A else
2N/A return (rc);
2N/A}
2N/A
2N/Aint
2N/Atimer_getoverrun(timer_t timerid)
2N/A{
2N/A return (__timer_getoverrun(timerid) + sigev_timer_getoverrun(timerid));
2N/A}
2N/A
2N/Aint
2N/Atimer_gettime(timer_t timerid, itimerspec_t *value)
2N/A{
2N/A return (__timer_gettime(timerid, value));
2N/A}
2N/A
2N/Aint
2N/Atimer_settime(timer_t timerid, int flags, const itimerspec_t *value,
2N/A itimerspec_t *ovalue)
2N/A{
2N/A return (__timer_settime(timerid, flags, value, ovalue));
2N/A}
2N/A
2N/A/*
2N/A * Cleanup after fork1() in the child process.
2N/A */
2N/Avoid
2N/Apostfork1_child_sigev_timer(void)
2N/A{
2N/A thread_communication_data_t *tcdp;
2N/A int timer;
2N/A
2N/A for (timer = 0; timer < timer_max; timer++) {
2N/A if ((tcdp = timer_tcd[timer]) != NULL) {
2N/A timer_tcd[timer] = NULL;
2N/A tcd_teardown(tcdp);
2N/A }
2N/A }
2N/A}