Home | History | Annotate | Line # | Download | only in sys
t_futex_ops.c revision 1.2
      1  1.2  riastrad /* $NetBSD: t_futex_ops.c,v 1.2 2020/04/28 17:27:03 riastradh Exp $ */
      2  1.1   thorpej 
      3  1.1   thorpej /*-
      4  1.1   thorpej  * Copyright (c) 2019, 2020 The NetBSD Foundation, Inc.
      5  1.1   thorpej  * All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1   thorpej  * modification, are permitted provided that the following conditions
      9  1.1   thorpej  * are met:
     10  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1   thorpej  *
     16  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1   thorpej  */
     28  1.1   thorpej 
     29  1.1   thorpej #include <sys/cdefs.h>
     30  1.1   thorpej __COPYRIGHT("@(#) Copyright (c) 2019, 2020\
     31  1.1   thorpej  The NetBSD Foundation, inc. All rights reserved.");
     32  1.2  riastrad __RCSID("$NetBSD: t_futex_ops.c,v 1.2 2020/04/28 17:27:03 riastradh Exp $");
     33  1.1   thorpej 
     34  1.1   thorpej #include <sys/fcntl.h>
     35  1.1   thorpej #include <sys/mman.h>
     36  1.1   thorpej #include <sys/wait.h>
     37  1.1   thorpej #include <atomic.h>
     38  1.1   thorpej #include <errno.h>
     39  1.1   thorpej #include <lwp.h>
     40  1.1   thorpej #include <stdlib.h>
     41  1.1   thorpej #include <stdio.h>
     42  1.1   thorpej #include <time.h>
     43  1.1   thorpej #include <limits.h>
     44  1.1   thorpej #include <unistd.h>
     45  1.1   thorpej 
     46  1.1   thorpej #include <atf-c.h>
     47  1.1   thorpej 
     48  1.1   thorpej #include <libc/include/futex_private.h>
     49  1.1   thorpej 
     50  1.1   thorpej #define	LOAD(x)		(*(volatile int *)(x))
     51  1.1   thorpej #define	STORE(x, y)	*(volatile int *)(x) = (y)
     52  1.1   thorpej 
     53  1.1   thorpej #if 0
     54  1.1   thorpej #define	DPRINTF(x)	printf x
     55  1.1   thorpej #else
     56  1.1   thorpej #define	DPRINTF(x)	__nothing
     57  1.1   thorpej #endif
     58  1.1   thorpej 
     59  1.1   thorpej #define	STACK_SIZE	65536
     60  1.1   thorpej 
     61  1.1   thorpej static volatile int futex_word;
     62  1.1   thorpej static volatile int futex_word1;
     63  1.1   thorpej 
     64  1.1   thorpej static volatile unsigned int nlwps_running;
     65  1.1   thorpej 
     66  1.1   thorpej struct lwp_data {
     67  1.1   thorpej 	ucontext_t	context;
     68  1.1   thorpej 	void		(*func)(void *);
     69  1.1   thorpej 	void		*stack_base;
     70  1.1   thorpej 	lwpid_t		lwpid;
     71  1.1   thorpej 	pid_t		child;
     72  1.1   thorpej 	lwpid_t		threadid;
     73  1.1   thorpej 	int		wait_op;
     74  1.1   thorpej 	int		op_flags;
     75  1.1   thorpej 	int		bitset;
     76  1.1   thorpej 	volatile int	*futex_ptr;
     77  1.1   thorpej 	volatile int	*error_ptr;
     78  1.1   thorpej 	int		block_val;
     79  1.1   thorpej 
     80  1.1   thorpej 	void		(*exit_func)(void);
     81  1.1   thorpej 
     82  1.1   thorpej 	int		futex_error;
     83  1.1   thorpej };
     84  1.1   thorpej 
     85  1.1   thorpej #define	WAITER_LWP0		0
     86  1.1   thorpej #define	WAITER_LWP1		1
     87  1.1   thorpej #define	WAITER_LWP2		2
     88  1.1   thorpej #define	WAITER_LWP3		3
     89  1.1   thorpej #define	WAITER_LWP4		4
     90  1.1   thorpej #define	WAITER_LWP5		5
     91  1.1   thorpej #define	NLWPS			6
     92  1.1   thorpej 
     93  1.1   thorpej struct lwp_data lwp_data[NLWPS];
     94  1.1   thorpej 
     95  1.1   thorpej static const char *bs_path = "t_futex_ops_backing_store";
     96  1.1   thorpej static int bs_fd = -1;
     97  1.1   thorpej static int *bs_addr = MAP_FAILED;
     98  1.1   thorpej static void *bs_source_buffer = NULL;
     99  1.1   thorpej static void *bs_verify_buffer = NULL;
    100  1.1   thorpej static long bs_pagesize;
    101  1.1   thorpej 
    102  1.1   thorpej static void
    103  1.1   thorpej create_lwp_waiter(struct lwp_data *d)
    104  1.1   thorpej {
    105  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&d->context, 0, &d->lwpid) == 0);
    106  1.1   thorpej }
    107  1.1   thorpej 
    108  1.1   thorpej static void
    109  1.1   thorpej exit_lwp_waiter(void)
    110  1.1   thorpej {
    111  1.1   thorpej 	_lwp_exit();
    112  1.1   thorpej }
    113  1.1   thorpej 
    114  1.1   thorpej static void
    115  1.1   thorpej reap_lwp_waiter(struct lwp_data *d)
    116  1.1   thorpej {
    117  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(d->lwpid, NULL) == 0);
    118  1.1   thorpej }
    119  1.1   thorpej 
    120  1.1   thorpej static void
    121  1.1   thorpej create_proc_waiter(struct lwp_data *d)
    122  1.1   thorpej {
    123  1.1   thorpej 	pid_t pid;
    124  1.1   thorpej 
    125  1.1   thorpej 	ATF_REQUIRE((pid = fork()) != -1);
    126  1.1   thorpej 	if (pid == 0) {
    127  1.1   thorpej 		(*d->func)(d);
    128  1.1   thorpej 		_exit(666);		/* backstop */
    129  1.1   thorpej 	} else
    130  1.1   thorpej 		d->child = pid;
    131  1.1   thorpej }
    132  1.1   thorpej 
    133  1.1   thorpej static void
    134  1.1   thorpej exit_proc_waiter(void)
    135  1.1   thorpej {
    136  1.1   thorpej 	_exit(0);
    137  1.1   thorpej }
    138  1.1   thorpej 
    139  1.1   thorpej static void
    140  1.1   thorpej reap_proc_waiter(struct lwp_data *d)
    141  1.1   thorpej {
    142  1.1   thorpej 	int status;
    143  1.1   thorpej 
    144  1.1   thorpej 	ATF_REQUIRE(waitpid(d->child, &status, 0) == d->child);
    145  1.1   thorpej 	ATF_REQUIRE(WIFEXITED(status));
    146  1.1   thorpej 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
    147  1.1   thorpej }
    148  1.1   thorpej 
    149  1.1   thorpej static void
    150  1.1   thorpej setup_lwp_context(struct lwp_data *d, void (*func)(void *))
    151  1.1   thorpej {
    152  1.1   thorpej 
    153  1.1   thorpej 	memset(d, 0, sizeof(*d));
    154  1.1   thorpej 	d->stack_base = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
    155  1.1   thorpej 	    MAP_ANON | MAP_STACK | MAP_PRIVATE, -1, 0);
    156  1.1   thorpej 	ATF_REQUIRE(d->stack_base != MAP_FAILED);
    157  1.1   thorpej 	_lwp_makecontext(&d->context, func, d, NULL, d->stack_base, STACK_SIZE);
    158  1.1   thorpej 	d->threadid = 0;
    159  1.1   thorpej 	d->func = func;
    160  1.1   thorpej }
    161  1.1   thorpej 
    162  1.1   thorpej static void
    163  1.1   thorpej simple_test_waiter_lwp(void *arg)
    164  1.1   thorpej {
    165  1.1   thorpej 	struct lwp_data *d = arg;
    166  1.1   thorpej 
    167  1.1   thorpej 	d->threadid = _lwp_self();
    168  1.1   thorpej 
    169  1.1   thorpej 	atomic_inc_uint(&nlwps_running);
    170  1.1   thorpej 	membar_sync();
    171  1.1   thorpej 
    172  1.1   thorpej 	if (__futex(d->futex_ptr, d->wait_op | d->op_flags,
    173  1.1   thorpej 		    d->block_val, NULL, NULL, 0, d->bitset) == -1) {
    174  1.1   thorpej 		d->futex_error = errno;
    175  1.1   thorpej 		_lwp_exit();
    176  1.1   thorpej 	} else {
    177  1.1   thorpej 		d->futex_error = 0;
    178  1.1   thorpej 	}
    179  1.1   thorpej 
    180  1.1   thorpej 	membar_sync();
    181  1.1   thorpej 	atomic_dec_uint(&nlwps_running);
    182  1.1   thorpej 
    183  1.1   thorpej 	_lwp_exit();
    184  1.1   thorpej }
    185  1.1   thorpej 
    186  1.1   thorpej static bool
    187  1.1   thorpej verify_zero_bs(void)
    188  1.1   thorpej {
    189  1.1   thorpej 
    190  1.1   thorpej 	if (bs_verify_buffer == NULL) {
    191  1.1   thorpej 		bs_verify_buffer = malloc(bs_pagesize);
    192  1.1   thorpej 		ATF_REQUIRE(bs_verify_buffer != NULL);
    193  1.1   thorpej 	}
    194  1.1   thorpej 
    195  1.1   thorpej 	ATF_REQUIRE(pread(bs_fd, bs_verify_buffer,
    196  1.1   thorpej 			  bs_pagesize, 0) == bs_pagesize);
    197  1.1   thorpej 
    198  1.1   thorpej 	return (memcmp(bs_verify_buffer, bs_source_buffer, bs_pagesize) == 0);
    199  1.1   thorpej }
    200  1.1   thorpej 
    201  1.1   thorpej static void
    202  1.1   thorpej create_bs(int map_flags)
    203  1.1   thorpej {
    204  1.1   thorpej 
    205  1.1   thorpej 	bs_pagesize = sysconf(_SC_PAGESIZE);
    206  1.1   thorpej 	ATF_REQUIRE(bs_pagesize > 0);
    207  1.1   thorpej 
    208  1.1   thorpej 	if ((map_flags & (MAP_FILE | MAP_ANON)) == MAP_FILE) {
    209  1.1   thorpej 		bs_source_buffer = calloc(1, bs_pagesize);
    210  1.1   thorpej 		ATF_REQUIRE(bs_source_buffer != NULL);
    211  1.1   thorpej 
    212  1.1   thorpej 		bs_fd = open(bs_path, O_RDWR | O_CREAT | O_EXCL, 0644);
    213  1.1   thorpej 		ATF_REQUIRE(bs_fd != -1);
    214  1.1   thorpej 
    215  1.1   thorpej 		ATF_REQUIRE(pwrite(bs_fd, bs_source_buffer,
    216  1.1   thorpej 				   bs_pagesize, 0) == bs_pagesize);
    217  1.1   thorpej 		ATF_REQUIRE(verify_zero_bs());
    218  1.1   thorpej 	}
    219  1.1   thorpej 
    220  1.1   thorpej 	bs_addr = mmap(NULL, bs_pagesize, PROT_READ | PROT_WRITE,
    221  1.1   thorpej 		       map_flags | MAP_HASSEMAPHORE, bs_fd, 0);
    222  1.1   thorpej 	ATF_REQUIRE(bs_addr != MAP_FAILED);
    223  1.1   thorpej }
    224  1.1   thorpej 
    225  1.1   thorpej static void
    226  1.1   thorpej cleanup_bs(void)
    227  1.1   thorpej {
    228  1.1   thorpej 
    229  1.1   thorpej 	if (bs_fd != -1) {
    230  1.1   thorpej 		(void) close(bs_fd);
    231  1.1   thorpej 		bs_fd = -1;
    232  1.1   thorpej 		(void) unlink(bs_path);
    233  1.1   thorpej 	}
    234  1.1   thorpej 	if (bs_source_buffer != NULL) {
    235  1.1   thorpej 		free(bs_source_buffer);
    236  1.1   thorpej 		bs_source_buffer = NULL;
    237  1.1   thorpej 	}
    238  1.1   thorpej 	if (bs_verify_buffer != NULL) {
    239  1.1   thorpej 		free(bs_verify_buffer);
    240  1.1   thorpej 		bs_verify_buffer = NULL;
    241  1.1   thorpej 	}
    242  1.1   thorpej 	if (bs_addr != MAP_FAILED) {
    243  1.1   thorpej 		munmap(bs_addr, bs_pagesize);
    244  1.1   thorpej 		bs_addr = MAP_FAILED;
    245  1.1   thorpej 	}
    246  1.1   thorpej }
    247  1.1   thorpej 
    248  1.1   thorpej static void
    249  1.1   thorpej do_cleanup(void)
    250  1.1   thorpej {
    251  1.1   thorpej 	int i;
    252  1.1   thorpej 
    253  1.1   thorpej 	for (i = 0; i < NLWPS; i++) {
    254  1.1   thorpej 		struct lwp_data *d = &lwp_data[i];
    255  1.1   thorpej 		if (d->stack_base != NULL && d->stack_base != MAP_FAILED) {
    256  1.1   thorpej 			(void) munmap(d->stack_base, STACK_SIZE);
    257  1.1   thorpej 		}
    258  1.1   thorpej 	}
    259  1.1   thorpej 	memset(lwp_data, 0, sizeof(lwp_data));
    260  1.1   thorpej 	STORE(&futex_word, 0);
    261  1.1   thorpej 	STORE(&futex_word1, 0);
    262  1.1   thorpej 	nlwps_running = 0;
    263  1.1   thorpej 
    264  1.1   thorpej 	cleanup_bs();
    265  1.1   thorpej }
    266  1.1   thorpej 
    267  1.1   thorpej /*****************************************************************************/
    268  1.1   thorpej 
    269  1.1   thorpej static void
    270  1.1   thorpej wait_wake_test_waiter_lwp(void *arg)
    271  1.1   thorpej {
    272  1.1   thorpej 	struct lwp_data *d = arg;
    273  1.1   thorpej 
    274  1.1   thorpej 	d->threadid = _lwp_self();
    275  1.1   thorpej 
    276  1.1   thorpej 	STORE(d->futex_ptr, 1);
    277  1.1   thorpej 	membar_sync();
    278  1.1   thorpej 
    279  1.1   thorpej 	/* This will block because *futex_ptr == 1. */
    280  1.1   thorpej 	if (__futex(d->futex_ptr, FUTEX_WAIT | d->op_flags,
    281  1.1   thorpej 		    1, NULL, NULL, 0, 0) == -1) {
    282  1.1   thorpej 		STORE(d->error_ptr, errno);
    283  1.1   thorpej 		(*d->exit_func)();
    284  1.1   thorpej 	} else {
    285  1.1   thorpej 		STORE(d->error_ptr, 0);
    286  1.1   thorpej 	}
    287  1.1   thorpej 
    288  1.1   thorpej 	do {
    289  1.1   thorpej 		membar_sync();
    290  1.1   thorpej 		sleep(1);
    291  1.1   thorpej 	} while (LOAD(d->futex_ptr) != 0);
    292  1.1   thorpej 
    293  1.1   thorpej 	STORE(d->futex_ptr, 2);
    294  1.1   thorpej 	membar_sync();
    295  1.1   thorpej 
    296  1.1   thorpej 	do {
    297  1.1   thorpej 		membar_sync();
    298  1.1   thorpej 		sleep(1);
    299  1.1   thorpej 	} while (LOAD(d->futex_ptr) != 3);
    300  1.1   thorpej 
    301  1.1   thorpej 	/* This will not block because futex_word != 666. */
    302  1.1   thorpej 	if (__futex(d->futex_ptr, FUTEX_WAIT | d->op_flags,
    303  1.1   thorpej 		    666, NULL, NULL, 0, 0) == -1) {
    304  1.1   thorpej 		/* This SHOULD be EAGAIN. */
    305  1.1   thorpej 		STORE(d->error_ptr, errno);
    306  1.1   thorpej 	}
    307  1.1   thorpej 
    308  1.1   thorpej 	STORE(d->futex_ptr, 4);
    309  1.1   thorpej 	membar_sync();
    310  1.1   thorpej 
    311  1.1   thorpej 	(*d->exit_func)();
    312  1.1   thorpej }
    313  1.1   thorpej 
    314  1.1   thorpej static void
    315  1.1   thorpej do_futex_wait_wake_test(volatile int *futex_ptr, volatile int *error_ptr,
    316  1.1   thorpej 			void (*create_func)(struct lwp_data *),
    317  1.1   thorpej 			void (*exit_func)(void),
    318  1.1   thorpej 			void (*reap_func)(struct lwp_data *),
    319  1.1   thorpej 			int flags)
    320  1.1   thorpej {
    321  1.1   thorpej 	struct lwp_data *wlwp = &lwp_data[WAITER_LWP0];
    322  1.1   thorpej 	int tries;
    323  1.1   thorpej 
    324  1.1   thorpej 	if (error_ptr == NULL)
    325  1.1   thorpej 		error_ptr = &wlwp->futex_error;
    326  1.1   thorpej 
    327  1.1   thorpej 	if (create_func == NULL)
    328  1.1   thorpej 		create_func = create_lwp_waiter;
    329  1.1   thorpej 	if (exit_func == NULL)
    330  1.1   thorpej 		exit_func = exit_lwp_waiter;
    331  1.1   thorpej 	if (reap_func == NULL)
    332  1.1   thorpej 		reap_func = reap_lwp_waiter;
    333  1.1   thorpej 
    334  1.1   thorpej 	setup_lwp_context(wlwp, wait_wake_test_waiter_lwp);
    335  1.1   thorpej 
    336  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: testing with flags 0x%x\n", flags));
    337  1.1   thorpej 	wlwp->op_flags = flags;
    338  1.1   thorpej 	wlwp->error_ptr = error_ptr;
    339  1.1   thorpej 	STORE(error_ptr, -1);
    340  1.1   thorpej 	wlwp->futex_ptr = futex_ptr;
    341  1.1   thorpej 	STORE(futex_ptr, 0);
    342  1.1   thorpej 	wlwp->exit_func = exit_func;
    343  1.1   thorpej 	membar_sync();
    344  1.1   thorpej 
    345  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: creating watier LWP\n"));
    346  1.1   thorpej 	(*create_func)(wlwp);
    347  1.1   thorpej 
    348  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waiting for LWP %d to enter futex\n",
    349  1.1   thorpej 	    wlwp->lwpid));
    350  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    351  1.1   thorpej 		membar_sync();
    352  1.1   thorpej 		if (LOAD(futex_ptr) == 1)
    353  1.1   thorpej 			break;
    354  1.1   thorpej 		sleep(1);
    355  1.1   thorpej 	}
    356  1.1   thorpej 	membar_sync();
    357  1.1   thorpej 	ATF_REQUIRE(LOAD(futex_ptr) == 1);
    358  1.1   thorpej 
    359  1.1   thorpej 	/*
    360  1.1   thorpej 	 * If the LWP is blocked in the futex, it will not have yet
    361  1.1   thorpej 	 * modified *error_ptr.
    362  1.1   thorpej 	 */
    363  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: checking for successful wait (%d)\n",
    364  1.1   thorpej 	    LOAD(error_ptr)));
    365  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    366  1.1   thorpej 		membar_sync();
    367  1.1   thorpej 		if (LOAD(error_ptr) == -1)
    368  1.1   thorpej 			break;
    369  1.1   thorpej 		sleep(1);
    370  1.1   thorpej 	}
    371  1.1   thorpej 	membar_sync();
    372  1.1   thorpej 	ATF_REQUIRE(LOAD(error_ptr) == -1);
    373  1.1   thorpej 
    374  1.1   thorpej 	/* Make sure invalid #wakes in rejected. */
    375  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    376  1.1   thorpej 	    __futex(futex_ptr, FUTEX_WAKE | flags,
    377  1.1   thorpej 		    -1, NULL, NULL, 0, 0) == -1);
    378  1.1   thorpej 
    379  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waking 1 waiter\n"));
    380  1.1   thorpej 	ATF_REQUIRE(__futex(futex_ptr, FUTEX_WAKE | flags,
    381  1.1   thorpej 			    1, NULL, NULL, 0, 0) == 1);
    382  1.1   thorpej 
    383  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: checking for successful wake (%d)\n",
    384  1.1   thorpej 	    LOAD(error_ptr)));
    385  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    386  1.1   thorpej 		membar_sync();
    387  1.1   thorpej 		if (LOAD(error_ptr) == 0)
    388  1.1   thorpej 			break;
    389  1.1   thorpej 		sleep(1);
    390  1.1   thorpej 	}
    391  1.1   thorpej 	membar_sync();
    392  1.1   thorpej 	ATF_REQUIRE(LOAD(error_ptr) == 0);
    393  1.1   thorpej 
    394  1.1   thorpej 	STORE(futex_ptr, 0);
    395  1.1   thorpej 	membar_sync();
    396  1.1   thorpej 
    397  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waiting for LWP to advance (2)\n"));
    398  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    399  1.1   thorpej 		membar_sync();
    400  1.1   thorpej 		if (LOAD(futex_ptr) == 2)
    401  1.1   thorpej 			break;
    402  1.1   thorpej 		sleep(1);
    403  1.1   thorpej 	}
    404  1.1   thorpej 	membar_sync();
    405  1.1   thorpej 	ATF_REQUIRE(LOAD(futex_ptr) == 2);
    406  1.1   thorpej 
    407  1.1   thorpej 	STORE(futex_ptr, 3);
    408  1.1   thorpej 	membar_sync();
    409  1.1   thorpej 
    410  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: waiting for LWP to advance (4)\n"));
    411  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    412  1.1   thorpej 		membar_sync();
    413  1.1   thorpej 		if (LOAD(futex_ptr) == 4)
    414  1.1   thorpej 			break;
    415  1.1   thorpej 		sleep(1);
    416  1.1   thorpej 	}
    417  1.1   thorpej 	membar_sync();
    418  1.1   thorpej 	ATF_REQUIRE(LOAD(futex_ptr) == 4);
    419  1.1   thorpej 
    420  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: checking for expected EGAIN\n"));
    421  1.1   thorpej 	ATF_REQUIRE(LOAD(error_ptr) == EAGAIN);
    422  1.1   thorpej 
    423  1.1   thorpej 	DPRINTF(("futex_basic_wait_wake: reaping LWP %d\n", wlwp->lwpid));
    424  1.1   thorpej 	(*reap_func)(wlwp);
    425  1.1   thorpej }
    426  1.1   thorpej 
    427  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_basic_wait_wake_private);
    428  1.1   thorpej ATF_TC_HEAD(futex_basic_wait_wake_private, tc)
    429  1.1   thorpej {
    430  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    431  1.1   thorpej 	    "tests basic futex WAIT + WAKE operations (PRIVATE)");
    432  1.1   thorpej }
    433  1.1   thorpej ATF_TC_BODY(futex_basic_wait_wake_private, tc)
    434  1.1   thorpej {
    435  1.1   thorpej 	do_futex_wait_wake_test(&futex_word, NULL,
    436  1.1   thorpej 				NULL, NULL, NULL,
    437  1.1   thorpej 				FUTEX_PRIVATE_FLAG);
    438  1.1   thorpej }
    439  1.1   thorpej ATF_TC_CLEANUP(futex_basic_wait_wake_private, tc)
    440  1.1   thorpej {
    441  1.1   thorpej 	do_cleanup();
    442  1.1   thorpej }
    443  1.1   thorpej 
    444  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_basic_wait_wake_shared);
    445  1.1   thorpej ATF_TC_HEAD(futex_basic_wait_wake_shared, tc)
    446  1.1   thorpej {
    447  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    448  1.1   thorpej 	    "tests basic futex WAIT + WAKE operations (SHARED)");
    449  1.1   thorpej }
    450  1.1   thorpej ATF_TC_BODY(futex_basic_wait_wake_shared, tc)
    451  1.1   thorpej {
    452  1.1   thorpej 	do_futex_wait_wake_test(&futex_word, NULL,
    453  1.1   thorpej 				NULL, NULL, NULL,
    454  1.1   thorpej 				0);
    455  1.1   thorpej }
    456  1.1   thorpej ATF_TC_CLEANUP(futex_basic_wait_wake_shared, tc)
    457  1.1   thorpej {
    458  1.1   thorpej 	do_cleanup();
    459  1.1   thorpej }
    460  1.1   thorpej 
    461  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_private);
    462  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_private, tc)
    463  1.1   thorpej {
    464  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    465  1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_ANON + PRIVATE)");
    466  1.1   thorpej }
    467  1.1   thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_private, tc)
    468  1.1   thorpej {
    469  1.1   thorpej 	create_bs(MAP_ANON | MAP_PRIVATE);
    470  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    471  1.1   thorpej 				NULL, NULL, NULL,
    472  1.1   thorpej 				FUTEX_PRIVATE_FLAG);
    473  1.1   thorpej }
    474  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_private, tc)
    475  1.1   thorpej {
    476  1.1   thorpej 	do_cleanup();
    477  1.1   thorpej }
    478  1.1   thorpej 
    479  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_shared);
    480  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_shared, tc)
    481  1.1   thorpej {
    482  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    483  1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_ANON + SHARED)");
    484  1.1   thorpej }
    485  1.1   thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_shared, tc)
    486  1.1   thorpej {
    487  1.1   thorpej 	create_bs(MAP_ANON | MAP_PRIVATE);
    488  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    489  1.1   thorpej 				NULL, NULL, NULL,
    490  1.1   thorpej 				0);
    491  1.1   thorpej }
    492  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_shared, tc)
    493  1.1   thorpej {
    494  1.1   thorpej 	do_cleanup();
    495  1.1   thorpej }
    496  1.1   thorpej 
    497  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_private);
    498  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_private, tc)
    499  1.1   thorpej {
    500  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    501  1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE + PRIVATE)");
    502  1.1   thorpej }
    503  1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_private, tc)
    504  1.1   thorpej {
    505  1.1   thorpej 	/*
    506  1.1   thorpej 	 * This combination (non-COW mapped file + PRIVATE futex)
    507  1.1   thorpej 	 * doesn't really make sense, but we should make sure it
    508  1.1   thorpej 	 * works as expected.
    509  1.1   thorpej 	 */
    510  1.1   thorpej 	create_bs(MAP_FILE | MAP_SHARED);
    511  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    512  1.1   thorpej 				NULL, NULL, NULL,
    513  1.1   thorpej 				FUTEX_PRIVATE_FLAG);
    514  1.1   thorpej 	ATF_REQUIRE(! verify_zero_bs());
    515  1.1   thorpej }
    516  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_private, tc)
    517  1.1   thorpej {
    518  1.1   thorpej 	do_cleanup();
    519  1.1   thorpej }
    520  1.1   thorpej 
    521  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_cow_private);
    522  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_cow_private, tc)
    523  1.1   thorpej {
    524  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    525  1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE COW + PRIVATE)");
    526  1.1   thorpej }
    527  1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_cow_private, tc)
    528  1.1   thorpej {
    529  1.1   thorpej 	create_bs(MAP_FILE | MAP_PRIVATE);
    530  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    531  1.1   thorpej 				NULL, NULL, NULL,
    532  1.1   thorpej 				FUTEX_PRIVATE_FLAG);
    533  1.1   thorpej 	ATF_REQUIRE(verify_zero_bs());
    534  1.1   thorpej }
    535  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_cow_private, tc)
    536  1.1   thorpej {
    537  1.1   thorpej 	do_cleanup();
    538  1.1   thorpej }
    539  1.1   thorpej 
    540  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_shared);
    541  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_shared, tc)
    542  1.1   thorpej {
    543  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    544  1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE + SHARED)");
    545  1.1   thorpej }
    546  1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_shared, tc)
    547  1.1   thorpej {
    548  1.1   thorpej 	create_bs(MAP_FILE | MAP_SHARED);
    549  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    550  1.1   thorpej 				NULL, NULL, NULL,
    551  1.1   thorpej 				0);
    552  1.1   thorpej 	ATF_REQUIRE(! verify_zero_bs());
    553  1.1   thorpej }
    554  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_shared, tc)
    555  1.1   thorpej {
    556  1.1   thorpej 	do_cleanup();
    557  1.1   thorpej }
    558  1.1   thorpej 
    559  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_cow_shared);
    560  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_cow_shared, tc)
    561  1.1   thorpej {
    562  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    563  1.1   thorpej 	    "tests futex WAIT + WAKE operations (MAP_FILE COW + SHARED)");
    564  1.1   thorpej }
    565  1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_cow_shared, tc)
    566  1.1   thorpej {
    567  1.1   thorpej 	/*
    568  1.1   thorpej 	 * This combination (COW mapped file + SHARED futex)
    569  1.1   thorpej 	 * doesn't really make sense, but we should make sure it
    570  1.1   thorpej 	 * works as expected.
    571  1.1   thorpej 	 */
    572  1.1   thorpej 	create_bs(MAP_FILE | MAP_PRIVATE);
    573  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], NULL,
    574  1.1   thorpej 				NULL, NULL, NULL,
    575  1.1   thorpej 				0);
    576  1.1   thorpej 	ATF_REQUIRE(verify_zero_bs());
    577  1.1   thorpej }
    578  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_cow_shared, tc)
    579  1.1   thorpej {
    580  1.1   thorpej 	do_cleanup();
    581  1.1   thorpej }
    582  1.1   thorpej 
    583  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_anon_bs_shared_proc);
    584  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_anon_bs_shared_proc, tc)
    585  1.1   thorpej {
    586  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    587  1.1   thorpej 	    "tests multiproc futex WAIT + WAKE operations (MAP_ANON + SHARED)");
    588  1.1   thorpej }
    589  1.1   thorpej ATF_TC_BODY(futex_wait_wake_anon_bs_shared_proc, tc)
    590  1.1   thorpej {
    591  1.1   thorpej 	create_bs(MAP_ANON | MAP_SHARED);
    592  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], &bs_addr[1],
    593  1.1   thorpej 				create_proc_waiter,
    594  1.1   thorpej 				exit_proc_waiter,
    595  1.1   thorpej 				reap_proc_waiter,
    596  1.1   thorpej 				0);
    597  1.1   thorpej }
    598  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_anon_bs_shared_proc, tc)
    599  1.1   thorpej {
    600  1.1   thorpej 	do_cleanup();
    601  1.1   thorpej }
    602  1.1   thorpej 
    603  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_file_bs_shared_proc);
    604  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_file_bs_shared_proc, tc)
    605  1.1   thorpej {
    606  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    607  1.1   thorpej 	    "tests multiproc futex WAIT + WAKE operations (MAP_ANON + SHARED)");
    608  1.1   thorpej }
    609  1.1   thorpej ATF_TC_BODY(futex_wait_wake_file_bs_shared_proc, tc)
    610  1.1   thorpej {
    611  1.1   thorpej 	create_bs(MAP_FILE | MAP_SHARED);
    612  1.1   thorpej 	do_futex_wait_wake_test(&bs_addr[0], &bs_addr[1],
    613  1.1   thorpej 				create_proc_waiter,
    614  1.1   thorpej 				exit_proc_waiter,
    615  1.1   thorpej 				reap_proc_waiter,
    616  1.1   thorpej 				0);
    617  1.1   thorpej }
    618  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_file_bs_shared_proc, tc)
    619  1.1   thorpej {
    620  1.1   thorpej 	do_cleanup();
    621  1.1   thorpej }
    622  1.1   thorpej 
    623  1.1   thorpej /*****************************************************************************/
    624  1.1   thorpej 
    625  1.1   thorpej ATF_TC(futex_wait_pointless_bitset);
    626  1.1   thorpej ATF_TC_HEAD(futex_wait_pointless_bitset, tc)
    627  1.1   thorpej {
    628  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    629  1.1   thorpej 	    "tests basic futex WAIT + WAKE operations (SHARED)");
    630  1.1   thorpej }
    631  1.1   thorpej ATF_TC_BODY(futex_wait_pointless_bitset, tc)
    632  1.1   thorpej {
    633  1.1   thorpej 
    634  1.1   thorpej 	futex_word = 1;
    635  1.2  riastrad 	ATF_REQUIRE_ERRNO(EINVAL,
    636  1.2  riastrad 	    __futex(&futex_word, FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG,
    637  1.2  riastrad 		1, NULL, NULL, 0, 0) == -1);
    638  1.1   thorpej }
    639  1.1   thorpej 
    640  1.1   thorpej static void
    641  1.1   thorpej do_futex_wait_wake_bitset_test(int flags)
    642  1.1   thorpej {
    643  1.1   thorpej 	struct lwp_data *wlwp0 = &lwp_data[WAITER_LWP0];
    644  1.1   thorpej 	struct lwp_data *wlwp1 = &lwp_data[WAITER_LWP1];
    645  1.1   thorpej 	int i, tries;
    646  1.1   thorpej 
    647  1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP1; i++) {
    648  1.1   thorpej 		setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
    649  1.1   thorpej 		lwp_data[i].op_flags = flags;
    650  1.1   thorpej 		lwp_data[i].futex_error = -1;
    651  1.1   thorpej 		lwp_data[i].bitset = __BIT(i);
    652  1.1   thorpej 		lwp_data[i].wait_op = FUTEX_WAIT_BITSET;
    653  1.1   thorpej 		lwp_data[i].futex_ptr = &futex_word;
    654  1.1   thorpej 		lwp_data[i].block_val = 1;
    655  1.1   thorpej 	}
    656  1.1   thorpej 
    657  1.1   thorpej 	STORE(&futex_word, 1);
    658  1.1   thorpej 	membar_sync();
    659  1.1   thorpej 
    660  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&wlwp0->context, 0, &wlwp0->lwpid) == 0);
    661  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&wlwp1->context, 0, &wlwp1->lwpid) == 0);
    662  1.1   thorpej 
    663  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    664  1.1   thorpej 		membar_sync();
    665  1.1   thorpej 		if (nlwps_running == 2)
    666  1.1   thorpej 			break;
    667  1.1   thorpej 		sleep(1);
    668  1.1   thorpej 	}
    669  1.1   thorpej 	membar_sync();
    670  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 2, "waiters failed to start");
    671  1.1   thorpej 
    672  1.1   thorpej 	/* Ensure they're blocked. */
    673  1.1   thorpej 	ATF_REQUIRE(wlwp0->futex_error == -1);
    674  1.1   thorpej 	ATF_REQUIRE(wlwp1->futex_error == -1);
    675  1.1   thorpej 
    676  1.1   thorpej 	/* Make sure invalid #wakes in rejected. */
    677  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    678  1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    679  1.1   thorpej 		    -1, NULL, NULL, 0, 0) == -1);
    680  1.1   thorpej 
    681  1.1   thorpej 	/* This should result in no wakeups because no bits are set. */
    682  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    683  1.1   thorpej 			    INT_MAX, NULL, NULL, 0, 0) == 0);
    684  1.1   thorpej 
    685  1.1   thorpej 	/* This should result in no wakeups because the wrongs bits are set. */
    686  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    687  1.1   thorpej 			    INT_MAX, NULL, NULL, 0,
    688  1.1   thorpej 			    ~(wlwp0->bitset | wlwp1->bitset)) == 0);
    689  1.1   thorpej 
    690  1.1   thorpej 	/* Trust, but verify. */
    691  1.1   thorpej 	sleep(1);
    692  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    693  1.1   thorpej 		membar_sync();
    694  1.1   thorpej 		if (nlwps_running == 2)
    695  1.1   thorpej 			break;
    696  1.1   thorpej 		sleep(1);
    697  1.1   thorpej 	}
    698  1.1   thorpej 	membar_sync();
    699  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 2, "waiters exited unexpectedly");
    700  1.1   thorpej 
    701  1.1   thorpej 	/* Wake up the first LWP. */
    702  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    703  1.1   thorpej 			    INT_MAX, NULL, NULL, 0,
    704  1.1   thorpej 			    wlwp0->bitset) == 1);
    705  1.1   thorpej 	sleep(1);
    706  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    707  1.1   thorpej 		membar_sync();
    708  1.1   thorpej 		if (nlwps_running == 1)
    709  1.1   thorpej 			break;
    710  1.1   thorpej 		sleep(1);
    711  1.1   thorpej 	}
    712  1.1   thorpej 	membar_sync();
    713  1.1   thorpej 	ATF_REQUIRE(nlwps_running == 1);
    714  1.1   thorpej 	ATF_REQUIRE(wlwp0->futex_error == 0);
    715  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(wlwp0->lwpid, NULL) == 0);
    716  1.1   thorpej 
    717  1.1   thorpej 	/* Wake up the second LWP. */
    718  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_BITSET | flags,
    719  1.1   thorpej 			    INT_MAX, NULL, NULL, 0,
    720  1.1   thorpej 			    wlwp1->bitset) == 1);
    721  1.1   thorpej 	sleep(1);
    722  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    723  1.1   thorpej 		membar_sync();
    724  1.1   thorpej 		if (nlwps_running == 0)
    725  1.1   thorpej 			break;
    726  1.1   thorpej 		sleep(1);
    727  1.1   thorpej 	}
    728  1.1   thorpej 	membar_sync();
    729  1.1   thorpej 	ATF_REQUIRE(nlwps_running == 0);
    730  1.1   thorpej 	ATF_REQUIRE(wlwp1->futex_error == 0);
    731  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(wlwp1->lwpid, NULL) == 0);
    732  1.1   thorpej }
    733  1.1   thorpej 
    734  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wait_wake_bitset);
    735  1.1   thorpej ATF_TC_HEAD(futex_wait_wake_bitset, tc)
    736  1.1   thorpej {
    737  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    738  1.1   thorpej 	    "tests futex WAIT_BITSET + WAKE_BITSET operations");
    739  1.1   thorpej }
    740  1.1   thorpej ATF_TC_BODY(futex_wait_wake_bitset, tc)
    741  1.1   thorpej {
    742  1.1   thorpej 	do_futex_wait_wake_bitset_test(FUTEX_PRIVATE_FLAG);
    743  1.1   thorpej }
    744  1.1   thorpej ATF_TC_CLEANUP(futex_wait_wake_bitset, tc)
    745  1.1   thorpej {
    746  1.1   thorpej 	do_cleanup();
    747  1.1   thorpej }
    748  1.1   thorpej 
    749  1.1   thorpej /*****************************************************************************/
    750  1.1   thorpej 
    751  1.1   thorpej static void
    752  1.1   thorpej do_futex_requeue_test(int flags, int op)
    753  1.1   thorpej {
    754  1.1   thorpej 	struct lwp_data *wlwp0 = &lwp_data[WAITER_LWP0];
    755  1.1   thorpej 	struct lwp_data *wlwp1 = &lwp_data[WAITER_LWP1];
    756  1.1   thorpej 	struct lwp_data *wlwp2 = &lwp_data[WAITER_LWP2];
    757  1.1   thorpej 	struct lwp_data *wlwp3 = &lwp_data[WAITER_LWP3];
    758  1.1   thorpej 	const int good_val3 = (op == FUTEX_CMP_REQUEUE) ?   1 : 0;
    759  1.1   thorpej 	const int bad_val3  = (op == FUTEX_CMP_REQUEUE) ? 666 : 0;
    760  1.1   thorpej 	int i, tries;
    761  1.1   thorpej 
    762  1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP3; i++) {
    763  1.1   thorpej 		setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
    764  1.1   thorpej 		lwp_data[i].op_flags = flags;
    765  1.1   thorpej 		lwp_data[i].futex_error = -1;
    766  1.1   thorpej 		lwp_data[i].futex_ptr = &futex_word;
    767  1.1   thorpej 		lwp_data[i].block_val = 1;
    768  1.1   thorpej 		lwp_data[i].bitset = 0;
    769  1.1   thorpej 		lwp_data[i].wait_op = FUTEX_WAIT;
    770  1.1   thorpej 	}
    771  1.1   thorpej 
    772  1.1   thorpej 	STORE(&futex_word, 1);
    773  1.1   thorpej 	STORE(&futex_word1, 1);
    774  1.1   thorpej 	membar_sync();
    775  1.1   thorpej 
    776  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&wlwp0->context, 0, &wlwp0->lwpid) == 0);
    777  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&wlwp1->context, 0, &wlwp1->lwpid) == 0);
    778  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&wlwp2->context, 0, &wlwp2->lwpid) == 0);
    779  1.1   thorpej 	ATF_REQUIRE(_lwp_create(&wlwp3->context, 0, &wlwp3->lwpid) == 0);
    780  1.1   thorpej 
    781  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    782  1.1   thorpej 		membar_sync();
    783  1.1   thorpej 		if (nlwps_running == 4)
    784  1.1   thorpej 			break;
    785  1.1   thorpej 		sleep(1);
    786  1.1   thorpej 	}
    787  1.1   thorpej 	membar_sync();
    788  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 4, "waiters failed to start");
    789  1.1   thorpej 
    790  1.1   thorpej 	/* Ensure they're blocked. */
    791  1.1   thorpej 	ATF_REQUIRE(wlwp0->futex_error == -1);
    792  1.1   thorpej 	ATF_REQUIRE(wlwp1->futex_error == -1);
    793  1.1   thorpej 	ATF_REQUIRE(wlwp2->futex_error == -1);
    794  1.1   thorpej 	ATF_REQUIRE(wlwp3->futex_error == -1);
    795  1.1   thorpej 
    796  1.1   thorpej 	/* Make sure invalid #wakes and #requeues are rejected. */
    797  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    798  1.1   thorpej 	    __futex(&futex_word, op | flags,
    799  1.1   thorpej 		    -1, NULL, &futex_word1, INT_MAX, bad_val3) == -1);
    800  1.1   thorpej 
    801  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    802  1.1   thorpej 	    __futex(&futex_word, op | flags,
    803  1.1   thorpej 		    0, NULL, &futex_word1, -1, bad_val3) == -1);
    804  1.1   thorpej 
    805  1.1   thorpej 	/*
    806  1.1   thorpej 	 * FUTEX 0: 4 LWPs
    807  1.1   thorpej 	 * FUTEX 1: 0 LWPs
    808  1.1   thorpej 	 */
    809  1.1   thorpej 
    810  1.1   thorpej 	if (op == FUTEX_CMP_REQUEUE) {
    811  1.1   thorpej 		/* This should fail because the futex_word value is 1. */
    812  1.1   thorpej 		ATF_REQUIRE_ERRNO(EAGAIN,
    813  1.1   thorpej 		    __futex(&futex_word, op | flags,
    814  1.1   thorpej 			    0, NULL, &futex_word1, INT_MAX, bad_val3) == -1);
    815  1.1   thorpej 	}
    816  1.1   thorpej 
    817  1.1   thorpej 	/*
    818  1.1   thorpej 	 * FUTEX 0: 4 LWPs
    819  1.1   thorpej 	 * FUTEX 1: 0 LWPs
    820  1.1   thorpej 	 */
    821  1.1   thorpej 
    822  1.1   thorpej 	/* Move all waiters from 0 to 1. */
    823  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, op | flags,
    824  1.1   thorpej 			    0, NULL, &futex_word1, INT_MAX, good_val3) == 0);
    825  1.1   thorpej 
    826  1.1   thorpej 	/*
    827  1.1   thorpej 	 * FUTEX 0: 0 LWPs
    828  1.1   thorpej 	 * FUTEX 1: 4 LWPs
    829  1.1   thorpej 	 */
    830  1.1   thorpej 
    831  1.1   thorpej 	if (op == FUTEX_CMP_REQUEUE) {
    832  1.1   thorpej 		/* This should fail because the futex_word1 value is 1. */
    833  1.1   thorpej 		ATF_REQUIRE_ERRNO(EAGAIN,
    834  1.1   thorpej 		    __futex(&futex_word1, op | flags,
    835  1.1   thorpej 			    1, NULL, &futex_word, 1, bad_val3) == -1);
    836  1.1   thorpej 	}
    837  1.1   thorpej 
    838  1.1   thorpej 	/*
    839  1.1   thorpej 	 * FUTEX 0: 0 LWPs
    840  1.1   thorpej 	 * FUTEX 1: 4 LWPs
    841  1.1   thorpej 	 */
    842  1.1   thorpej 
    843  1.1   thorpej 	/* Wake one waiter on 1, move one waiter to 0. */
    844  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word1, op | flags,
    845  1.1   thorpej 			    1, NULL, &futex_word, 1, good_val3) == 1);
    846  1.1   thorpej 
    847  1.1   thorpej 	/*
    848  1.1   thorpej 	 * FUTEX 0: 1 LWP
    849  1.1   thorpej 	 * FUTEX 1: 2 LWPs
    850  1.1   thorpej 	 */
    851  1.1   thorpej 
    852  1.1   thorpej 	/* Wake all waiters on 0 (should be 1). */
    853  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE | flags,
    854  1.1   thorpej 			    INT_MAX, NULL, NULL, 0, 0) == 1);
    855  1.1   thorpej 
    856  1.1   thorpej 	/* Wake all waiters on 1 (should be 2). */
    857  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word1, FUTEX_WAKE | flags,
    858  1.1   thorpej 			    INT_MAX, NULL, NULL, 0, 0) == 2);
    859  1.1   thorpej 
    860  1.1   thorpej 	/* Trust, but verify. */
    861  1.1   thorpej 	sleep(1);
    862  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
    863  1.1   thorpej 		membar_sync();
    864  1.1   thorpej 		if (nlwps_running == 0)
    865  1.1   thorpej 			break;
    866  1.1   thorpej 		sleep(1);
    867  1.1   thorpej 	}
    868  1.1   thorpej 	membar_sync();
    869  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "waiters failed to exit");
    870  1.1   thorpej 
    871  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(wlwp0->lwpid, NULL) == 0);
    872  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(wlwp1->lwpid, NULL) == 0);
    873  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(wlwp2->lwpid, NULL) == 0);
    874  1.1   thorpej 	ATF_REQUIRE(_lwp_wait(wlwp3->lwpid, NULL) == 0);
    875  1.1   thorpej }
    876  1.1   thorpej 
    877  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_requeue);
    878  1.1   thorpej ATF_TC_HEAD(futex_requeue, tc)
    879  1.1   thorpej {
    880  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    881  1.1   thorpej 	    "tests futex REQUEUE operations");
    882  1.1   thorpej }
    883  1.1   thorpej ATF_TC_BODY(futex_requeue, tc)
    884  1.1   thorpej {
    885  1.1   thorpej 	do_futex_requeue_test(FUTEX_PRIVATE_FLAG, FUTEX_REQUEUE);
    886  1.1   thorpej }
    887  1.1   thorpej ATF_TC_CLEANUP(futex_requeue, tc)
    888  1.1   thorpej {
    889  1.1   thorpej 	do_cleanup();
    890  1.1   thorpej }
    891  1.1   thorpej 
    892  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_cmp_requeue);
    893  1.1   thorpej ATF_TC_HEAD(futex_cmp_requeue, tc)
    894  1.1   thorpej {
    895  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    896  1.1   thorpej 	    "tests futex CMP_REQUEUE operations");
    897  1.1   thorpej }
    898  1.1   thorpej ATF_TC_BODY(futex_cmp_requeue, tc)
    899  1.1   thorpej {
    900  1.1   thorpej 	do_futex_requeue_test(FUTEX_PRIVATE_FLAG, FUTEX_CMP_REQUEUE);
    901  1.1   thorpej }
    902  1.1   thorpej ATF_TC_CLEANUP(futex_cmp_requeue, tc)
    903  1.1   thorpej {
    904  1.1   thorpej 	do_cleanup();
    905  1.1   thorpej }
    906  1.1   thorpej 
    907  1.1   thorpej /*****************************************************************************/
    908  1.1   thorpej 
    909  1.1   thorpej static void
    910  1.1   thorpej do_futex_wake_op_op_test(int flags)
    911  1.1   thorpej {
    912  1.1   thorpej 	int op;
    913  1.1   thorpej 
    914  1.1   thorpej 	futex_word = 0;
    915  1.1   thorpej 	futex_word1 = 0;
    916  1.1   thorpej 
    917  1.1   thorpej 	/*
    918  1.1   thorpej 	 * The op= operations should work even if there are no waiters.
    919  1.1   thorpej 	 */
    920  1.1   thorpej 
    921  1.1   thorpej 	/*
    922  1.1   thorpej 	 * Because these operations use both futex addresses, exercise
    923  1.1   thorpej 	 * rejecting unaligned futex addresses here.
    924  1.1   thorpej 	 */
    925  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
    926  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    927  1.1   thorpej 	    __futex((int *)1, FUTEX_WAKE_OP | flags,
    928  1.1   thorpej 		    0, NULL, &futex_word1, 0, op) == -1);
    929  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 0);
    930  1.1   thorpej 
    931  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    932  1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_OP | flags,
    933  1.1   thorpej 		    0, NULL, (int *)1, 0, op) == -1);
    934  1.1   thorpej 	ATF_REQUIRE(futex_word == 0);
    935  1.1   thorpej 
    936  1.1   thorpej 	/* Check unmapped uaddr2 handling, too. */
    937  1.1   thorpej 	ATF_REQUIRE_ERRNO(EFAULT,
    938  1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_OP | flags,
    939  1.1   thorpej 		    0, NULL, NULL, 0, op) == -1);
    940  1.1   thorpej 	ATF_REQUIRE(futex_word == 0);
    941  1.1   thorpej 
    942  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
    943  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
    944  1.1   thorpej 			    0, NULL, &futex_word1, 0, op) == 0);
    945  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 1);
    946  1.1   thorpej 
    947  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_ADD, 1, FUTEX_OP_CMP_EQ, 0);
    948  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
    949  1.1   thorpej 			    0, NULL, &futex_word1, 0, op) == 0);
    950  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
    951  1.1   thorpej 
    952  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_OR, 2, FUTEX_OP_CMP_EQ, 0);
    953  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
    954  1.1   thorpej 			    0, NULL, &futex_word1, 0, op) == 0);
    955  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
    956  1.1   thorpej 
    957  1.1   thorpej 	/* This should fail because of invalid shift value 32. */
    958  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_OR | FUTEX_OP_OPARG_SHIFT, 32,
    959  1.1   thorpej 		      FUTEX_OP_CMP_EQ, 0);
    960  1.1   thorpej 	ATF_REQUIRE_ERRNO(EINVAL,
    961  1.1   thorpej 	    __futex(&futex_word, FUTEX_WAKE_OP | flags,
    962  1.1   thorpej 		    0, NULL, &futex_word1, 0, op) == -1);
    963  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
    964  1.1   thorpej 
    965  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_OR | FUTEX_OP_OPARG_SHIFT, 31,
    966  1.1   thorpej 		      FUTEX_OP_CMP_EQ, 0);
    967  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
    968  1.1   thorpej 			    0, NULL, &futex_word1, 0, op) == 0);
    969  1.1   thorpej 	ATF_REQUIRE(futex_word1 == (int)0x80000002);
    970  1.1   thorpej 
    971  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_ANDN | FUTEX_OP_OPARG_SHIFT, 31,
    972  1.1   thorpej 		      FUTEX_OP_CMP_EQ, 0);
    973  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
    974  1.1   thorpej 			    0, NULL, &futex_word1, 0, op) == 0);
    975  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
    976  1.1   thorpej 
    977  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_XOR, 2, FUTEX_OP_CMP_EQ, 0);
    978  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
    979  1.1   thorpej 			    0, NULL, &futex_word1, 0, op) == 0);
    980  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 0);
    981  1.1   thorpej }
    982  1.1   thorpej 
    983  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wake_op_op);
    984  1.1   thorpej ATF_TC_HEAD(futex_wake_op_op, tc)
    985  1.1   thorpej {
    986  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
    987  1.1   thorpej 	    "tests futex WAKE_OP OP operations");
    988  1.1   thorpej }
    989  1.1   thorpej ATF_TC_BODY(futex_wake_op_op, tc)
    990  1.1   thorpej {
    991  1.1   thorpej 	do_futex_wake_op_op_test(FUTEX_PRIVATE_FLAG);
    992  1.1   thorpej }
    993  1.1   thorpej ATF_TC_CLEANUP(futex_wake_op_op, tc)
    994  1.1   thorpej {
    995  1.1   thorpej 	do_cleanup();
    996  1.1   thorpej }
    997  1.1   thorpej 
    998  1.1   thorpej static void
    999  1.1   thorpej create_wake_op_test_lwps(int flags)
   1000  1.1   thorpej {
   1001  1.1   thorpej 	int i;
   1002  1.1   thorpej 
   1003  1.1   thorpej 	futex_word1 = 0;
   1004  1.1   thorpej 	membar_sync();
   1005  1.1   thorpej 
   1006  1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
   1007  1.1   thorpej 		setup_lwp_context(&lwp_data[i], simple_test_waiter_lwp);
   1008  1.1   thorpej 		lwp_data[i].op_flags = flags;
   1009  1.1   thorpej 		lwp_data[i].futex_error = -1;
   1010  1.1   thorpej 		lwp_data[i].futex_ptr = &futex_word1;
   1011  1.1   thorpej 		lwp_data[i].block_val = 0;
   1012  1.1   thorpej 		lwp_data[i].bitset = 0;
   1013  1.1   thorpej 		lwp_data[i].wait_op = FUTEX_WAIT;
   1014  1.1   thorpej 		ATF_REQUIRE(_lwp_create(&lwp_data[i].context, 0,
   1015  1.1   thorpej 					&lwp_data[i].lwpid) == 0);
   1016  1.1   thorpej 	}
   1017  1.1   thorpej 
   1018  1.1   thorpej 	for (i = 0; i < 5; i++) {
   1019  1.1   thorpej 		membar_sync();
   1020  1.1   thorpej 		if (nlwps_running == 6)
   1021  1.1   thorpej 			break;
   1022  1.1   thorpej 		sleep(1);
   1023  1.1   thorpej 	}
   1024  1.1   thorpej 	membar_sync();
   1025  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 6, "waiters failed to start");
   1026  1.1   thorpej 
   1027  1.1   thorpej 	/* Ensure they're blocked. */
   1028  1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
   1029  1.1   thorpej 		ATF_REQUIRE(lwp_data[i].futex_error == -1);
   1030  1.1   thorpej 	}
   1031  1.1   thorpej }
   1032  1.1   thorpej 
   1033  1.1   thorpej static void
   1034  1.1   thorpej reap_wake_op_test_lwps(void)
   1035  1.1   thorpej {
   1036  1.1   thorpej 	int i;
   1037  1.1   thorpej 
   1038  1.1   thorpej 	for (i = WAITER_LWP0; i <= WAITER_LWP5; i++) {
   1039  1.1   thorpej 		ATF_REQUIRE(_lwp_wait(lwp_data[i].lwpid, NULL) == 0);
   1040  1.1   thorpej 	}
   1041  1.1   thorpej }
   1042  1.1   thorpej 
   1043  1.1   thorpej static void
   1044  1.1   thorpej do_futex_wake_op_cmp_test(int flags)
   1045  1.1   thorpej {
   1046  1.1   thorpej 	int tries, op;
   1047  1.1   thorpej 
   1048  1.1   thorpej 	futex_word = 0;
   1049  1.1   thorpej 	membar_sync();
   1050  1.1   thorpej 
   1051  1.1   thorpej 	/*
   1052  1.1   thorpej 	 * Verify and negative and positive for each individual
   1053  1.1   thorpej 	 * compare.
   1054  1.1   thorpej 	 */
   1055  1.1   thorpej 
   1056  1.1   thorpej 	create_wake_op_test_lwps(flags);
   1057  1.1   thorpej 
   1058  1.1   thorpej 	/* #LWPs = 6 */
   1059  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 1);
   1060  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1061  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 0);
   1062  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 0);
   1063  1.1   thorpej 
   1064  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_EQ, 0);
   1065  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1066  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 1);
   1067  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 1);
   1068  1.1   thorpej 
   1069  1.1   thorpej 	/* #LWPs = 5 */
   1070  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_NE, 1);
   1071  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1072  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 0);
   1073  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 1);
   1074  1.1   thorpej 
   1075  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_NE, 2);
   1076  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1077  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 1);
   1078  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
   1079  1.1   thorpej 
   1080  1.1   thorpej 	/* #LWPs = 4 */
   1081  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_LT, 2);
   1082  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1083  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 0);
   1084  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
   1085  1.1   thorpej 
   1086  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_LT, 3);
   1087  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1088  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 1);
   1089  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
   1090  1.1   thorpej 
   1091  1.1   thorpej 	/* #LWPs = 3 */
   1092  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_LE, 1);
   1093  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1094  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 0);
   1095  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 1);
   1096  1.1   thorpej 
   1097  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 1, FUTEX_OP_CMP_LE, 1);
   1098  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1099  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 1);
   1100  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 1);
   1101  1.1   thorpej 
   1102  1.1   thorpej 	/* #LWPs = 2 */
   1103  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 3, FUTEX_OP_CMP_GT, 3);
   1104  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1105  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 0);
   1106  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 3);
   1107  1.1   thorpej 
   1108  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_GT, 2);
   1109  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1110  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 1);
   1111  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
   1112  1.1   thorpej 
   1113  1.1   thorpej 	/* #LWPs = 1 */
   1114  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 3, FUTEX_OP_CMP_GE, 4);
   1115  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1116  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 0);
   1117  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 3);
   1118  1.1   thorpej 
   1119  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_GE, 3);
   1120  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word, FUTEX_WAKE_OP | flags,
   1121  1.1   thorpej 			    0, NULL, &futex_word1, 1, op) == 1);
   1122  1.1   thorpej 	ATF_REQUIRE(futex_word1 == 2);
   1123  1.1   thorpej 
   1124  1.1   thorpej 	/* #LWPs = 0 */
   1125  1.1   thorpej 
   1126  1.1   thorpej 	/* Trust, but verify. */
   1127  1.1   thorpej 	sleep(1);
   1128  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
   1129  1.1   thorpej 		membar_sync();
   1130  1.1   thorpej 		if (nlwps_running == 0)
   1131  1.1   thorpej 			break;
   1132  1.1   thorpej 		sleep(1);
   1133  1.1   thorpej 	}
   1134  1.1   thorpej 	membar_sync();
   1135  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "waiters failed to exit");
   1136  1.1   thorpej 
   1137  1.1   thorpej 	reap_wake_op_test_lwps();
   1138  1.1   thorpej 
   1139  1.1   thorpej 	/*
   1140  1.1   thorpej 	 * Verify wakes on uaddr work even if the uaddr2 comparison
   1141  1.1   thorpej 	 * fails.
   1142  1.1   thorpej 	 */
   1143  1.1   thorpej 
   1144  1.1   thorpej 	create_wake_op_test_lwps(flags);
   1145  1.1   thorpej 
   1146  1.1   thorpej 	/* #LWPs = 6 */
   1147  1.1   thorpej 	ATF_REQUIRE(futex_word == 0);
   1148  1.1   thorpej 	op = FUTEX_OP(FUTEX_OP_SET, 0, FUTEX_OP_CMP_EQ, 666);
   1149  1.1   thorpej 	ATF_REQUIRE(__futex(&futex_word1, FUTEX_WAKE_OP | flags,
   1150  1.1   thorpej 			    INT_MAX, NULL, &futex_word, 0, op) == 6);
   1151  1.1   thorpej 	ATF_REQUIRE(futex_word == 0);
   1152  1.1   thorpej 
   1153  1.1   thorpej 	/* #LWPs = 0 */
   1154  1.1   thorpej 
   1155  1.1   thorpej 	/* Trust, but verify. */
   1156  1.1   thorpej 	sleep(1);
   1157  1.1   thorpej 	for (tries = 0; tries < 5; tries++) {
   1158  1.1   thorpej 		membar_sync();
   1159  1.1   thorpej 		if (nlwps_running == 0)
   1160  1.1   thorpej 			break;
   1161  1.1   thorpej 		sleep(1);
   1162  1.1   thorpej 	}
   1163  1.1   thorpej 	membar_sync();
   1164  1.1   thorpej 	ATF_REQUIRE_EQ_MSG(nlwps_running, 0, "waiters failed to exit");
   1165  1.1   thorpej 
   1166  1.1   thorpej 	reap_wake_op_test_lwps();
   1167  1.1   thorpej }
   1168  1.1   thorpej 
   1169  1.1   thorpej ATF_TC_WITH_CLEANUP(futex_wake_op_cmp);
   1170  1.1   thorpej ATF_TC_HEAD(futex_wake_op_cmp, tc)
   1171  1.1   thorpej {
   1172  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1173  1.1   thorpej 	    "tests futex WAKE_OP CMP operations");
   1174  1.1   thorpej }
   1175  1.1   thorpej ATF_TC_BODY(futex_wake_op_cmp, tc)
   1176  1.1   thorpej {
   1177  1.1   thorpej 	do_futex_wake_op_cmp_test(FUTEX_PRIVATE_FLAG);
   1178  1.1   thorpej }
   1179  1.1   thorpej ATF_TC_CLEANUP(futex_wake_op_cmp, tc)
   1180  1.1   thorpej {
   1181  1.1   thorpej 	do_cleanup();
   1182  1.1   thorpej }
   1183  1.1   thorpej 
   1184  1.1   thorpej /*****************************************************************************/
   1185  1.1   thorpej 
   1186  1.1   thorpej static void
   1187  1.1   thorpej do_futex_wait_timeout(bool relative, clockid_t clock)
   1188  1.1   thorpej {
   1189  1.1   thorpej 	struct timespec ts;
   1190  1.1   thorpej 	struct timespec deadline;
   1191  1.1   thorpej 	int op = relative ? FUTEX_WAIT : FUTEX_WAIT_BITSET;
   1192  1.1   thorpej 
   1193  1.1   thorpej 	if (clock == CLOCK_REALTIME)
   1194  1.1   thorpej 		op |= FUTEX_CLOCK_REALTIME;
   1195  1.1   thorpej 
   1196  1.1   thorpej 	ATF_REQUIRE(clock_gettime(clock, &deadline) == 0);
   1197  1.1   thorpej 	deadline.tv_sec += 2;
   1198  1.1   thorpej 	if (relative) {
   1199  1.1   thorpej 		ts.tv_sec = 2;
   1200  1.1   thorpej 		ts.tv_nsec = 0;
   1201  1.1   thorpej 	} else {
   1202  1.1   thorpej 		ts = deadline;
   1203  1.1   thorpej 	}
   1204  1.1   thorpej 
   1205  1.1   thorpej 	futex_word = 1;
   1206  1.1   thorpej 	ATF_REQUIRE_ERRNO(ETIMEDOUT,
   1207  1.1   thorpej 	    __futex(&futex_word, op | FUTEX_PRIVATE_FLAG,
   1208  1.1   thorpej 		    1, &ts, NULL, 0, FUTEX_BITSET_MATCH_ANY) == -1);
   1209  1.1   thorpej 
   1210  1.1   thorpej 	/* Can't reliably check CLOCK_REALTIME in the presence of NTP. */
   1211  1.1   thorpej 	if (clock != CLOCK_REALTIME) {
   1212  1.1   thorpej 		ATF_REQUIRE(clock_gettime(clock, &ts) == 0);
   1213  1.1   thorpej 		ATF_REQUIRE(ts.tv_sec >= deadline.tv_sec);
   1214  1.1   thorpej 		ATF_REQUIRE(ts.tv_sec > deadline.tv_sec ||
   1215  1.1   thorpej 			    ts.tv_nsec >= deadline.tv_nsec);
   1216  1.1   thorpej 	}
   1217  1.1   thorpej }
   1218  1.1   thorpej 
   1219  1.1   thorpej ATF_TC(futex_wait_timeout_relative);
   1220  1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_relative, tc)
   1221  1.1   thorpej {
   1222  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1223  1.1   thorpej 	    "tests futex WAIT with relative timeout");
   1224  1.1   thorpej }
   1225  1.1   thorpej ATF_TC_BODY(futex_wait_timeout_relative, tc)
   1226  1.1   thorpej {
   1227  1.1   thorpej 	do_futex_wait_timeout(true, CLOCK_MONOTONIC);
   1228  1.1   thorpej }
   1229  1.1   thorpej 
   1230  1.1   thorpej ATF_TC(futex_wait_timeout_relative_rt);
   1231  1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_relative_rt, tc)
   1232  1.1   thorpej {
   1233  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1234  1.1   thorpej 	    "tests futex WAIT with relative timeout (REALTIME)");
   1235  1.1   thorpej }
   1236  1.1   thorpej ATF_TC_BODY(futex_wait_timeout_relative_rt, tc)
   1237  1.1   thorpej {
   1238  1.1   thorpej 	do_futex_wait_timeout(true, CLOCK_REALTIME);
   1239  1.1   thorpej }
   1240  1.1   thorpej 
   1241  1.1   thorpej ATF_TC(futex_wait_timeout_deadline);
   1242  1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_deadline, tc)
   1243  1.1   thorpej {
   1244  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1245  1.1   thorpej 	    "tests futex WAIT with absolute deadline");
   1246  1.1   thorpej }
   1247  1.1   thorpej ATF_TC_BODY(futex_wait_timeout_deadline, tc)
   1248  1.1   thorpej {
   1249  1.1   thorpej 	do_futex_wait_timeout(false, CLOCK_MONOTONIC);
   1250  1.1   thorpej }
   1251  1.1   thorpej 
   1252  1.1   thorpej ATF_TC(futex_wait_timeout_deadline_rt);
   1253  1.1   thorpej ATF_TC_HEAD(futex_wait_timeout_deadline_rt, tc)
   1254  1.1   thorpej {
   1255  1.1   thorpej 	atf_tc_set_md_var(tc, "descr",
   1256  1.1   thorpej 	    "tests futex WAIT with absolute deadline (REALTIME)");
   1257  1.1   thorpej }
   1258  1.1   thorpej ATF_TC_BODY(futex_wait_timeout_deadline_rt, tc)
   1259  1.1   thorpej {
   1260  1.1   thorpej 	do_futex_wait_timeout(false, CLOCK_REALTIME);
   1261  1.1   thorpej }
   1262  1.1   thorpej 
   1263  1.1   thorpej /*****************************************************************************/
   1264  1.1   thorpej 
   1265  1.1   thorpej ATF_TP_ADD_TCS(tp)
   1266  1.1   thorpej {
   1267  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_basic_wait_wake_private);
   1268  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_basic_wait_wake_shared);
   1269  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_private);
   1270  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_shared);
   1271  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_private);
   1272  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_shared);
   1273  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_cow_private);
   1274  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_cow_shared);
   1275  1.1   thorpej 
   1276  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_anon_bs_shared_proc);
   1277  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_file_bs_shared_proc);
   1278  1.1   thorpej 
   1279  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_pointless_bitset);
   1280  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_wake_bitset);
   1281  1.1   thorpej 
   1282  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_relative);
   1283  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_relative_rt);
   1284  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_deadline);
   1285  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wait_timeout_deadline_rt);
   1286  1.1   thorpej 
   1287  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_requeue);
   1288  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_cmp_requeue);
   1289  1.1   thorpej 
   1290  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wake_op_op);
   1291  1.1   thorpej 	ATF_TP_ADD_TC(tp, futex_wake_op_cmp);
   1292  1.1   thorpej 
   1293  1.1   thorpej 	return atf_no_error();
   1294  1.1   thorpej }
   1295