ObjFW
OFThreadPool.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "OFObject.h"
18 
19 OF_ASSUME_NONNULL_BEGIN
20 
23 #ifdef OF_HAVE_BLOCKS
24 
27 typedef void (^of_thread_pool_block_t)(void);
28 #endif
29 
30 @class OFCondition;
31 @class OFList OF_GENERIC(ObjectType);
32 @class OFMutableArray OF_GENERIC(ObjectType);
33 @class OFThreadPoolJob;
34 
44 {
45  size_t _size;
46  OFMutableArray *_threads;
47  volatile int _count;
48 @public
49  OFList *_queue;
50  OFCondition *_queueCondition;
51  volatile int _doneCount;
52  OFCondition *_countCondition;
53 }
54 
63 + (instancetype)threadPool;
64 
71 + (instancetype)threadPoolWithSize: (size_t)size;
72 
80 - initWithSize: (size_t)size OF_DESIGNATED_INITIALIZER;
81 
90 - (void)dispatchWithTarget: (id)target
91  selector: (SEL)selector
92  object: (nullable id)object;
93 
94 #ifdef OF_HAVE_BLOCKS
95 
100 - (void)dispatchWithBlock: (of_thread_pool_block_t)block;
101 #endif
102 
106 - (void)waitUntilDone;
107 
113 - (size_t)size;
114 @end
115 
116 OF_ASSUME_NONNULL_END
void(^ of_thread_pool_block_t)(void)
A block for a job which should be executed in a thread pool.
Definition: OFThreadPool.h:27
void waitUntilDone()
Waits until all jobs are done.
Definition: OFThreadPool.m:312
instancetype threadPool()
Returns a new thread pool with one thread for each core in the system.
Definition: OFThreadPool.m:221
size_t size()
Returns the size of the thread pool.
Definition: OFThreadPool.m:353
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
A class providing a pool of reusable threads.
Definition: OFThreadPool.h:43
An abstract class for storing, adding and removing objects in an array.
Definition: OFMutableArray.h:40
A class which provides easy to use double-linked lists.
Definition: OFList.h:47
A class implementing a condition variable for thread synchronization.
Definition: OFCondition.h:28