.
[glibc/history.git] / rt / tst-cputimer1.c
blob8f5dd76cf2ab7010c3aeaae0bdc4f687c6fe6537
1 /* Tests for POSIX timer implementation using process CPU clock. */
3 #include <unistd.h>
5 #if _POSIX_THREADS && defined _POSIX_CPUTIME
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <time.h>
12 #include <pthread.h>
14 #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
15 #define TEST_CLOCK_MISSING(clock) \
16 (setup_test () ? "process CPU clock timer support" : NULL)
18 /* This function is intended to rack up both user and system time. */
19 static void *
20 chew_cpu (void *arg)
22 while (1)
24 static volatile char buf[4096];
25 for (int i = 0; i < 100; ++i)
26 for (size_t j = 0; j < sizeof buf; ++j)
27 buf[j] = 0xaa;
28 int nullfd = open ("/dev/null", O_WRONLY);
29 for (int i = 0; i < 100; ++i)
30 for (size_t j = 0; j < sizeof buf; ++j)
31 buf[j] = 0xbb;
32 write (nullfd, (char *) buf, sizeof buf);
33 close (nullfd);
36 return NULL;
39 static int
40 setup_test (void)
42 /* Test timers on our own process CPU clock by having a worker thread
43 eating CPU. First make sure we can make such timers at all. */
45 timer_t t;
46 if (timer_create (TEST_CLOCK, NULL, &t) != 0)
48 printf ("timer_create: %m\n");
49 return 1;
51 timer_delete (t);
53 pthread_t th;
54 int e = pthread_create (&th, NULL, chew_cpu, NULL);
55 if (e != 0)
57 printf ("pthread_create: %s\n", strerror (e));
58 exit (1);
61 return 0;
64 #else
65 # define TEST_CLOCK_MISSING(clock) "process clocks"
66 #endif
68 #include "tst-timer4.c"