39#define _Class _ThreadPool
103 const Array *threads = this->threads;
105 synchronized(this->condition, {
106 for (
size_t i = 0; i < threads->
count; i++) {
112 for (
size_t i = 0; i < threads->
count; i++) {
116 assert(this->
head == NULL);
125#pragma mark - ThreadPool
158 if (self->
size == 0) {
167 synchronized(self->condition, {
169 self->tail->
next = work;
177 return isDetached ? NULL : work;
203 const size_t size = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
204#elif defined(_SC_NPROCESSORS_ONLN)
205 const long processors = sysconf(_SC_NPROCESSORS_ONLN);
206 const size_t size = processors > 0 ? (size_t) processors : 1;
208 const size_t size = 1;
226 self->head = work->
next;
228 if (self->tail == work) {
229 self->tail = previous;
253 while (self->head == NULL && $(thread,
isCancelled) ==
false) {
296 assert(self->condition);
301 assert(self->threads);
303 for (
size_t i = 0; i < self->
size; i++) {
327 assert(work->
pool == self);
330 bool isPending =
false;
332 synchronized(self->condition, {
339 $(self->condition, wait);
354#pragma mark - Class lifecycle
381 .name =
"ThreadPool",
384 .interfaceSize =
sizeof(ThreadPoolInterface),
static ident objectAtIndex(const Array *self, size_t index)
static Array * initWithCapacity(Array *self, size_t capacity)
static void addObject(Array *self, const ident obj)
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
static void broadcast(Condition *self)
static void unlock(Lock *self)
static void lock(Lock *self)
static bool isCancelled(const Operation *self)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
static void start(Operation *self)
static void cancel(Operation *self)
static int head(RESTClient *self, const char *url, const char **headers)
static void join(Thread *self, ident *status)
ident(* ThreadFunction)(Thread *thread)
The function type for Thread execution.
static void dequeue(ThreadPool *self, ThreadPoolWork *work)
Removes work from the queue.
static ThreadPoolWork * submit(ThreadPool *self, ThreadFunction function, ident data, bool isDetached)
Queues function, or runs it on the calling thread if this ThreadPool has no Threads.
static ThreadPoolWork * execute(ThreadPool *self, ThreadFunction function, ident data)
static ThreadPool * init(ThreadPool *self)
static void runOnCallingThread(ThreadPoolWork *work)
Runs work on the calling thread, with a Thread that is never started.
Class * _ThreadPool(void)
static ident run(Thread *thread)
ThreadFunction for the ThreadPool Threads.
static void dealloc(Object *self)
static void detach(ThreadPool *self, ThreadFunction function, ident data)
static ident _wait(ThreadPool *self, ThreadPoolWork *work)
static ThreadPool * initWithSize(ThreadPool *self, size_t size)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
ThreadPoolWorkState
The states of a ThreadPoolWork.
A fixed set of Threads that run ThreadFunctions on behalf of the caller.
void * ident
The identity type, similar to Objective-C id.
#define do_once(once, block)
Executes the given block at most one time.
size_t count
The count of elements.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
POSIX Threads conditional variables.
Object is the root Class of The Objectively Class hierarchy.
void dealloc(Object *self)
Frees all resources held by this Object.
A fixed set of Threads that run ThreadFunctions on behalf of the caller.
ident wait(ThreadPool *self, ThreadPoolWork *work)
Waits for the specified ThreadPoolWork to finish, and frees it.
ThreadPool * initWithSize(ThreadPool *self, size_t size)
Initializes this ThreadPool with the specified number of Threads.
size_t size
The number of Threads.
An opaque handle to a ThreadFunction submitted with ThreadPool::execute.
bool isDetached
true if the Thread that runs this ThreadPoolWork frees it.
ident result
The return value of the ThreadFunction.
ThreadPoolWorkState state
The state.
ThreadPoolWork * next
The next ThreadPoolWork in the queue, or NULL.
ThreadFunction function
The ThreadFunction.
ThreadPool * pool
The ThreadPool owning this work (weak reference).