1 /* Tests for POSIX timer implementation.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
27 #include <support/xunistd.h>
30 notify_func1 (union sigval sigval
)
32 puts ("notify_func1");
37 notify_func2 (union sigval sigval
)
39 puts ("notify_func2");
46 static const char text
[] = "signal_func\n";
47 signal (sig
, signal_func
);
48 xwrite (STDOUT_FILENO
, text
, sizeof text
- 1);
59 while (nanosleep (&ts
, &ts
) == -1 && errno
== EINTR
)
70 timer_t timer_sig
, timer_thr1
, timer_thr2
;
72 struct sigevent sigev1
=
74 .sigev_notify
= SIGEV_SIGNAL
,
75 .sigev_signo
= ZSIGALRM
77 struct sigevent sigev2
;
78 struct itimerspec itimer1
= { { 0, 200000000 }, { 0, 200000000 } };
79 struct itimerspec itimer2
= { { 0, 100000000 }, { 0, 500000000 } };
80 struct itimerspec itimer3
= { { 0, 150000000 }, { 0, 300000000 } };
81 struct itimerspec old
;
83 retval
= clock_gettime (CLOCK_REALTIME
, &ts
);
85 sigev2
.sigev_notify
= SIGEV_THREAD
;
86 sigev2
.sigev_notify_function
= notify_func1
;
87 sigev2
.sigev_notify_attributes
= NULL
;
88 /* It is unnecessary to do the following but to set a good example
90 sigev2
.sigev_value
.sival_ptr
= NULL
;
92 setvbuf (stdout
, 0, _IOLBF
, 0);
94 printf ("clock_gettime returned %d, timespec = { %jd, %jd }\n",
95 retval
, (intmax_t) ts
.tv_sec
, (intmax_t) ts
.tv_nsec
);
97 retval
= clock_getres (CLOCK_REALTIME
, &ts
);
99 printf ("clock_getres returned %d, timespec = { %jd, %jd }\n",
100 retval
, (intmax_t) ts
.tv_sec
, (intmax_t) ts
.tv_nsec
);
102 if (timer_create (CLOCK_REALTIME
, &sigev1
, &timer_sig
) != 0)
104 printf ("timer_create for timer_sig failed: %m\n");
107 if (timer_create (CLOCK_REALTIME
, &sigev2
, &timer_thr1
) != 0)
109 printf ("timer_create for timer_thr1 failed: %m\n");
112 sigev2
.sigev_notify_function
= notify_func2
;
113 if (timer_create (CLOCK_REALTIME
, &sigev2
, &timer_thr2
) != 0)
115 printf ("timer_create for timer_thr2 failed: %m\n");
119 if (timer_settime (timer_thr1
, 0, &itimer2
, &old
) != 0)
121 printf ("timer_settime for timer_thr1 failed: %m\n");
124 if (timer_settime (timer_thr2
, 0, &itimer3
, &old
) != 0)
126 printf ("timer_settime for timer_thr2 failed: %m\n");
130 signal (ZSIGALRM
, signal_func
);
132 if (timer_settime (timer_sig
, 0, &itimer1
, &old
) != 0)
134 printf ("timer_settime for timer_sig failed: %m\n");
140 if (timer_delete (timer_sig
) != 0)
142 printf ("timer_delete for timer_sig failed: %m\n");
145 if (timer_delete (timer_thr1
) != 0)
147 printf ("timer_delete for timer_thr1 failed: %m\n");
153 if (timer_delete (timer_thr2
) != 0)
155 printf ("timer_delete for timer_thr2 failed: %m\n");