1 /* Tests for POSIX timer implementation.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, see <http://www.gnu.org/licenses/>. */
29 notify_func1 (union sigval sigval
)
31 puts ("notify_func1");
36 notify_func2 (union sigval sigval
)
38 puts ("notify_func2");
45 static const char text
[] = "signal_func\n";
46 signal (sig
, signal_func
);
47 write (STDOUT_FILENO
, text
, sizeof text
- 1);
58 while (nanosleep (&ts
, &ts
) == -1 && errno
== EINTR
)
69 timer_t timer_sig
, timer_thr1
, timer_thr2
;
71 struct sigevent sigev1
=
73 .sigev_notify
= SIGEV_SIGNAL
,
74 .sigev_signo
= ZSIGALRM
76 struct sigevent sigev2
;
77 struct itimerspec itimer1
= { { 0, 200000000 }, { 0, 200000000 } };
78 struct itimerspec itimer2
= { { 0, 100000000 }, { 0, 500000000 } };
79 struct itimerspec itimer3
= { { 0, 150000000 }, { 0, 300000000 } };
80 struct itimerspec old
;
82 retval
= clock_gettime (CLOCK_REALTIME
, &ts
);
84 sigev2
.sigev_notify
= SIGEV_THREAD
;
85 sigev2
.sigev_notify_function
= notify_func1
;
86 sigev2
.sigev_notify_attributes
= NULL
;
87 /* It is unnecessary to do the following but to set a good example
89 sigev2
.sigev_value
.sival_ptr
= NULL
;
91 setvbuf (stdout
, 0, _IOLBF
, 0);
93 printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
94 retval
, ts
.tv_sec
, ts
.tv_nsec
);
96 retval
= clock_getres (CLOCK_REALTIME
, &ts
);
98 printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
99 retval
, ts
.tv_sec
, ts
.tv_nsec
);
101 if (timer_create (CLOCK_REALTIME
, &sigev1
, &timer_sig
) != 0)
103 printf ("timer_create for timer_sig failed: %m\n");
106 if (timer_create (CLOCK_REALTIME
, &sigev2
, &timer_thr1
) != 0)
108 printf ("timer_create for timer_thr1 failed: %m\n");
111 sigev2
.sigev_notify_function
= notify_func2
;
112 if (timer_create (CLOCK_REALTIME
, &sigev2
, &timer_thr2
) != 0)
114 printf ("timer_create for timer_thr2 failed: %m\n");
118 if (timer_settime (timer_thr1
, 0, &itimer2
, &old
) != 0)
120 printf ("timer_settime for timer_thr1 failed: %m\n");
123 if (timer_settime (timer_thr2
, 0, &itimer3
, &old
) != 0)
125 printf ("timer_settime for timer_thr2 failed: %m\n");
129 signal (ZSIGALRM
, signal_func
);
131 if (timer_settime (timer_sig
, 0, &itimer1
, &old
) != 0)
133 printf ("timer_settime for timer_sig failed: %m\n");
139 if (timer_delete (timer_sig
) != 0)
141 printf ("timer_delete for timer_sig failed: %m\n");
144 if (timer_delete (timer_thr1
) != 0)
146 printf ("timer_delete for timer_thr1 failed: %m\n");
152 if (timer_delete (timer_thr2
) != 0)
154 printf ("timer_delete for timer_thr2 failed: %m\n");