add changelog for 1.13
[beanstalkd.git] / darwin.c
blobb2713599e8921ebf34ca21fca9dab26987caeba2
1 #include "dat.h"
2 #include <stdint.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #include <sys/event.h>
9 #include <sys/time.h>
11 enum
13 Infinity = 1 << 30
16 static int kq;
19 int
20 sockinit(void)
22 kq = kqueue();
23 if (kq == -1) {
24 twarn("kqueue");
25 return -1;
27 return 0;
31 int
32 sockwant(Socket *s, int rw)
34 int n = 0;
35 struct kevent evs[2] = {{0}};
36 struct kevent *ev = evs;
37 struct timespec ts = {.tv_sec = 0, .tv_nsec = 0};
39 if (s->added) {
40 ev->ident = s->fd;
41 ev->filter = s->added;
42 ev->flags = EV_DELETE;
43 ev++;
44 n++;
47 if (rw) {
48 ev->ident = s->fd;
49 switch (rw) {
50 case 'r':
51 ev->filter = EVFILT_READ;
52 break;
53 case 'w':
54 ev->filter = EVFILT_WRITE;
55 break;
56 default:
57 // check only for hangup
58 ev->filter = EVFILT_READ;
59 ev->fflags = NOTE_LOWAT;
60 ev->data = Infinity;
62 ev->flags = EV_ADD;
63 ev->udata = s;
64 s->added = ev->filter;
65 ev++;
66 n++;
69 return kevent(kq, evs, n, NULL, 0, &ts);
73 int
74 socknext(Socket **s, int64 timeout)
76 int r;
77 struct kevent ev;
78 static struct timespec ts;
80 ts.tv_sec = timeout / 1000000000;
81 ts.tv_nsec = timeout % 1000000000;
82 r = kevent(kq, NULL, 0, &ev, 1, &ts);
83 if (r == -1 && errno != EINTR) {
84 twarn("kevent");
85 return -1;
88 if (r > 0) {
89 *s = ev.udata;
90 if (ev.flags & EV_EOF) {
91 return 'h';
93 switch (ev.filter) {
94 case EVFILT_READ:
95 return 'r';
96 case EVFILT_WRITE:
97 return 'w';
100 return 0;