These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / tools / testing / selftests / futex / functional / futex_wait_private_mapped_file.c
1 /******************************************************************************
2  *
3  * Copyright FUJITSU LIMITED 2010
4  * Copyright KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
5  *
6  *   This program is free software;  you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  * DESCRIPTION
12  *      Internally, Futex has two handling mode, anon and file. The private file
13  *      mapping is special. At first it behave as file, but after write anything
14  *      it behave as anon. This test is intent to test such case.
15  *
16  * AUTHOR
17  *      KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
18  *
19  * HISTORY
20  *      2010-Jan-6: Initial version by KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
21  *
22  *****************************************************************************/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <syscall.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <linux/futex.h>
30 #include <pthread.h>
31 #include <libgen.h>
32 #include <signal.h>
33
34 #include "logging.h"
35 #include "futextest.h"
36
37 #define PAGE_SZ 4096
38
39 char pad[PAGE_SZ] = {1};
40 futex_t val = 1;
41 char pad2[PAGE_SZ] = {1};
42
43 #define WAKE_WAIT_US 3000000
44 struct timespec wait_timeout = { .tv_sec = 5, .tv_nsec = 0};
45
46 void usage(char *prog)
47 {
48         printf("Usage: %s\n", prog);
49         printf("  -c    Use color\n");
50         printf("  -h    Display this help message\n");
51         printf("  -v L  Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
52                VQUIET, VCRITICAL, VINFO);
53 }
54
55 void *thr_futex_wait(void *arg)
56 {
57         int ret;
58
59         info("futex wait\n");
60         ret = futex_wait(&val, 1, &wait_timeout, 0);
61         if (ret && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
62                 error("futex error.\n", errno);
63                 print_result(RET_ERROR);
64                 exit(RET_ERROR);
65         }
66
67         if (ret && errno == ETIMEDOUT)
68                 fail("waiter timedout\n");
69
70         info("futex_wait: ret = %d, errno = %d\n", ret, errno);
71
72         return NULL;
73 }
74
75 int main(int argc, char **argv)
76 {
77         pthread_t thr;
78         int ret = RET_PASS;
79         int res;
80         int c;
81
82         while ((c = getopt(argc, argv, "chv:")) != -1) {
83                 switch (c) {
84                 case 'c':
85                         log_color(1);
86                         break;
87                 case 'h':
88                         usage(basename(argv[0]));
89                         exit(0);
90                 case 'v':
91                         log_verbosity(atoi(optarg));
92                         break;
93                 default:
94                         usage(basename(argv[0]));
95                         exit(1);
96                 }
97         }
98
99         printf("%s: Test the futex value of private file mappings in FUTEX_WAIT\n",
100                basename(argv[0]));
101
102         ret = pthread_create(&thr, NULL, thr_futex_wait, NULL);
103         if (ret < 0) {
104                 fprintf(stderr, "pthread_create error\n");
105                 ret = RET_ERROR;
106                 goto out;
107         }
108
109         info("wait a while\n");
110         usleep(WAKE_WAIT_US);
111         val = 2;
112         res = futex_wake(&val, 1, 0);
113         info("futex_wake %d\n", res);
114         if (res != 1) {
115                 fail("FUTEX_WAKE didn't find the waiting thread.\n");
116                 ret = RET_FAIL;
117         }
118
119         info("join\n");
120         pthread_join(thr, NULL);
121
122  out:
123         print_result(ret);
124         return ret;
125 }