/
tpool-kqueue.c
164 lines (138 loc) · 4.23 KB
/
tpool-kqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* tpool-kqueue.c: kqueue related stuff
*
* Authors:
* Gonzalo Paniagua Javier (gonzalo@ximian.com)
*
* Copyright 2011 Novell, Inc (http://www.novell.com)
*/
struct _tp_kqueue_data {
int fd;
};
typedef struct _tp_kqueue_data tp_kqueue_data;
static void tp_kqueue_modify (gpointer event_data, int fd, int operation, int events, gboolean is_new);
static void tp_kqueue_shutdown (gpointer event_data);
static void tp_kqueue_wait (gpointer event_data);
static gpointer
tp_kqueue_init (SocketIOData *data)
{
tp_kqueue_data *result;
result = g_new0 (tp_kqueue_data, 1);
result->fd = kqueue ();
if (result->fd == -1)
return NULL;
data->shutdown = tp_kqueue_shutdown;
data->modify = tp_kqueue_modify;
data->wait = tp_kqueue_wait;
return result;
}
static void
kevent_change (int kfd, struct kevent *evt, const char *error_str)
{
if (kevent (kfd, evt, 1, NULL, 0, NULL) == -1) {
int err = errno;
g_message ("kqueue(%s): %d %s", error_str, err, g_strerror (err));
}
}
static void
tp_kqueue_modify (gpointer event_data, int fd, int operation, int events, gboolean is_new)
{
tp_kqueue_data *data = event_data;
struct kevent evt;
memset (&evt, 0, sizeof (evt));
if ((events & MONO_POLLIN) != 0) {
EV_SET (&evt, fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_ONESHOT, 0, 0, 0);
kevent_change (data->fd, &evt, "ADD read");
}
if ((events & MONO_POLLOUT) != 0) {
EV_SET (&evt, fd, EVFILT_WRITE, EV_ADD | EV_ENABLE | EV_ONESHOT, 0, 0, 0);
kevent_change (data->fd, &evt, "ADD write");
}
}
static void
tp_kqueue_shutdown (gpointer event_data)
{
tp_kqueue_data *data = event_data;
close (data->fd);
g_free (data);
}
#define KQUEUE_NEVENTS 128
static void
tp_kqueue_wait (gpointer p)
{
SocketIOData *socket_io_data;
int kfd;
MonoInternalThread *thread;
struct kevent *events, *evt;
int ready = 0, i;
gpointer async_results [KQUEUE_NEVENTS * 2]; // * 2 because each loop can add up to 2 results here
gint nresults;
tp_kqueue_data *data;
socket_io_data = p;
data = socket_io_data->event_data;
kfd = data->fd;
thread = mono_thread_internal_current ();
events = g_new0 (struct kevent, KQUEUE_NEVENTS);
while (1) {
mono_gc_set_skip_thread (TRUE);
do {
if (ready == -1) {
if (THREAD_WANTS_A_BREAK (thread))
mono_thread_interruption_checkpoint ();
}
ready = kevent (kfd, NULL, 0, events, KQUEUE_NEVENTS, NULL);
} while (ready == -1 && errno == EINTR);
mono_gc_set_skip_thread (FALSE);
if (ready == -1) {
int err = errno;
g_free (events);
if (err != EBADF)
g_warning ("kevent wait: %d %s", err, g_strerror (err));
return;
}
EnterCriticalSection (&socket_io_data->io_lock);
if (socket_io_data->inited == 3) {
g_free (events);
LeaveCriticalSection (&socket_io_data->io_lock);
return; /* cleanup called */
}
nresults = 0;
for (i = 0; i < ready; i++) {
int fd;
MonoMList *list;
MonoObject *ares;
evt = &events [i];
fd = evt->ident;
list = mono_g_hash_table_lookup (socket_io_data->sock_to_state, GINT_TO_POINTER (fd));
if (list != NULL && (evt->filter == EVFILT_READ || (evt->flags & EV_ERROR) != 0)) {
ares = get_io_event (&list, MONO_POLLIN);
if (ares != NULL)
async_results [nresults++] = ares;
}
if (list != NULL && (evt->filter == EVFILT_WRITE || (evt->flags & EV_ERROR) != 0)) {
ares = get_io_event (&list, MONO_POLLOUT);
if (ares != NULL)
async_results [nresults++] = ares;
}
if (list != NULL) {
int p;
mono_g_hash_table_replace (socket_io_data->sock_to_state, GINT_TO_POINTER (fd), list);
p = get_events_from_list (list);
if (evt->filter == EVFILT_READ && (p & MONO_POLLIN) != 0) {
EV_SET (evt, fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_ONESHOT, 0, 0, 0);
kevent_change (kfd, evt, "READD read");
}
if (evt->filter == EVFILT_WRITE && (p & MONO_POLLOUT) != 0) {
EV_SET (evt, fd, EVFILT_WRITE, EV_ADD | EV_ENABLE | EV_ONESHOT, 0, 0, 0);
kevent_change (kfd, evt, "READD write");
}
} else {
mono_g_hash_table_remove (socket_io_data->sock_to_state, GINT_TO_POINTER (fd));
}
}
LeaveCriticalSection (&socket_io_data->io_lock);
threadpool_append_jobs (&async_io_tp, (MonoObject **) async_results, nresults);
mono_gc_bzero (async_results, sizeof (gpointer) * nresults);
}
}
#undef KQUEUE_NEVENTS