xref: /illumos-gate/usr/src/lib/libc/port/rt/clock_timer.c (revision 66e150d7d3c0cb2de3c45c74612784ffd3e73de6)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include <time.h>
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include "sigev_thread.h"
36 
37 /*
38  * System call wrappers found elsewhere in libc (common/sys/__clock_timer.s).
39  */
40 extern int __clock_getres(clockid_t, timespec_t *);
41 extern int __clock_gettime(clockid_t, timespec_t *);
42 extern int __clock_settime(clockid_t, const timespec_t *);
43 extern int __timer_create(clockid_t, struct sigevent *, timer_t *);
44 extern int __timer_delete(timer_t);
45 extern int __timer_getoverrun(timer_t);
46 extern int __timer_gettime(timer_t, itimerspec_t *);
47 extern int __timer_settime(timer_t, int, const itimerspec_t *, itimerspec_t *);
48 
49 /*
50  * Array of pointers to tcd's, indexed by timer id.
51  * No more than 'timer_max' timers can be created by any process.
52  */
53 int timer_max = 0;
54 thread_communication_data_t **timer_tcd;
55 static pthread_once_t timer_once = PTHREAD_ONCE_INIT;
56 
57 static void
58 timer_init(void)
59 {
60 	timer_max = (int)_sysconf(_SC_TIMER_MAX);
61 	timer_tcd = malloc(timer_max * sizeof (*timer_tcd));
62 	(void) memset(timer_tcd, 0, timer_max * sizeof (*timer_tcd));
63 }
64 
65 int
66 clock_getres(clockid_t clock_id, timespec_t *res)
67 {
68 	return (__clock_getres(clock_id, res));
69 }
70 
71 int
72 clock_gettime(clockid_t clock_id, timespec_t *tp)
73 {
74 	return (__clock_gettime(clock_id, tp));
75 }
76 
77 int
78 clock_settime(clockid_t clock_id, const timespec_t *tp)
79 {
80 	return (__clock_settime(clock_id, tp));
81 }
82 
83 int
84 timer_create(clockid_t clock_id, struct sigevent *sigevp, timer_t *timerid)
85 {
86 	struct sigevent sigevent;
87 	port_notify_t port_notify;
88 	thread_communication_data_t *tcdp;
89 	int sigev_thread = 0;
90 	int rc;
91 
92 	(void) pthread_once(&timer_once, timer_init);
93 
94 	if (sigevp != NULL &&
95 	    sigevp->sigev_notify == SIGEV_THREAD &&
96 	    sigevp->sigev_notify_function != NULL) {
97 		sigev_thread = 1;
98 		tcdp = setup_sigev_handler(sigevp, TIMER);
99 		if (tcdp == NULL)
100 			return (-1);
101 		/* copy the sigevent structure so we can modify it */
102 		sigevent = *sigevp;
103 		sigevp = &sigevent;
104 		port_notify.portnfy_port = tcdp->tcd_port;
105 		port_notify.portnfy_user = NULL;
106 		sigevp->sigev_value.sival_ptr = &port_notify;
107 	}
108 
109 	rc = __timer_create(clock_id, sigevp, timerid);
110 
111 	if (sigev_thread) {
112 		if (rc == 0) {
113 			if ((rc = launch_spawner(tcdp)) != 0)
114 				__timer_delete(*timerid);
115 			else
116 				timer_tcd[*timerid] = tcdp;
117 		}
118 		if (rc != 0)
119 			free_sigev_handler(tcdp);
120 	}
121 
122 	return (rc);
123 }
124 
125 int
126 timer_delete(timer_t timerid)
127 {
128 	int rc;
129 
130 	if ((rc = del_sigev_timer(timerid)) == 0)
131 		return (__timer_delete(timerid));
132 	else
133 		return (rc);
134 }
135 
136 int
137 timer_getoverrun(timer_t timerid)
138 {
139 	return (__timer_getoverrun(timerid) + sigev_timer_getoverrun(timerid));
140 }
141 
142 int
143 timer_gettime(timer_t timerid, itimerspec_t *value)
144 {
145 	return (__timer_gettime(timerid, value));
146 }
147 
148 int
149 timer_settime(timer_t timerid, int flags, const itimerspec_t *value,
150 	itimerspec_t *ovalue)
151 {
152 	return (__timer_settime(timerid, flags, value, ovalue));
153 }
154 
155 /*
156  * Cleanup after fork1() in the child process.
157  */
158 void
159 postfork1_child_sigev_timer(void)
160 {
161 	thread_communication_data_t *tcdp;
162 	int timer;
163 
164 	for (timer = 0; timer < timer_max; timer++) {
165 		if ((tcdp = timer_tcd[timer]) != NULL) {
166 			timer_tcd[timer] = NULL;
167 			tcd_teardown(tcdp);
168 		}
169 	}
170 }
171