ObjFW  Diff

Differences From Artifact [0f25fb90be]:

To Artifact [ef7d60eaa2]:


23
24
25
26
27
28
29
30
31
32
33
34

35
36
37
38
39
40
41
23
24
25
26
27
28
29

30
31
32
33
34
35
36
37
38
39
40
41







-




+








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

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

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

#import "autorelease.h"
#import "macros.h"

#define EVENTLIST_SIZE 64

@implementation OFStreamObserver_kqueue
- init
{
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
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







-
+







-
+


+
+








-


-









-
+
-

-









+
+




-
+







-




-





+


-
-




	EV_SET(&event, fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
	[changeList addItem: &event];
}

- (BOOL)observeWithTimeout: (int)timeout
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	void *pool = objc_autoreleasePoolPush();
	struct timespec timespec = { timeout, 0 };
	struct kevent eventList[EVENTLIST_SIZE];
	int i, events;

	[self _processQueue];

	if ([self _processCache]) {
		[pool release];
		objc_autoreleasePoolPop(pool);
		return YES;
	}

	objc_autoreleasePoolPop(pool);

	events = kevent(kernelQueue, [changeList cArray],
	    (int)[changeList count], eventList, EVENTLIST_SIZE,
	    (timeout == -1 ? NULL : &timespec));

	if (events == -1) {
		switch (errno) {
		case EINTR:
			[pool release];
			return NO;
		case ENOMEM:
			[pool release];
			@throw [OFOutOfMemoryException
			    exceptionWithClass: [self class]];
		default:
			assert(0);
		}
	}

	[changeList removeAllItems];

	if (events == 0) {
	if (events == 0)
		[pool release];
		return NO;
	}

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

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

			continue;
		}

		pool = objc_autoreleasePoolPush();

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

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

		objc_autoreleasePoolPop(pool);
	}

	[pool release];

	return YES;
}
@end