#include <catta/malloc.h>
#include <catta/timeval.h>
#include <catta/simple-watch.h>
+#include "fdutil.h" // catta_set_nonblock
+#include "internal.h" // closesocket
struct CattaWatch {
CattaSimplePoll *simple_poll;
char c = 'W';
assert(s);
- write(s->wakeup_pipe[1], &c, sizeof(c));
+ writepipe(s->wakeup_pipe[1], &c, sizeof(c));
s->wakeup_issued = 1;
}
s->wakeup_issued = 0;
for(;;)
- if (read(s->wakeup_pipe[0], &c, sizeof(c)) != sizeof(c))
+ if (readpipe(s->wakeup_pipe[0], c, sizeof(c)) != sizeof(c))
break;
}
-static int set_nonblock(int fd) {
- int n;
-
- assert(fd >= 0);
-
- if ((n = fcntl(fd, F_GETFL)) < 0)
- return -1;
-
- if (n & O_NONBLOCK)
- return 0;
-
- return fcntl(fd, F_SETFL, n|O_NONBLOCK);
-}
-
static CattaWatch* watch_new(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata) {
CattaWatch *w;
CattaSimplePoll *s;
if (!(s = catta_new(CattaSimplePoll, 1)))
return NULL;
+ winsock_init(); // on Windows, pipe uses sockets; no-op on other platforms
if (pipe(s->wakeup_pipe) < 0) {
catta_free(s);
+ winsock_exit();
return NULL;
}
- set_nonblock(s->wakeup_pipe[0]);
- set_nonblock(s->wakeup_pipe[1]);
+ catta_set_nonblock(s->wakeup_pipe[0]);
+ catta_set_nonblock(s->wakeup_pipe[1]);
s->api.userdata = s;
catta_free(s->pollfds);
if (s->wakeup_pipe[0] >= 0)
- close(s->wakeup_pipe[0]);
+ closepipe(s->wakeup_pipe[0]);
if (s->wakeup_pipe[1] >= 0)
- close(s->wakeup_pipe[1]);
+ closepipe(s->wakeup_pipe[1]);
catta_free(s);
+ winsock_exit(); // match the winsock_init in catta_simple_poll_new
}
static int rebuild(CattaSimplePoll *s) {