#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";
const int pauseat = 8;
static void
munch(int fd, enum ev_type ev, void *arg)
{
static int i = 0;
char buf[sizeof (cookie)] = { 0 };
ssize_t nbytes;
ssize_t expected;
ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
if (i > pauseat) {
expected = strlen(cookie) - pauseat - 1;
} else {
expected = 1;
}
if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
FAIL_ERRNO("bad read");
}
VERBOSE(("read %ld bytes '%s'", nbytes, buf));
ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, expected);
if (expected == 1) {
ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
} else {
ASSERT_STR_EQ(("bad last half of cookie"), buf, &cookie[i]);
}
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
i++;
}
static void
tick(int ms, enum ev_type ev, void *arg)
{
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
}
int
main(int argc, const char *argv[])
{
int pipefds[2];
struct mevent *evp, *timer;
ssize_t written;
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 pipefd"), evp, NULL);
for (int i = 0; cookie[i] != 0; i++) {
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);
if (i == pauseat) {
timer = mevent_add(10, EVF_TIMER, tick,
&cookie[pauseat]);
ASSERT_PTR_NEQ(("mevent_add timer"), timer, NULL);
VERBOSE(("disable munch"));
mevent_disable(evp);
}
}
pthread_mutex_lock(&mtx);
mevent_enable(evp);
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
PASS();
}