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
|
@implementation OFKernelEventObserver_poll
- init
{
self = [super init];
@try {
struct pollfd p = { 0, POLLIN, 0 };
_FDs = [[OFDataArray alloc] initWithItemSize:
sizeof(struct pollfd)];
p.fd = _cancelFD[0];
[_FDs addItem: &p];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_FDs release];
[super dealloc];
}
- (void)OF_addFileDescriptor: (int)fd
withEvents: (short)events
{
struct pollfd *FDs = [_FDs items];
size_t i, count = [_FDs count];
bool found = false;
for (i = 0; i < count; i++) {
if (FDs[i].fd == fd) {
FDs[i].events |= events;
found = true;
break;
}
}
if (!found) {
struct pollfd p = { fd, events, 0 };
[_FDs addItem: &p];
}
}
- (void)OF_removeFileDescriptor: (int)fd
withEvents: (short)events
{
struct pollfd *FDs = [_FDs items];
size_t i, nFDs = [_FDs count];
for (i = 0; i < nFDs; i++) {
if (FDs[i].fd == fd) {
FDs[i].events &= ~events;
if (FDs[i].events == 0)
[_FDs removeItemAtIndex: i];
break;
}
}
}
- (void)OF_addFileDescriptorForReading: (int)fd
{
[self OF_addFileDescriptor: fd
withEvents: POLLIN];
}
- (void)OF_addFileDescriptorForWriting: (int)fd
{
[self OF_addFileDescriptor: fd
withEvents: POLLOUT];
}
- (void)OF_removeFileDescriptorForReading: (int)fd
{
[self OF_removeFileDescriptor: fd
withEvents: POLLIN];
}
- (void)OF_removeFileDescriptorForWriting: (int)fd
{
[self OF_removeFileDescriptor: fd
withEvents: POLLOUT];
}
- (bool)observeForTimeInterval: (of_time_interval_t)timeInterval
{
void *pool = objc_autoreleasePoolPush();
struct pollfd *FDs;
int events;
|
|
>
|
>
|
>
|
|
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
>
|
>
|
|
>
|
|
|
>
|
|
|
>
|
|
|
>
|
|
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
|
@implementation OFKernelEventObserver_poll
- init
{
self = [super init];
@try {
struct pollfd p = { _cancelFD[0], POLLIN, 0 };
_FDs = [[OFDataArray alloc] initWithItemSize:
sizeof(struct pollfd)];
[_FDs addItem: &p];
_maxFD = _cancelFD[0];
_FDToObject = [self allocMemoryWithSize: sizeof(id)
count: _maxFD + 1];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_FDs release];
[super dealloc];
}
- (void)OF_addObject: (id)object
fileDescriptor: (int)fd
events: (short)events
{
struct pollfd *FDs = [_FDs items];
size_t i, count = [_FDs count];
bool found = false;
for (i = 0; i < count; i++) {
if (FDs[i].fd == fd) {
FDs[i].events |= events;
found = true;
break;
}
}
if (!found) {
struct pollfd p = { fd, events, 0 };
if (fd > _maxFD) {
_maxFD = fd;
_FDToObject = [self resizeMemory: _FDToObject
size: sizeof(id)
count: _maxFD + 1];
}
_FDToObject[fd] = object;
[_FDs addItem: &p];
}
}
- (void)OF_removeObject: (id)object
fileDescriptor: (int)fd
events: (short)events
{
struct pollfd *FDs = [_FDs items];
size_t i, nFDs = [_FDs count];
for (i = 0; i < nFDs; i++) {
if (FDs[i].fd == fd) {
FDs[i].events &= ~events;
if (FDs[i].events == 0) {
/*
* TODO: Remove from and resize _FDToObject,
* adjust _maxFD.
*/
[_FDs removeItemAtIndex: i];
}
break;
}
}
}
- (void)OF_addObjectForReading: (id)object
{
[self OF_addObject: object
fileDescriptor: [object fileDescriptorForReading]
events: POLLIN];
}
- (void)OF_addObjectForWriting: (id)object
{
[self OF_addObject: object
fileDescriptor: [object fileDescriptorForWriting]
events: POLLOUT];
}
- (void)OF_removeObjectForReading: (id)object
{
[self OF_removeObject: object
fileDescriptor: [object fileDescriptorForReading]
events: POLLIN];
}
- (void)OF_removeObjectForWriting: (id)object
{
[self OF_removeObject: object
fileDescriptor: [object fileDescriptorForWriting]
events: POLLOUT];
}
- (bool)observeForTimeInterval: (of_time_interval_t)timeInterval
{
void *pool = objc_autoreleasePoolPush();
struct pollfd *FDs;
int events;
|
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
@throw [OFObserveFailedException exceptionWithObserver: self
errNo: errno];
if (events == 0)
return false;
for (i = 0; i < nFDs; i++) {
if (FDs[i].revents & POLLIN) {
if (FDs[i].fd == _cancelFD[0]) {
char buffer;
OF_ENSURE(read(_cancelFD[0], &buffer, 1) == 1);
FDs[i].revents = 0;
|
>
>
>
|
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
@throw [OFObserveFailedException exceptionWithObserver: self
errNo: errno];
if (events == 0)
return false;
for (i = 0; i < nFDs; i++) {
if (FDs[i].fd > _maxFD)
@throw [OFOutOfRangeException exception];
if (FDs[i].revents & POLLIN) {
if (FDs[i].fd == _cancelFD[0]) {
char buffer;
OF_ENSURE(read(_cancelFD[0], &buffer, 1) == 1);
FDs[i].revents = 0;
|