1/* Tests for POSIX timer implementation using process CPU clock. */
2
3#include <unistd.h>
4
5#if _POSIX_THREADS && defined _POSIX_CPUTIME
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <fcntl.h>
11#include <time.h>
12#include <pthread.h>
13
14#define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
15#define TEST_CLOCK_MISSING(clock) \
16 (setup_test () ? "process CPU clock timer support" : NULL)
17
18/* This function is intended to rack up both user and system time. */
19static void *
20chew_cpu (void *arg)
21{
22 while (1)
23 {
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 (file: "/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 (fd: nullfd);
34 }
35
36 return NULL;
37}
38
39static int
40setup_test (void)
41{
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. */
44
45 timer_t t;
46 if (timer_create (TEST_CLOCK, NULL, timerid: &t) != 0)
47 {
48 printf (format: "timer_create: %m\n");
49 return 1;
50 }
51 timer_delete (timerid: t);
52
53 pthread_t th;
54 int e = pthread_create (newthread: &th, NULL, start_routine: chew_cpu, NULL);
55 if (e != 0)
56 {
57 printf (format: "pthread_create: %s\n", strerror (errnum: e));
58 exit (1);
59 }
60
61 return 0;
62}
63
64#else
65# define TEST_CLOCK_MISSING(clock) "process clocks"
66#endif
67
68#include "tst-timer4.c"
69

source code of glibc/rt/tst-cputimer1.c