• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * Licensed under the GNU GPLv2 or later.
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19  /*
20   * Block several processes on a mutex, then wake them up.
21   */
22 
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 
27 #include "test.h"
28 #include "safe_macros.h"
29 #include "futextest.h"
30 
31 const char *TCID="futex_wake03";
32 const int TST_TOTAL=11;
33 
34 static futex_t *futex;
35 
do_child(void)36 static void do_child(void)
37 {
38 	futex_wait(futex, *futex, NULL, 0);
39 	exit(0);
40 }
41 
do_wake(int nr_children)42 static void do_wake(int nr_children)
43 {
44 	int res, i, cnt;
45 
46 	res = futex_wake(futex, nr_children, 0);
47 
48 	if (res != nr_children) {
49 		tst_resm(TFAIL,
50 		         "futex_wake() woken up %i children, expected %i",
51 		         res, nr_children);
52 		return;
53 	}
54 
55 	for (cnt = 0, i = 0; i < 100000; i++) {
56 		while (waitpid(-1, &res, WNOHANG) > 0)
57 			cnt++;
58 
59 		if (cnt == nr_children)
60 			break;
61 
62 		usleep(100);
63 	}
64 
65 	if (cnt != nr_children) {
66 		tst_resm(TFAIL, "reaped only %i childs, expected %i",
67 		         cnt, nr_children);
68 	} else {
69 		tst_resm(TPASS, "futex_wake() woken up %i childs", cnt);
70 	}
71 }
72 
verify_futex_wake(void)73 static void verify_futex_wake(void)
74 {
75 	int i, res;
76 	pid_t pids[55];
77 
78 	for (i = 0; i < (int)ARRAY_SIZE(pids); i++) {
79 		pids[i] = tst_fork();
80 
81 		switch (pids[i]) {
82 		case -1:
83 			tst_brkm(TBROK | TERRNO, NULL, "fork()");
84 		case 0:
85 			do_child();
86 		default:
87 		break;
88 		}
89 	}
90 
91 	for (i = 0; i < (int)ARRAY_SIZE(pids); i++)
92 		tst_process_state_wait2(pids[i], 'S');
93 
94 	for (i = 1; i <= 10; i++)
95 		do_wake(i);
96 
97 	res = futex_wake(futex, 1, 0);
98 
99 	if (res) {
100 		tst_resm(TFAIL, "futex_wake() woken up %u, none were waiting",
101 		         res);
102 	} else {
103 		tst_resm(TPASS, "futex_wake() woken up 0 children");
104 	}
105 }
106 
setup(void)107 static void setup(void)
108 {
109 	futex = SAFE_MMAP(NULL, NULL, sizeof(*futex), PROT_READ | PROT_WRITE,
110 			  MAP_ANONYMOUS | MAP_SHARED, -1, 0);
111 
112 	*futex = FUTEX_INITIALIZER;
113 }
114 
main(int argc,char * argv[])115 int main(int argc, char *argv[])
116 {
117 	int lc;
118 
119 	tst_parse_opts(argc, argv, NULL, NULL);
120 
121 	setup();
122 
123 	for (lc = 0; TEST_LOOPING(lc); lc++)
124 		verify_futex_wake();
125 
126 	tst_exit();
127 }
128