#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"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static char *cookie = "Chocolate chip with fudge stripes";
static void
munch(int fd, enum ev_type ev, void *arg)
{
static int i = 0;
char buf[8] = { 0 };
ssize_t nbytes;
ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
ASSERT_INT64_EQ(("bad read: %s", strerror(errno)), nbytes, 1);
}
VERBOSE(("read %ld bytes '%s'", nbytes, buf));
ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, 1);
ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
i++;
}
int
main(int argc, const char *argv[])
{
int pipefds[2];
struct mevent *evp;
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");
}
evp = mevent_add(pipefds[0], EVF_READ, munch, cookie);
ASSERT_PTR_NEQ(("mevent_add"), evp, NULL);
for (int i = 0; cookie[i] != '\0'; i++) {
ssize_t written;
pthread_mutex_lock(&mtx);
written = write(pipefds[1], cookie + i, 1);
if (written < 0) {
FAIL_ERRNO("bad write");
}
ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
}
PASS();
}