summaryrefslogtreecommitdiff
path: root/usr/src/cmd/bhyve/test/tst/mevent/read.disable.c
blob: d23b1af96cb75b3cccc1ebfd00e5a93d42fec808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2018 Joyent, Inc.
 */

/*
 *        Test:	read.cancel
 *   Assertion: A read is not requeued if mevent_disable() is called while it is
 *		being handled.
 *
 *    Strategy: 1. Create a pipe
 *		2. Call mevent_add() to be notified of writes to the pipe.  The
 *		   callback will signal a cv.
 *		3. Write to the pipe then wait for a wakeup.
 *		4. From the read event callback, disable the event then awaken
 *		   the main thread.
 *		5. In the main thread, add a timer event that will awaken the
 *		   main thread after a short delay.
 *		5. Write to the pipe and wait to be awoken.  The wakeup should
 *		   come from the timer event, not the read event.
 */

#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#include "testlib.h"
#include "mevent.h"

typedef enum {
	CB_NONE,
	CB_READ,
	CB_TIMER,
} lastwake_t;

static lastwake_t lastwake = CB_NONE;

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;

static struct mevent *read_event;

static void
munch(int fd, enum ev_type ev, void *arg)
{
	ssize_t nbytes;
	char buf[32] = { 0 };
	int err;

	if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
		FAIL_ERRNO("bad read");
	}
	VERBOSE(("read %ld bytes '%s'", nbytes, buf));

	err = mevent_disable(read_event);
	ASSERT_INT_EQ(("mevent_disable: ", strerror(err)), err, 0);

	pthread_mutex_lock(&mtx);

	ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_NONE);
	lastwake = CB_READ;

	pthread_cond_signal(&cv);
	VERBOSE(("wakeup"));

	pthread_mutex_unlock(&mtx);
}

static void
tick(int ms, enum ev_type ev, void *arg)
{
	pthread_mutex_lock(&mtx);

	ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_READ);
	lastwake = CB_TIMER;

	pthread_cond_signal(&cv);
	VERBOSE(("wakeup"));

	pthread_mutex_unlock(&mtx);
}

int
main(int argc, const char *argv[])
{
	int pipefds[2];
	struct mevent *timer;
	ssize_t written;
	char *msgs[] = { "first", "second" };
	char *msg;

	start_test(argv[0], 5);
	start_event_thread();

	if (pipe(pipefds) != 0) {
		FAIL_ERRNO("pipe");
	}
	if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
		FAIL_ERRNO("set pipe nonblocking");
	}

	/*
	 * First write
	 */
	msg = msgs[0];
	read_event = mevent_add(pipefds[0], EVF_READ, munch, msg);
	ASSERT_PTR_NEQ(("mevent_add pipefd"), read_event, NULL);

	pthread_mutex_lock(&mtx);
	written = write(pipefds[1], msg, strlen(msg));
	if (written < 0) {
		FAIL_ERRNO("bad write");
	}
	ASSERT_INT64_EQ(("write '%s' failed", msg), written, strlen(msg));

	/*
	 * Wait for it to be read
	 */
	pthread_cond_wait(&cv, &mtx);
	ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_READ);
	pthread_mutex_unlock(&mtx);

	/*
	 * Add timer, second write.
	 */
	msg = msgs[1];
	timer = mevent_add(50, EVF_TIMER, tick, msg);
	ASSERT_PTR_NEQ(("mevent_add timer"), timer, NULL);

	pthread_mutex_lock(&mtx);
	written = write(pipefds[1], msg, strlen(msg));
	if (written < 0) {
		FAIL_ERRNO("bad write");
	}
	ASSERT_INT64_EQ(("write '%s' failed", msg), written, strlen(msg));

	/*
	 * Wait for timer to expire
	 */
	pthread_cond_wait(&cv, &mtx);
	ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_TIMER);
	pthread_mutex_unlock(&mtx);

	PASS();
}