Thread pool
Functions | Variables
threadpool.c File Reference
#include "threadpool.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
Include dependency graph for threadpool.c:

Functions

static void push (queue_t *queue, runnable_t runnable)
 Add a new element to the queue. More...
 
static runnable_t pop (queue_t *queue)
 Read and pop the first element in queue. More...
 
static void free_queue (queue_t *queue)
 Deallocate the queue. More...
 
static void sigint_destroy ()
 Destroy all pools after receiving SIGINT. More...
 
 __attribute__ ((constructor))
 Constructor of the exception handler. More...
 
 __attribute__ ((destructor))
 Destructor of the handler. More...
 
static void * thread_function (void *arg)
 Function run by every thread. More...
 
int thread_pool_init (thread_pool_t *pool, size_t num_threads)
 Initialize the thread-pool. More...
 
void thread_pool_destroy (thread_pool_t *pool)
 Destroy the thread-pool. More...
 
int defer (struct thread_pool *pool, runnable_t runnable)
 Add a new task to the pool. More...
 

Variables

struct {
   struct sigaction   action
 new sigaction;
 
   struct sigaction   old_action
 old sigaction;
 
   thread_pool_t **   known_pools
 array of pointers to all known pools;
 
   size_t   pools_size
 size of the known_pools array;
 
   unsigned   last
 number of initialized pools;
 
   sigset_t   block_mask
 mask with blocked signals;
 
handler
 Signal handler.
 

Detailed Description

Threadpool implementation.

Author
Michał Niedziółka micha.nosp@m.l.ni.nosp@m.edzio.nosp@m.lka@.nosp@m.stude.nosp@m.nts..nosp@m.mimuw.nosp@m..edu.nosp@m..pl
Date
12.02.2020

Function Documentation

◆ __attribute__() [1/2]

__attribute__ ( (constructor)  )

Constructor of the exception handler.

Initialize block_mask with SIGINT. Set handling function and flags.

Here is the call graph for this function:

◆ __attribute__() [2/2]

__attribute__ ( (destructor)  )

Destructor of the handler.

Deallocate exception handler.

◆ defer()

int defer ( thread_pool_t pool,
runnable_t  runnable 
)

Add a new task to the pool.

Parameters
[in,out]pool– pointer to thread-pool;
[in]runnable– task that will be run on the pool.
Returns
0, if defer was finished correctly. Non-zero value, if errors occurred.
Here is the call graph for this function:

◆ free_queue()

static void free_queue ( queue_t queue)
static

Deallocate the queue.

Iterate through every node in queue and free them.

Parameters
[in,out]queue– pointer to the queue;

◆ pop()

static runnable_t pop ( queue_t queue)
static

Read and pop the first element in queue.

Return the first element in the queue. Pop the node from the queue and assign new first node.

Parameters
[in,out]queue– pointer to the queue;
Returns
runnable with the first element in the queue.

◆ push()

static void push ( queue_t queue,
runnable_t  runnable 
)
static

Add a new element to the queue.

Allocate a new node and copy the runnable to it. Push it at the end of the queue.

Parameters
[in,out]queue– pointer to the queue;
[in]runnable– nowe dane do dołączenia do listy;

◆ sigint_destroy()

static void sigint_destroy ( )
static

Destroy all pools after receiving SIGINT.

After SIGINT is catched wait for all pools to finish their tasks and destroy them.

Here is the call graph for this function:

◆ thread_function()

static void* thread_function ( void *  arg)
static

Function run by every thread.

If there is a task to run pop it from the queue and run. If not, sleep on the semaphore. Repeat until the thread-pool is destroyed.

Here is the call graph for this function:

◆ thread_pool_destroy()

void thread_pool_destroy ( thread_pool_t pool)

Destroy the thread-pool.

Finish all of the tasks and remove the pool.

Parameters
[in,out]pool– pointer to the thread-pool;
Here is the call graph for this function:

◆ thread_pool_init()

int thread_pool_init ( thread_pool_t pool,
size_t  pool_size 
)

Initialize the thread-pool.

Create a thread_pool_t object at argument pool with number of thread passed as pool_size argument.

Parameters
[in,out]pool– pointer to the thread-pool;
[in]pool_size– size of the thread-pool;
Returns
0, if init was finished correctly. Non-zero value, if errors occurred.
Here is the call graph for this function: