Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
ThreadPool Struct Reference

#include <ThreadPool.h>

Overview

A fixed set of Threads that run ThreadFunctions on behalf of the caller.

Submitted functions wait in a first-in, first-out queue until a Thread is free. Each ThreadFunction receives a Thread whose data is the user data that was submitted with the function.

Deallocating a ThreadPool sets isCancelled on its Threads, which then run the functions still in the queue before they exit. A long-running function MAY check isCancelled to return early. A function MUST NOT cancel the Thread that it is given.

A function runs on the calling thread, with a Thread that is never started and never cancelled, in two cases: ThreadPool::wait is called while the function is still in the queue, or the size of the ThreadPool is 0. A function MUST NOT join, kill or detach that Thread, and MUST NOT rely on isCancelled alone to return, because it is never set.

Remarks
A ThreadPool MUST NOT be deallocated by one of its own Threads.

Definition at line 64 of file ThreadPool.h.

Inheritance diagram for ThreadPool:
Object

Properties

Object object
 The superclass.
 
size_t size
 The number of Threads.
 
- Properties inherited from Object
Class * clazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class * _ThreadPool (void)
 The ThreadPool archetype.
 
void detach (ThreadPool *self, ThreadFunction function, ident data)
 Queues function without a handle to wait on.
 
ThreadPoolWork * execute (ThreadPool *self, ThreadFunction function, ident data)
 Queues function, returning a handle to wait on.
 
ThreadPool * init (ThreadPool *self)
 Initializes this ThreadPool with one Thread per online processor.
 
ThreadPool * initWithSize (ThreadPool *self, size_t size)
 Initializes this ThreadPool with the specified number of Threads.
 
ident wait (ThreadPool *self, ThreadPoolWork *work)
 Waits for the specified ThreadPoolWork to finish, and frees it.
 
- Methods inherited from Object
Class * _Object (void)
 The Object archetype.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

ThreadPoolInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Property Details

◆ interface

ThreadPoolInterface* ThreadPool::interface[0]
protected

The interface type.

Definition at line 75 of file ThreadPool.h.

◆ object

Object ThreadPool::object

The superclass.

Definition at line 69 of file ThreadPool.h.

◆ size

size_t ThreadPool::size

The number of Threads.

Remarks
Read-only.

Definition at line 93 of file ThreadPool.h.

Method Details

◆ _ThreadPool()

Class * _ThreadPool ( void  )

The ThreadPool archetype.

Returns
The ThreadPool Class.

Definition at line 375 of file ThreadPool.c.

375 {
376 static Class *clazz;
377 static Once once;
378
379 do_once(&once, {
380 clazz = _initialize(&(const ClassDef) {
381 .name = "ThreadPool",
382 .superclass = _Object(),
383 .instanceSize = sizeof(ThreadPool),
384 .interfaceSize = sizeof(ThreadPoolInterface),
385 .initialize = initialize,
386 });
387 });
388
389 return clazz;
390}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:90
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
A fixed set of Threads that run ThreadFunctions on behalf of the caller.
Definition ThreadPool.h:64

◆ detach()

void detach ( ThreadPool *  self,
ThreadFunction  function,
ident  data 
)

Queues function without a handle to wait on.

Parameters
selfThe ThreadPool.
functionThe ThreadFunction.
dataThe user data, available to function as thread->data.
Remarks
The return value of function is discarded.

Definition at line 184 of file ThreadPool.c.

184 {
185 submit(self, function, data, true);
186}
static Data * data(void)
Definition Data.c:286
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.
Definition ThreadPool.c:145

◆ execute()

ThreadPoolWork * execute ( ThreadPool *  self,
ThreadFunction  function,
ident  data 
)

Queues function, returning a handle to wait on.

Parameters
selfThe ThreadPool.
functionThe ThreadFunction.
dataThe user data, available to function as thread->data.
Returns
The ThreadPoolWork, which MUST be passed to ThreadPool::wait.

Definition at line 192 of file ThreadPool.c.

192 {
193 return submit(self, function, data, false);
194}

◆ init()

ThreadPool * init ( ThreadPool *  self)

Initializes this ThreadPool with one Thread per online processor.

Parameters
selfThe ThreadPool.
Returns
The initialized ThreadPool, or NULL on error.

Definition at line 200 of file ThreadPool.c.

200 {
201
202#if defined(_WIN32)
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;
207#else
208 const size_t size = 1;
209#endif
210
211 return $(self, initWithSize, size);
212}
ThreadPool * initWithSize(ThreadPool *self, size_t size)
Initializes this ThreadPool with the specified number of Threads.
Definition ThreadPool.c:290
size_t size
The number of Threads.
Definition ThreadPool.h:93

◆ initWithSize()

ThreadPool * initWithSize ( ThreadPool *  self,
size_t  size 
)

Initializes this ThreadPool with the specified number of Threads.

Parameters
selfThe ThreadPool.
sizeThe number of Threads. With 0, every function runs on the calling thread.
Returns
The initialized ThreadPool, or NULL on error.

Definition at line 290 of file ThreadPool.c.

290 {
291
292 self = (ThreadPool *) super(Object, self, init);
293 if (self) {
294
295 self->condition = $(alloc(Condition), init);
296 assert(self->condition);
297
298 self->size = size;
299
300 self->threads = $(alloc(Array), initWithCapacity, size);
301 assert(self->threads);
302
303 for (size_t i = 0; i < self->size; i++) {
304 Thread *thread = $(alloc(Thread), initWithFunction, run, self);
305 assert(thread);
306
307 $(self->threads, addObject, thread);
308 $(thread, start);
309
310 release(thread);
311 }
312 }
313
314 return self;
315}
static Array * initWithCapacity(Array *self, size_t capacity)
Definition Array.c:453
static void addObject(Array *self, const ident obj)
Definition Array.c:181
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
#define super(type, obj, method,...)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:125
static void start(Operation *self)
Definition Operation.c:185
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Threads.
Arrays.
Definition Array.h:56
POSIX Threads conditional variables.
Definition Condition.h:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
POSIX Threads.
Definition Thread.h:53
ThreadPool * init(ThreadPool *self)
Initializes this ThreadPool with one Thread per online processor.
Definition ThreadPool.c:200

◆ wait()

ident wait ( ThreadPool *  self,
ThreadPoolWork *  work 
)

Waits for the specified ThreadPoolWork to finish, and frees it.

Parameters
selfThe ThreadPool.
workThe ThreadPoolWork returned by ThreadPool::execute, or NULL.
Returns
The return value of the ThreadFunction, or NULL if work is NULL.
Remarks
If work is still in the queue, it runs on the calling thread. So a ThreadFunction MAY wait on other work of its own ThreadPool without a deadlock.

The documentation for this struct was generated from the following files: