t_timeleft.c revision 1.4 1 1.4 christos /* $NetBSD: t_timeleft.c,v 1.4 2019/01/14 00:23:43 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos __COPYRIGHT("@(#) Copyright (c) 2008\
34 1.1 christos The NetBSD Foundation, inc. All rights reserved.");
35 1.4 christos __RCSID("$NetBSD: t_timeleft.c,v 1.4 2019/01/14 00:23:43 christos Exp $");
36 1.1 christos
37 1.1 christos #include <sys/types.h>
38 1.1 christos #include <sys/select.h>
39 1.1 christos
40 1.1 christos #include <atf-c.h>
41 1.1 christos #include <time.h>
42 1.1 christos #include <errno.h>
43 1.1 christos #include <lwp.h>
44 1.1 christos #include <signal.h>
45 1.1 christos #include <pthread.h>
46 1.1 christos #include <stdio.h>
47 1.1 christos #include <sched.h>
48 1.1 christos #include <unistd.h>
49 1.1 christos
50 1.1 christos static void
51 1.1 christos sighandler(int signo __unused)
52 1.1 christos {
53 1.1 christos }
54 1.1 christos
55 1.1 christos struct info {
56 1.1 christos void (*fun)(struct timespec *);
57 1.1 christos struct timespec ts;
58 1.1 christos };
59 1.1 christos
60 1.1 christos static void
61 1.1 christos timeleft__lwp_park(struct timespec *ts)
62 1.1 christos {
63 1.1 christos ATF_REQUIRE_ERRNO(EINTR, _lwp_park(CLOCK_MONOTONIC, TIMER_RELTIME,
64 1.1 christos ts, 0, ts, NULL) == -1);
65 1.1 christos }
66 1.1 christos
67 1.1 christos #if 0
68 1.1 christos static void
69 1.1 christos timeleft_pselect(struct timespec *ts)
70 1.1 christos {
71 1.1 christos ATF_REQUIRE_ERRNO(EINTR, pselect(1, NULL, NULL, NULL, ts, NULL));
72 1.1 christos }
73 1.1 christos #endif
74 1.1 christos
75 1.1 christos static void *
76 1.1 christos runner(void *arg)
77 1.1 christos {
78 1.1 christos struct info *i = arg;
79 1.1 christos (*i->fun)(&i->ts);
80 1.1 christos return NULL;
81 1.1 christos }
82 1.1 christos
83 1.1 christos static void
84 1.1 christos tester(void (*fun)(struct timespec *))
85 1.1 christos {
86 1.1 christos const struct timespec ts = { 5, 0 };
87 1.3 christos const struct timespec sts = { 1, 0 };
88 1.1 christos struct info i = { fun, ts };
89 1.1 christos pthread_t thr;
90 1.1 christos
91 1.1 christos ATF_REQUIRE(signal(SIGINT, sighandler) == 0);
92 1.1 christos ATF_REQUIRE(pthread_create(&thr, NULL, runner, &i) == 0);
93 1.1 christos
94 1.4 christos ATF_REQUIRE(nanosleep(&sts, NULL) == 0);
95 1.4 christos ATF_REQUIRE(pthread_kill(thr, SIGINT) == 0);
96 1.4 christos ATF_REQUIRE(pthread_join(thr, NULL) == 0);
97 1.1 christos printf("Orig time %ju.%lu\n", (intmax_t)ts.tv_sec, ts.tv_nsec);
98 1.1 christos printf("Time left %ju.%lu\n", (intmax_t)i.ts.tv_sec, i.ts.tv_nsec);
99 1.1 christos ATF_REQUIRE(timespeccmp(&i.ts, &ts, <));
100 1.1 christos }
101 1.1 christos
102 1.1 christos ATF_TC(timeleft__lwp_park);
103 1.1 christos ATF_TC_HEAD(timeleft__lwp_park, tc)
104 1.1 christos {
105 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks that _lwp_park(2) returns "
106 1.1 christos "the time left to sleep after interrupted");
107 1.1 christos }
108 1.1 christos
109 1.1 christos ATF_TC_BODY(timeleft__lwp_park, tc)
110 1.1 christos {
111 1.1 christos tester(timeleft__lwp_park);
112 1.1 christos }
113 1.1 christos
114 1.1 christos #if 0
115 1.1 christos ATF_TC(timeleft_pselect);
116 1.1 christos ATF_TC_HEAD(timeleft_pselect, tc)
117 1.1 christos {
118 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks that pselect(2) returns "
119 1.1 christos "the time left to sleep after interrupted");
120 1.1 christos }
121 1.1 christos
122 1.1 christos ATF_TC_BODY(timeleft_pselect, tc)
123 1.1 christos {
124 1.1 christos tester(timeleft_pselect);
125 1.1 christos }
126 1.1 christos #endif
127 1.1 christos
128 1.1 christos ATF_TP_ADD_TCS(tp)
129 1.1 christos {
130 1.1 christos
131 1.1 christos ATF_TP_ADD_TC(tp, timeleft__lwp_park);
132 1.1 christos #if 0
133 1.1 christos ATF_TP_ADD_TC(tp, timeleft_pselect);
134 1.1 christos #endif
135 1.1 christos
136 1.1 christos return atf_no_error();
137 1.1 christos }
138