-
Notifications
You must be signed in to change notification settings - Fork 6
/
myconf.c
169 lines (128 loc) · 4.54 KB
/
myconf.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
164
165
166
167
168
169
/*************************************************************************************************
* System-dependent configurations of Tokyo Tyrant
* Copyright (C) 2006-2010 FAL Labs
* This file is part of Tokyo Tyrant.
* Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Tyrant is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "myconf.h"
/*************************************************************************************************
* common settings
*************************************************************************************************/
int _tt_dummyfunc(void){
return 0;
}
int _tt_dummyfuncv(int a, ...){
return 0;
}
/*************************************************************************************************
* epoll emulation
*************************************************************************************************/
#if defined(TTUSEKQUEUE)
#include <sys/event.h>
int _tt_epoll_create(int size){
return kqueue();
}
int _tt_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event){
if(op == EPOLL_CTL_ADD || op == EPOLL_CTL_MOD){
struct kevent kev;
int kfilt = 0;
if(event->events & EPOLLIN){
kfilt |= EVFILT_READ;
} else {
fprintf(stderr, "the epoll emulation supports EPOLLIN only\n");
return -1;
}
if(event->events & EPOLLOUT){
fprintf(stderr, "the epoll emulation supports EPOLLIN only\n");
return -1;
}
int kflags = EV_ADD;
if(event->events & EPOLLONESHOT) kflags |= EV_ONESHOT;
EV_SET(&kev, fd, kfilt, kflags, 0, 0, NULL);
return kevent(epfd, &kev, 1, NULL, 0, NULL) != -1 ? 0 : -1;
} else if(op == EPOLL_CTL_DEL){
struct kevent kev;
int kfilt = EVFILT_READ;
EV_SET(&kev, fd, kfilt, EV_DELETE, 0, 0, NULL);
return kevent(epfd, &kev, 1, NULL, 0, NULL) != -1 || errno == ENOENT ? 0 : -1;
}
return -1;
}
int _tt_epoll_reassoc(int epfd, int fd){
return 0;
}
int _tt_epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout){
div_t td = div(timeout, 1000);
struct timespec ts;
ts.tv_sec = td.quot;
ts.tv_nsec = td.rem * 1000000;
struct kevent kevs[maxevents];
int num = kevent(epfd, NULL, 0, kevs, maxevents, &ts);
if(num == -1) return -1;
for(int i = 0; i < num; i++){
events[i].data.fd = kevs[i].ident;
}
return num;
}
#elif defined(TTUSEEVPORTS)
#include <port.h>
int _tt_epoll_create(int size){
int port = port_create();
if(port < 0) return -1;
return port;
}
int _tt_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event){
if(op == EPOLL_CTL_ADD || op == EPOLL_CTL_MOD){
if(event->events & EPOLLIN){
int result = port_associate(epfd, PORT_SOURCE_FD, fd, POLLIN, event);
if(result == -1) return -1;
return 0;
} else {
return -1;
}
} else if(op == EPOLL_CTL_DEL){
int result = port_dissociate(epfd, PORT_SOURCE_FD, fd);
if(result == -1) return -1;
return 0;
}
return -1;
}
int _tt_epoll_reassoc(int epfd, int fd){
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
if(epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev) != 0) return -1;
return 0;
}
int _tt_epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout){
div_t td = div(timeout, 1000);
struct timespec ts;
ts.tv_sec = td.quot;
ts.tv_nsec = td.rem * 1000000;
port_event_t list[maxevents];
unsigned int actevents = maxevents;
if(port_getn(epfd, list, 0, &actevents, &ts) == -1) return -1;
if(actevents == 0) actevents = 1;
if(actevents > maxevents) actevents = maxevents;
if(port_getn(epfd, list, maxevents, &actevents, &ts) == -1){
if(errno == EINTR){
return 0;
} else if(errno != ETIME){
return -1;
}
}
for(int i = 0; i < actevents; i++){
events[i].data.fd = list[i].portev_object;
}
return actevents;
}
#endif
// END OF FILE