ObjFW  Diff

Differences From Artifact [b202b955cf]:

To Artifact [c4217fefce]:


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
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







+
+
+
+






+
+
+
-
+



-
+









+







#import "OFKernelEventObserver.h"

#import "OFInitializationFailedException.h"
#import "OFObserveFailedException.h"
#import "OFOutOfRangeException.h"

#import "socket_helpers.h"

#ifdef OF_AMIGAOS
# include <proto/exec.h>
#endif

@implementation OFSelectKernelEventObserver
- (instancetype)init
{
	self = [super init];

#ifdef OF_AMIGAOS
	_maxFD = 0;
#else
#ifndef OF_WINDOWS
# ifndef OF_WINDOWS
	if (_cancelFD[0] >= (int)FD_SETSIZE)
		@throw [OFInitializationFailedException
		    exceptionWithClass: self.class];
#endif
# endif

	FD_ZERO(&_readFDs);
	FD_ZERO(&_writeFDs);
	FD_SET(_cancelFD[0], &_readFDs);

	if (_cancelFD[0] > INT_MAX)
		@throw [OFOutOfRangeException exception];

	_maxFD = (int)_cancelFD[0];
#endif

	return self;
}

- (void)of_addObjectForReading: (id <OFReadyForReadingObserving>)object
{
	int fd = object.fileDescriptorForReading;
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
148
149
150
151
152
153
154

155
156
157
158
159
160
161
162







-
+







{
	id const *objects;
	fd_set readFDs;
	fd_set writeFDs;
	struct timeval timeout;
	int events;
#ifdef OF_AMIGAOS
	ULONG execSignalMask;
	ULONG execSignalMask, cancelSignal;
#endif
	size_t count;

	[self of_processQueue];

	if ([self of_processReadBuffers])
		return;
171
172
173
174
175
176
177




178







179
180







181
182
183
184
185
186
187
188
189
190
191
192
193
194

195
196
197
198
199

200
201

202
203
204

205

206
207
208
209
210
211
212
179
180
181
182
183
184
185
186
187
188
189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

219

220
221
222

223
224

225
226
227

228
229
230
231
232
233
234
235
236
237







+
+
+
+
-
+
+
+
+
+
+
+


+
+
+
+
+
+
+













-
+
-



-
+

-
+


-
+

+







	timeout.tv_sec = (time_t)timeInterval;
#else
	timeout.tv_sec = (long)timeInterval;
#endif
	timeout.tv_usec = (int)lrint((timeInterval - timeout.tv_sec) * 1000);

#ifdef OF_AMIGAOS
	if ((cancelSignal = AllocSignal(-1)) == (ULONG)-1)
		@throw [OFObserveFailedException exceptionWithObserver: self
								 errNo: EAGAIN];

	execSignalMask = _execSignalMask;
	execSignalMask = _execSignalMask | (1 << cancelSignal);

	Forbid();

	_waitingTask = FindTask(NULL);
	_cancelSignal = cancelSignal;

	events = WaitSelect(_maxFD + 1, &readFDs, &writeFDs, NULL,
	    (void *)(timeInterval != -1 ? &timeout : NULL), &execSignalMask);

	execSignalMask &= ~(1 << cancelSignal);

	_waitingTask = NULL;
	FreeSignal(_cancelSignal);

	Permit();
#else
	events = select(_maxFD + 1, &readFDs, &writeFDs, NULL,
	    (timeInterval != -1 ? &timeout : NULL));
#endif

	if (events < 0)
		@throw [OFObserveFailedException exceptionWithObserver: self
								 errNo: errno];

#ifdef OF_AMIGAOS
	if (execSignalMask != 0 &&
	    [_delegate respondsToSelector: @selector(execSignalWasReceived:)])
		[_delegate execSignalWasReceived: execSignalMask];
#endif
#else

	if (FD_ISSET(_cancelFD[0], &readFDs)) {
		char buffer;

#ifdef OF_HAVE_PIPE
# ifdef OF_HAVE_PIPE
		OF_ENSURE(read(_cancelFD[0], &buffer, 1) == 1);
#else
# else
		OF_ENSURE(recvfrom(_cancelFD[0], (void *)&buffer, 1, 0, NULL,
		    NULL) == 1);
#endif
# endif
	}
#endif

	objects = _readObjects.objects;
	count = _readObjects.count;

	for (size_t i = 0; i < count; i++) {
		void *pool = objc_autoreleasePoolPush();
		int fd = [objects[i] fileDescriptorForReading];