14c87aefeSPatrick Mooney /*
24c87aefeSPatrick Mooney  * This file and its contents are supplied under the terms of the
34c87aefeSPatrick Mooney  * Common Development and Distribution License ("CDDL"), version 1.0.
44c87aefeSPatrick Mooney  * You may only use this file in accordance with the terms of version
54c87aefeSPatrick Mooney  * 1.0 of the CDDL.
64c87aefeSPatrick Mooney  *
74c87aefeSPatrick Mooney  * A full copy of the text of the CDDL should have accompanied this
84c87aefeSPatrick Mooney  * source.  A copy of the CDDL is also available via the Internet at
94c87aefeSPatrick Mooney  * http://www.illumos.org/license/CDDL.
104c87aefeSPatrick Mooney  */
114c87aefeSPatrick Mooney 
124c87aefeSPatrick Mooney /*
134c87aefeSPatrick Mooney  * Copyright 2018 Joyent, Inc.
144c87aefeSPatrick Mooney  */
154c87aefeSPatrick Mooney 
164c87aefeSPatrick Mooney /*
174c87aefeSPatrick Mooney  *        Test:	lists.delete
184c87aefeSPatrick Mooney  *   Assertion: mevent_delete() causes the total number of events to decrease
194c87aefeSPatrick Mooney  *
204c87aefeSPatrick Mooney  *    Strategy: 1. Create a pipe.
214c87aefeSPatrick Mooney  *		2. Call mevent_add() to be notified of writes to the pipe.  The
224c87aefeSPatrick Mooney  *		   callback will do nothing other than generate an error if it
234c87aefeSPatrick Mooney  *		   is called.
244c87aefeSPatrick Mooney  *		3. Create another pipe and add a read event watcher to it.  The
254c87aefeSPatrick Mooney  *		   callback will signal a cv when called.  A write to the pipe
264c87aefeSPatrick Mooney  *		   followed by a wait on the cv will ensure that async
274c87aefeSPatrick Mooney  *		   operations in mevent.c are complete.  See flush_and_wait().
284c87aefeSPatrick Mooney  *		4. Call flush_and_wait(), then get event count.
294c87aefeSPatrick Mooney  *		5. Delete the event created in step 2.
304c87aefeSPatrick Mooney  *		6. Call flush_and_wait(), then get event count.
314c87aefeSPatrick Mooney  *		7. Verify result in step 6 is one less than result in step 4.
324c87aefeSPatrick Mooney  */
334c87aefeSPatrick Mooney 
344c87aefeSPatrick Mooney #include <errno.h>
354c87aefeSPatrick Mooney #include <fcntl.h>
364c87aefeSPatrick Mooney #include <pthread.h>
374c87aefeSPatrick Mooney #include <signal.h>
384c87aefeSPatrick Mooney #include <stdio.h>
394c87aefeSPatrick Mooney #include <stdlib.h>
404c87aefeSPatrick Mooney #include <strings.h>
414c87aefeSPatrick Mooney #include <unistd.h>
424c87aefeSPatrick Mooney 
434c87aefeSPatrick Mooney #include <sys/types.h>
444c87aefeSPatrick Mooney #include <sys/stat.h>
454c87aefeSPatrick Mooney 
464c87aefeSPatrick Mooney #include "testlib.h"
474c87aefeSPatrick Mooney #include "mevent.h"
484c87aefeSPatrick Mooney 
494c87aefeSPatrick Mooney static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
504c87aefeSPatrick Mooney static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
514c87aefeSPatrick Mooney 
524c87aefeSPatrick Mooney static int
get_count(void)534c87aefeSPatrick Mooney get_count(void)
544c87aefeSPatrick Mooney {
554c87aefeSPatrick Mooney 	int global = -1, change = -1, del_pending = -1;
564c87aefeSPatrick Mooney 	int total;
574c87aefeSPatrick Mooney 
584c87aefeSPatrick Mooney 	test_mevent_count_lists(&global, &change, &del_pending);
594c87aefeSPatrick Mooney 	ASSERT_INT_NEQ(("count not set"), global, -1);
604c87aefeSPatrick Mooney 	ASSERT_INT_NEQ(("count not set"), change, -1);
614c87aefeSPatrick Mooney 	ASSERT_INT_NEQ(("count not set"), change, -1);
624c87aefeSPatrick Mooney 	ASSERT_INT_EQ(("pending delete not processed"), del_pending, 0);
634c87aefeSPatrick Mooney 
644c87aefeSPatrick Mooney 	total = global + change + del_pending;
654c87aefeSPatrick Mooney 
664c87aefeSPatrick Mooney 	VERBOSE(("count = %d (%d + %d + %d)", total, global, change,
674c87aefeSPatrick Mooney 	    del_pending));
684c87aefeSPatrick Mooney 
694c87aefeSPatrick Mooney 	return (total);
704c87aefeSPatrick Mooney }
714c87aefeSPatrick Mooney 
724c87aefeSPatrick Mooney static void
not_called_cb(int fd,enum ev_type ev,void * arg)734c87aefeSPatrick Mooney not_called_cb(int fd, enum ev_type ev, void *arg)
744c87aefeSPatrick Mooney {
754c87aefeSPatrick Mooney 	FAIL(("this callback should never be called"));
764c87aefeSPatrick Mooney }
774c87aefeSPatrick Mooney 
784c87aefeSPatrick Mooney static void
flush_cb(int fd,enum ev_type ev,void * arg)794c87aefeSPatrick Mooney flush_cb(int fd, enum ev_type ev, void *arg)
804c87aefeSPatrick Mooney {
814c87aefeSPatrick Mooney 	char buf[32];
824c87aefeSPatrick Mooney 
834c87aefeSPatrick Mooney 	/* Drain the pipe */
844c87aefeSPatrick Mooney 	while (read(fd, buf, sizeof (buf)) > 0)
854c87aefeSPatrick Mooney 		;
864c87aefeSPatrick Mooney 
874c87aefeSPatrick Mooney 	pthread_mutex_lock(&mtx);
884c87aefeSPatrick Mooney 	pthread_cond_signal(&cv);
894c87aefeSPatrick Mooney 	pthread_mutex_unlock(&mtx);
904c87aefeSPatrick Mooney }
914c87aefeSPatrick Mooney 
924c87aefeSPatrick Mooney void
flush_and_wait(int fd)934c87aefeSPatrick Mooney flush_and_wait(int fd)
944c87aefeSPatrick Mooney {
954c87aefeSPatrick Mooney 	uint8_t msg = 42;
964c87aefeSPatrick Mooney 
974c87aefeSPatrick Mooney 	/*
984c87aefeSPatrick Mooney 	 * Lock taken ahead of waking flush_cb so this thread doesn't race
994c87aefeSPatrick Mooney 	 * with the event thread.
1004c87aefeSPatrick Mooney 	 */
1014c87aefeSPatrick Mooney 	pthread_mutex_lock(&mtx);
1024c87aefeSPatrick Mooney 	if (write(fd, &msg, sizeof (msg)) != sizeof (msg)) {
1034c87aefeSPatrick Mooney 		FAIL(("bad write"));
1044c87aefeSPatrick Mooney 	}
1054c87aefeSPatrick Mooney 
1064c87aefeSPatrick Mooney 	/* Wait for it to be read */
1074c87aefeSPatrick Mooney 	pthread_cond_wait(&cv, &mtx);
1084c87aefeSPatrick Mooney 	pthread_mutex_unlock(&mtx);
1094c87aefeSPatrick Mooney }
1104c87aefeSPatrick Mooney 
1114c87aefeSPatrick Mooney int
main(int argc,const char * argv[])1124c87aefeSPatrick Mooney main(int argc, const char *argv[])
1134c87aefeSPatrick Mooney {
1144c87aefeSPatrick Mooney 	int unused_pipe[2];
1154c87aefeSPatrick Mooney 	int flush_pipe[2];
1164c87aefeSPatrick Mooney 	struct mevent *unused_evp, *flush_evp;
1174c87aefeSPatrick Mooney 	int count1, count2;
1184c87aefeSPatrick Mooney 
1194c87aefeSPatrick Mooney 	start_test(argv[0], 5);
1204c87aefeSPatrick Mooney 	start_event_thread();
1214c87aefeSPatrick Mooney 
1224c87aefeSPatrick Mooney 	/*
1234c87aefeSPatrick Mooney 	 * Create first pipe and related event
1244c87aefeSPatrick Mooney 	 */
1254c87aefeSPatrick Mooney 	if (pipe(unused_pipe) != 0) {
1264c87aefeSPatrick Mooney 		FAIL_ERRNO("pipe");
1274c87aefeSPatrick Mooney 	}
1284c87aefeSPatrick Mooney 	VERBOSE(("unused_pipe[] = { %d, %d }", unused_pipe[0], unused_pipe[1]));
1294c87aefeSPatrick Mooney 	if (fcntl(unused_pipe[0], F_SETFL, O_NONBLOCK) != 0) {
1304c87aefeSPatrick Mooney 		FAIL_ERRNO("set pipe nonblocking");
1314c87aefeSPatrick Mooney 	}
1324c87aefeSPatrick Mooney 	unused_evp = mevent_add(unused_pipe[0], EVF_READ, not_called_cb, NULL);
1334c87aefeSPatrick Mooney 	ASSERT_PTR_NEQ(("mevent_add"), unused_evp, NULL);
1344c87aefeSPatrick Mooney 
1354c87aefeSPatrick Mooney 	/*
1364c87aefeSPatrick Mooney 	 * Create flush pipe and related event
1374c87aefeSPatrick Mooney 	 */
1384c87aefeSPatrick Mooney 	if (pipe(flush_pipe) != 0) {
1394c87aefeSPatrick Mooney 		FAIL_ERRNO("pipe");
1404c87aefeSPatrick Mooney 	}
1414c87aefeSPatrick Mooney 	VERBOSE(("flush_pipe[] = { %d, %d }", flush_pipe[0],
1424c87aefeSPatrick Mooney 	    flush_pipe[1]));
1434c87aefeSPatrick Mooney 	if (fcntl(flush_pipe[0], F_SETFL, O_NONBLOCK) != 0) {
1444c87aefeSPatrick Mooney 		FAIL_ERRNO("set pipe nonblocking");
1454c87aefeSPatrick Mooney 	}
1464c87aefeSPatrick Mooney 	flush_evp = mevent_add(flush_pipe[0], EVF_READ, flush_cb, NULL);
1474c87aefeSPatrick Mooney 	ASSERT_PTR_NEQ(("mevent_add"), flush_evp, NULL);
1484c87aefeSPatrick Mooney 
1494c87aefeSPatrick Mooney 	/* Get count before delete. */
1504c87aefeSPatrick Mooney 	flush_and_wait(flush_pipe[1]);
1514c87aefeSPatrick Mooney 	count1 = get_count();
1524c87aefeSPatrick Mooney 
1534c87aefeSPatrick Mooney 	/*
1544c87aefeSPatrick Mooney 	 * Delete the first event and flush a read after the delete is
1554c87aefeSPatrick Mooney 	 * complete.
1564c87aefeSPatrick Mooney 	 */
1574c87aefeSPatrick Mooney 	if (mevent_delete(unused_evp) != 0) {
1584c87aefeSPatrick Mooney 		FAIL_ERRNO("mevent_delete");
1594c87aefeSPatrick Mooney 	}
1604c87aefeSPatrick Mooney 
1614c87aefeSPatrick Mooney 	/*
1624c87aefeSPatrick Mooney 	 * Verify count decreased.
1634c87aefeSPatrick Mooney 	 */
1644c87aefeSPatrick Mooney 	flush_and_wait(flush_pipe[1]);
1654c87aefeSPatrick Mooney 	count2 = get_count();
1664c87aefeSPatrick Mooney 	if (count1 - 1 != count2) {
1674c87aefeSPatrick Mooney 		FAIL(("mevent_delete() did not decrease count by 1: "
1684c87aefeSPatrick Mooney 		    "was %d, now %d", count1, count2));
1694c87aefeSPatrick Mooney 	}
1704c87aefeSPatrick Mooney 
1714c87aefeSPatrick Mooney 	PASS();
1724c87aefeSPatrick Mooney }