ObjFW  Check-in [4f6270da38]

Overview
Comment:Add support for kqueue.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4f6270da380dbc7ad110305b411eb8d35ab2afe289cc9bae05d6dae9ac05a35e
User & Date: js on 2011-09-22 01:11:45
Other Links: manifest | tags
Context
2011-09-22
02:20
Use rint() instead of nearbyint(). check-in: 45b499fb11 user: js tags: trunk
01:11
Add support for kqueue. check-in: 4f6270da38 user: js tags: trunk
2011-09-21
20:11
More precision for microseconds. check-in: 49ec1216cd user: js tags: trunk
Changes

Modified configure.ac from [a3510ce765] to [cccbc97d62].

462
463
464
465
466
467
468




469
470
471
472
473
474
475
AC_CHECK_FUNC(gmtime_r, [
	AC_DEFINE(HAVE_GMTIME_R, 1, [Whether we have gmtime_r])
])
AC_CHECK_FUNC(localtime_r, [
	AC_DEFINE(HAVE_LOCALTIME_R, 1, [Whether we have localtime_r])
])





AC_CHECK_HEADER(poll.h, [
	AC_DEFINE(HAVE_POLL_H, 1, [Whether we have poll.h])
	AC_SUBST(OFSTREAMOBSERVER_POLL_M, "OFStreamObserver_poll.m")
])
AC_CHECK_HEADERS(sys/select.h, [
	AC_DEFINE(OF_HAVE_SYS_SELECT_H, 1, [Whether we have sys/select.h])
	AC_SUBST(OFSTREAMOBSERVER_SELECT_M, "OFStreamObserver_select.m")







>
>
>
>







462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
AC_CHECK_FUNC(gmtime_r, [
	AC_DEFINE(HAVE_GMTIME_R, 1, [Whether we have gmtime_r])
])
AC_CHECK_FUNC(localtime_r, [
	AC_DEFINE(HAVE_LOCALTIME_R, 1, [Whether we have localtime_r])
])

AC_CHECK_FUNC(kqueue, [
	AC_DEFINE(HAVE_KQUEUE, 1, [Whether we have kqueue])
	AC_SUBST(OFSTREAMOBSERVER_KQUEUE_M, "OFStreamObserver_kqueue.m")
])
AC_CHECK_HEADER(poll.h, [
	AC_DEFINE(HAVE_POLL_H, 1, [Whether we have poll.h])
	AC_SUBST(OFSTREAMOBSERVER_POLL_M, "OFStreamObserver_poll.m")
])
AC_CHECK_HEADERS(sys/select.h, [
	AC_DEFINE(OF_HAVE_SYS_SELECT_H, 1, [Whether we have sys/select.h])
	AC_SUBST(OFSTREAMOBSERVER_SELECT_M, "OFStreamObserver_select.m")

Modified extra.mk.in from [f48b4e976e] to [ea8ed4b43a].

15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
MACH_ALIAS_LIST = @MACH_ALIAS_LIST@
OFBLOCKTESTS_M = @OFBLOCKTESTS_M@
OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@
OBJC_SYNC_M = @OBJC_SYNC_M@
OFHTTPREQUESTTESTS_M = @OFHTTPREQUESTTESTS_M@
OFPLUGIN_M = @OFPLUGIN_M@
OFPLUGINTESTS_M = @OFPLUGINTESTS_M@

OFSTREAMOBSERVER_POLL_M = @OFSTREAMOBSERVER_POLL_M@
OFSTREAMOBSERVER_SELECT_M = @OFSTREAMOBSERVER_SELECT_M@
OFTHREAD_M = @OFTHREAD_M@
OFTHREADTESTS_M = @OFTHREADTESTS_M@
PROPERTIESTESTS_M = @PROPERTIESTESTS_M@
REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@
TESTPLUGIN = @TESTPLUGIN@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@
THREADING_H = @THREADING_H@







>










15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
MACH_ALIAS_LIST = @MACH_ALIAS_LIST@
OFBLOCKTESTS_M = @OFBLOCKTESTS_M@
OBJC_PROPERTIES_M = @OBJC_PROPERTIES_M@
OBJC_SYNC_M = @OBJC_SYNC_M@
OFHTTPREQUESTTESTS_M = @OFHTTPREQUESTTESTS_M@
OFPLUGIN_M = @OFPLUGIN_M@
OFPLUGINTESTS_M = @OFPLUGINTESTS_M@
OFSTREAMOBSERVER_KQUEUE_M = @OFSTREAMOBSERVER_KQUEUE_M@
OFSTREAMOBSERVER_POLL_M = @OFSTREAMOBSERVER_POLL_M@
OFSTREAMOBSERVER_SELECT_M = @OFSTREAMOBSERVER_SELECT_M@
OFTHREAD_M = @OFTHREAD_M@
OFTHREADTESTS_M = @OFTHREADTESTS_M@
PROPERTIESTESTS_M = @PROPERTIESTESTS_M@
REEXPORT_LIBOBJC = @REEXPORT_LIBOBJC@
TESTPLUGIN = @TESTPLUGIN@
TESTS = @TESTS@
TEST_LAUNCHER = @TEST_LAUNCHER@
THREADING_H = @THREADING_H@

Modified src/Makefile from [8b454fce85] to [131aa12a4d].

77
78
79
80
81
82
83

84
85
86
87
88
89
90
	OFArray_subarray.m		\
	OFCountedSet_hashtable.m	\
	OFDictionary_hashtable.m	\
	OFMutableArray_adjacent.m	\
	OFMutableDictionary_hashtable.m	\
	OFMutableSet_hashtable.m	\
	OFSet_hashtable.m		\

	${OFSTREAMOBSERVER_POLL_M}	\
	${OFSTREAMOBSERVER_SELECT_M}	\
	${ASPRINTF_M}			\
	${FOUNDATION_COMPAT_M}		\
	iso_8859_15.m			\
	windows_1252.m			\
	${OBJC_PROPERTIES_M}		\







>







77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	OFArray_subarray.m		\
	OFCountedSet_hashtable.m	\
	OFDictionary_hashtable.m	\
	OFMutableArray_adjacent.m	\
	OFMutableDictionary_hashtable.m	\
	OFMutableSet_hashtable.m	\
	OFSet_hashtable.m		\
	${OFSTREAMOBSERVER_KQUEUE_M}	\
	${OFSTREAMOBSERVER_POLL_M}	\
	${OFSTREAMOBSERVER_SELECT_M}	\
	${ASPRINTF_M}			\
	${FOUNDATION_COMPAT_M}		\
	iso_8859_15.m			\
	windows_1252.m			\
	${OBJC_PROPERTIES_M}		\

Modified src/OFStreamObserver.m from [2c9b5a07cf] to [c099293000].

29
30
31
32
33
34
35



36
37
38
39
40
41
42
#import "OFDataArray.h"
#ifdef _WIN32
# import "OFTCPSocket.h"
#endif
#import "OFThread.h"
#import "OFAutoreleasePool.h"




#ifdef HAVE_POLL_H
# import "OFStreamObserver_poll.h"
#endif
#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)
# import "OFStreamObserver_select.h"
#endif








>
>
>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#import "OFDataArray.h"
#ifdef _WIN32
# import "OFTCPSocket.h"
#endif
#import "OFThread.h"
#import "OFAutoreleasePool.h"

#ifdef HAVE_KQUEUE
# import "OFStreamObserver_kqueue.h"
#endif
#ifdef HAVE_POLL_H
# import "OFStreamObserver_poll.h"
#endif
#if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)
# import "OFStreamObserver_select.h"
#endif

56
57
58
59
60
61
62
63








64
65
66
67
68
69
70

@implementation OFStreamObserver
+ observer
{
	return [[[self alloc] init] autorelease];
}

#if defined(HAVE_POLL_H)








+ alloc
{
	if (self == [OFStreamObserver class])
		return [OFStreamObserver_poll alloc];

	return [super alloc];
}







|
>
>
>
>
>
>
>
>







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

@implementation OFStreamObserver
+ observer
{
	return [[[self alloc] init] autorelease];
}

#if defined(HAVE_KQUEUE)
+ alloc
{
	if (self == [OFStreamObserver class])
		return [OFStreamObserver_kqueue alloc];

	return [super alloc];
}
#elif defined(HAVE_POLL_H)
+ alloc
{
	if (self == [OFStreamObserver class])
		return [OFStreamObserver_poll alloc];

	return [super alloc];
}

Added src/OFStreamObserver_kqueue.h version [e08f6267e6].



















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFStreamObserver.h"

@interface OFStreamObserver_kqueue: OFStreamObserver
{
	int kernelQueue;
	int FDs;
	struct kevent *eventList;
}
@end

Added src/OFStreamObserver_kqueue.m version [828093c668].







































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <unistd.h>

#include <assert.h>

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>

#import "OFStreamObserver_kqueue.h"
#import "OFDataArray.h"

#import "OFInitializationFailedException.h"
#import "OFOutOfMemoryException.h"

@interface OFStreamObserver_kqueue (addEventForFileDescriptor)
- (void)_addEventForFileDescriptor: (int)fd
			    filter: (int16_t)filter;
@end

@implementation OFStreamObserver_kqueue
- init
{
	self = [super init];

	@try {
		if ((kernelQueue = kqueue()) == -1)
			@throw [OFInitializationFailedException
			    newWithClass: isa];

		[self _addEventForFileDescriptor: cancelFD[0]
					  filter: EVFILT_READ];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	close(kernelQueue);

	[super dealloc];
}

- (void)_addEventForFileDescriptor: (int)fd
			    filter: (int16_t)filter
{
	struct kevent event, result;

	eventList = [self resizeMemory: eventList
			      toNItems: FDs + 1
				ofSize: sizeof(struct kevent)];

	EV_SET(&event, fd, filter, EV_ADD | EV_RECEIPT, 0, 0, 0);

	if (kevent(kernelQueue, &event, 1, &result, 1, NULL) != 1 ||
	    result.data != 0)
		/* FIXME: Find a better exception */
		@throw [OFInitializationFailedException newWithClass: isa];

	FDs++;
}

- (void)_removeEventForFileDescriptor: (int)fd
			       filter: (int16_t)filter
{
	struct kevent event, result;

	EV_SET(&event, fd, filter, EV_DELETE | EV_RECEIPT, 0, 0, 0);

	if (kevent(kernelQueue, &event, 1, &result, 1, NULL) != 1)
		/* FIXME: Find a better exception */
		@throw [OFInitializationFailedException newWithClass: isa];

	@try {
		eventList = [self resizeMemory: eventList
				      toNItems: FDs - 1
					ofSize: sizeof(struct kevent)];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only made it smaller */
		[e release];
	}

	FDs--;
}

- (void)_addFileDescriptorForReading: (int)fd
{
	[self _addEventForFileDescriptor: fd
				  filter: EVFILT_READ];
}

- (void)_addFileDescriptorForWriting: (int)fd
{
	[self _addEventForFileDescriptor: fd
				  filter: EVFILT_WRITE];
}

- (void)_removeFileDescriptorForReading: (int)fd
{
	[self _removeEventForFileDescriptor: fd
				     filter: EVFILT_READ];
}

- (void)_removeFileDescriptorForWriting: (int)fd
{
	[self _removeEventForFileDescriptor: fd
				     filter: EVFILT_WRITE];
}

- (BOOL)observeWithTimeout: (int)timeout
{
	struct timespec timespec = { timeout, 0 };
	int i, events;

	[self _processQueue];

	if ([self _processCache])
		return YES;

	events = kevent(kernelQueue, NULL, 0, eventList, FDs,
	    (timeout == -1 ? NULL : &timespec));

	if (events == -1)
		/* FIXME: Throw something */;
	if (events == 0)
		return NO;

	for (i = 0; i < events; i++) {
		if (eventList[i].ident == cancelFD[0]) {
			char buffer;

			assert(read(cancelFD[0], &buffer, 1) > 0);

			continue;
		}

		if (eventList[i].flags & EV_ERROR) {
			[delegate streamDidReceiveException:
			    FDToStream[eventList[i].ident]];
			continue;
		}

		switch (eventList[i].filter) {
		case EVFILT_READ:
			[delegate streamIsReadyForReading:
			    FDToStream[eventList[i].ident]];
			break;
		case EVFILT_WRITE:
			[delegate streamIsReadyForWriting:
			    FDToStream[eventList[i].ident]];
		default:
			assert(0);
		}
	}

	return YES;
}
@end