ObjFW  Diff

Differences From Artifact [5fc72f8432]:

To Artifact [70e4fc157b]:


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 *
 * 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.
 */

/*
 * TODO:
 *  - What to do when threads are running and the thread pool is deallocated?
 *  - How to tell the threads to terminate because they won't get new work?
 */

#include "config.h"

#import "OFThreadPool.h"
#import "OFArray.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFAutoreleasePool.h"







<
<
<
<
<
<







10
11
12
13
14
15
16






17
18
19
20
21
22
23
 *
 * 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"

#import "OFThreadPool.h"
#import "OFArray.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFAutoreleasePool.h"
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
180
181
182
183





184
185
186






187
188
189
190





191

192
193
194
195
196
197
198
199
200
201
		[object performSelector: selector
			     withObject: object];
}
@end

@interface OFThreadPoolThread: OFThread
{
	OFThreadPool *threadPool;




}

+ threadWithThreadPool: (OFThreadPool*)threadPool;
- initWithThreadPool: (OFThreadPool*)threadPool;
@end

@implementation OFThreadPoolThread
+ threadWithThreadPool: (OFThreadPool*)threadPool
{
	return [[[self alloc] initWithThreadPool: threadPool] autorelease];
}

- initWithThreadPool: (OFThreadPool*)threadPool_
{
	self = [super init];


	threadPool = threadPool_;








	return self;
}










- (id)main
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];






	for (;;) {
		OFThreadPoolJob *job;

		[threadPool->queueCondition lock];
		@try {
			of_list_object_t *listObject;






			listObject = [threadPool->queue firstListObject];

			while (listObject == NULL) {
				[threadPool->queueCondition wait];
				listObject =





				    [threadPool->queue firstListObject];
			}

			job = [[listObject->object retain] autorelease];
			[threadPool->queue removeListObject: listObject];
		} @finally {
			[threadPool->queueCondition unlock];





		}

		[job perform];






		[pool releaseObjects];

		[threadPool->countCondition lock];
		@try {





			threadPool->doneCount++;

			[threadPool->countCondition signal];
		} @finally {
			[threadPool->countCondition unlock];
		}
	}
}
@end

@implementation OFThreadPool
+ threadPool







|
>
>
>
>












|



>
|
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>




>
>
>
>
>




|



>
>
>
>
>
|


|
|
>
>
>
>
>
|



|

|
>
>
>
>
>



>
>
>
>
>
>


|

>
>
>
>
>
|
>
|

|







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
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
238
239
240
241
242
243
244
245
246
247
248
		[object performSelector: selector
			     withObject: object];
}
@end

@interface OFThreadPoolThread: OFThread
{
	OFList *queue;
	OFCondition *queueCondition, *countCondition;
@public
	volatile BOOL terminate;
	volatile int *doneCount;
}

+ threadWithThreadPool: (OFThreadPool*)threadPool;
- initWithThreadPool: (OFThreadPool*)threadPool;
@end

@implementation OFThreadPoolThread
+ threadWithThreadPool: (OFThreadPool*)threadPool
{
	return [[[self alloc] initWithThreadPool: threadPool] autorelease];
}

- initWithThreadPool: (OFThreadPool*)threadPool
{
	self = [super init];

	@try {
		queue = [threadPool->queue retain];
		queueCondition = [threadPool->queueCondition retain];
		countCondition = [threadPool->countCondition retain];
		doneCount = &threadPool->doneCount;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[queue release];
	[queueCondition release];
	[countCondition release];

	[super dealloc];
}

- (id)main
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	if (terminate) {
		[pool release];
		return nil;
	}

	for (;;) {
		OFThreadPoolJob *job;

		[queueCondition lock];
		@try {
			of_list_object_t *listObject;

			if (terminate) {
				[pool release];
				return nil;
			}

			listObject = [queue firstListObject];

			while (listObject == NULL) {
				[queueCondition wait];

				if (terminate) {
					[pool release];
					return nil;
				}

				listObject = [queue firstListObject];
			}

			job = [[listObject->object retain] autorelease];
			[queue removeListObject: listObject];
		} @finally {
			[queueCondition unlock];
		}

		if (terminate) {
			[pool release];
			return nil;
		}

		[job perform];

		if (terminate) {
			[pool release];
			return nil;
		}

		[pool releaseObjects];

		[countCondition lock];
		@try {
			if (terminate) {
				[pool release];
				return nil;
			}

			(*doneCount)++;

			[countCondition signal];
		} @finally {
			[countCondition unlock];
		}
	}
}
@end

@implementation OFThreadPool
+ threadPool
253
254
255
256
257
258
259




















260
261
262
263
264
265
266
	}

	return self;
}

- (void)dealloc
{




















	[threads release];
	[queue release];
	[queueCondition release];
	[countCondition release];

	[super dealloc];
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	}

	return self;
}

- (void)dealloc
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	[queueCondition lock];
	@try {
		[countCondition lock];
		@try {
			OFEnumerator *enumerator = [threads objectEnumerator];
			OFThreadPoolThread *thread;

			while ((thread = [enumerator nextObject]) != nil)
				thread->terminate = YES;
		} @finally {
			[countCondition unlock];
		}

		[queueCondition broadcast];
	} @finally {
		[queueCondition unlock];
	}
	[pool release];

	[threads release];
	[queue release];
	[queueCondition release];
	[countCondition release];

	[super dealloc];
}