1 | /* Tests for POSIX timer implementation using thread CPU clock. */ |
2 | |
3 | #include <unistd.h> |
4 | |
5 | #if _POSIX_THREADS && defined _POSIX_CPUTIME |
6 | |
7 | #include <errno.h> |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | #include <fcntl.h> |
12 | #include <time.h> |
13 | #include <pthread.h> |
14 | |
15 | #include <support/xunistd.h> |
16 | |
17 | static clockid_t worker_thread_clock; |
18 | |
19 | #define TEST_CLOCK worker_thread_clock |
20 | #define TEST_CLOCK_MISSING(clock) \ |
21 | (setup_test () ? "thread CPU clock timer support" : NULL) |
22 | |
23 | /* This function is intended to rack up both user and system time. */ |
24 | static void * |
25 | chew_cpu (void *arg) |
26 | { |
27 | while (1) |
28 | { |
29 | static volatile char buf[4096]; |
30 | for (int i = 0; i < 100; ++i) |
31 | for (size_t j = 0; j < sizeof buf; ++j) |
32 | buf[j] = 0xaa; |
33 | int nullfd = open (file: "/dev/null" , O_WRONLY); |
34 | for (int i = 0; i < 100; ++i) |
35 | for (size_t j = 0; j < sizeof buf; ++j) |
36 | buf[j] = 0xbb; |
37 | xwrite (nullfd, (char *) buf, sizeof buf); |
38 | close (fd: nullfd); |
39 | } |
40 | |
41 | return NULL; |
42 | } |
43 | |
44 | static int |
45 | setup_test (void) |
46 | { |
47 | /* Test timers on a thread CPU clock by having a worker thread eating |
48 | CPU. First make sure we can make such timers at all. */ |
49 | |
50 | pthread_t th; |
51 | int e = pthread_create (newthread: &th, NULL, start_routine: chew_cpu, NULL); |
52 | if (e != 0) |
53 | { |
54 | printf (format: "pthread_create: %s\n" , strerror (errnum: e)); |
55 | exit (1); |
56 | } |
57 | |
58 | e = pthread_getcpuclockid (thread_id: th, clock_id: &worker_thread_clock); |
59 | if (e == EPERM || e == ENOENT || e == ENOTSUP) |
60 | { |
61 | puts (s: "pthread_getcpuclockid does not support other threads" ); |
62 | return 1; |
63 | } |
64 | if (e != 0) |
65 | { |
66 | printf (format: "pthread_getcpuclockid: %s\n" , strerror (errnum: e)); |
67 | exit (1); |
68 | } |
69 | |
70 | timer_t t; |
71 | if (timer_create (TEST_CLOCK, NULL, timerid: &t) != 0) |
72 | { |
73 | printf (format: "timer_create: %m\n" ); |
74 | return 1; |
75 | } |
76 | timer_delete (timerid: t); |
77 | |
78 | return 0; |
79 | } |
80 | |
81 | #else |
82 | # define TEST_CLOCK_MISSING(clock) "process clocks" |
83 | #endif |
84 | |
85 | #include "tst-timer4.c" |
86 | |