ObjFW  Check-in [4ac1256458]

Overview
Comment:OFThreadPool: Terminate threads on deallocation.

Threads will not terminate instantly, but after finishing their current
job.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4ac12564583b14dff4da0cb404acd0bcfd8183af4728d1973d6b209f298f2aed
User & Date: js on 2012-03-21 15:39:31
Other Links: manifest | tags
Context
2012-03-21
17:33
objc_setProperty(): Use a signed char for copy. check-in: d989794cc2 user: js tags: trunk
15:39
OFThreadPool: Terminate threads on deallocation. check-in: 4ac1256458 user: js tags: trunk
14:47
OFObject: Use owner as a sentinel for pre_mem. check-in: ce1c7fe940 user: js tags: trunk
Changes

Modified src/OFThreadPool.h from [7a259d1129] to [2701771c31].

23
24
25
26
27
28
29



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@class OFMutableArray;
@class OFList;
@class OFCondition;
@class OFThreadPoolJob;

/**
 * \brief A class providing a pool of reusable threads.



 */
@interface OFThreadPool: OFObject
{
	size_t size;
	OFMutableArray *threads;
	int count;
@public
	OFList *queue;
	OFCondition *queueCondition;
	int doneCount;
	OFCondition *countCondition;
}

/**
 * \brief Returns a new thread pool with one thread for each core in the system.
 *
 * \warning If for some reason the number of cores in the system could not be







>
>
>





|



|







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
@class OFMutableArray;
@class OFList;
@class OFCondition;
@class OFThreadPoolJob;

/**
 * \brief A class providing a pool of reusable threads.
 *
 * \note When the thread pool is released, all threads will terminate after
 *	 they finish the job they are currently processing.
 */
@interface OFThreadPool: OFObject
{
	size_t size;
	OFMutableArray *threads;
	volatile int count;
@public
	OFList *queue;
	OFCondition *queueCondition;
	volatile int doneCount;
	OFCondition *countCondition;
}

/**
 * \brief Returns a new thread pool with one thread for each core in the system.
 *
 * \warning If for some reason the number of cores in the system could not be

Modified src/OFThreadPool.m from [5fc72f8432] to [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];
}