Sat, 18 Feb 2017 13:27:25 +0100
adds public aio and poll api and asynchronous send_range function
aio and poll api is only implemented on solaris yet
send_file saf uses send_range_aio for single range requests
69 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | ||
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
32 | #include "../util/atomic.h" |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
33 | |
69 | 34 | #include "event_bsd.h" |
35 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
36 | EVHandler* evhandler_create(EventHandlerConfig *cfg) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
37 | EVHandler *ev = malloc(sizeof(EVHandler)); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
38 | ev->current = 0; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
39 | ev->instances = calloc(cfg->nthreads, sizeof(void*)); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
40 | ev->numins = cfg->nthreads; |
69 | 41 | |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
42 | for(int i=0;i<cfg->nthreads;i++) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
43 | EventHandler *handler = malloc(sizeof(EventHandler)); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
44 | ev->instances[i] = handler; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
45 | |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
46 | handler->kqueue = kqueue(); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
47 | if(handler->kqueue == 0) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
48 | // TODO: error |
69 | 49 | return NULL; |
50 | } | |
51 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
52 | SYS_THREAD t = systhread_start( |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
53 | 0, |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
54 | 0, |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
55 | (thrstartfunc)ev_handle_events, |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
56 | handler); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
57 | systhread_detach(t); |
69 | 58 | } |
59 | ||
60 | return ev; | |
61 | } | |
62 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
63 | |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
64 | void ev_handle_events(EventHandler *ev) { |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
65 | struct timespec timeout; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
66 | timeout.tv_nsec = 0; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
67 | timeout.tv_sec = 600; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
68 | |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
69 | struct kevent events[64]; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
70 | struct kevent changes[64]; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
71 | int numchanges = 0; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
72 | |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
73 | for(;;) { |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
74 | // wait for events |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
75 | int nev = kevent(ev->kqueue, changes, numchanges, events, 64, &timeout); |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
76 | if(nev == -1) { |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
77 | // TODO: check for error |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
78 | perror("kevent"); |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
79 | continue; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
80 | } |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
81 | |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
82 | numchanges = 0; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
83 | for(int i=0;i<nev;i++) { |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
84 | Event *event = (Event*)events[i].udata; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
85 | if(event->fn) { |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
86 | int ep = event->events; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
87 | if(event->fn(ev, event)) { |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
88 | if(event->events != ep) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
89 | changes[numchanges++].filter = ev_convert2sys_events(ep); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
90 | } |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
91 | } else if(event->finish) { |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
92 | changes[numchanges++].filter = ev_convert2sys_events(ep); |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
93 | event->finish(ev, event); |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
94 | } |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
95 | } |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
96 | } |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
97 | } |
69 | 98 | } |
99 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
100 | int ev_convert2sys_events(int events) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
101 | int e = 0; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
102 | if((events & EVENT_POLLIN) == EVENT_POLLIN) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
103 | e |= EVFILT_READ; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
104 | } |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
105 | if((events & EVENT_POLLOUT) == EVENT_POLLOUT) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
106 | e |= EVFILT_WRITE; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
107 | } |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
108 | return e; |
69 | 109 | } |
110 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
111 | int ev_pollin(EventHandler *h, int fd, Event *event) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
112 | event->events = EVENT_POLLIN; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
113 | struct kevent kev; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
114 | EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, event); |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
115 | return kevent(h->kqueue, &kev, 1, NULL, 0, NULL); |
69 | 116 | } |
117 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
118 | int ev_pollout(EventHandler *h, int fd, Event *event) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
119 | event->events = EVENT_POLLOUT; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
120 | struct kevent kev; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
121 | EV_SET(&kev, fd, EVFILT_WRITE, EV_ADD, 0, 0, event); |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
122 | return kevent(h->kqueue, &kev, 1, NULL, 0, NULL); |
69 | 123 | } |
124 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
125 | int evt_send(EventHandler *h, Event *event) { |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
126 | return 0; |
69 | 127 | } |