Thread pool
Data Structures | Typedefs | Functions
future.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "future.h"
Include dependency graph for future.c:

Data Structures

struct  pair_future
 Pair of future variables. More...
 

Typedefs

typedef void *(* function_t) (void *)
 
typedef struct pair_future pair_future_t
 Pair of future variables.
 

Functions

static void fun (void *arg, size_t size __attribute__((unused)))
 Wrap callable function in runnable. More...
 
static void fun_with_wait (void *arg, size_t size __attribute__((unused)))
 Helper function for map. More...
 
int async (thread_pool_t *pool, future_t *future, callable_t callable)
 Create a future variable that will store the result of callable. More...
 
int map (thread_pool_t *pool, future_t *future, future_t *from, void *(*function)(void *, size_t, size_t *))
 Create a future variable that will store the result of callable. More...
 
void * await (future_t *future)
 Wait for future to finish. More...
 

Detailed Description

Future 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
11.02.2020

Function Documentation

◆ async()

int async ( thread_pool_t pool,
future_t future,
callable_t  callable 
)

Create a future variable that will store the result of callable.

Create a future variable. Create runnable function that will run callable function and add it to the thread-pool.

Parameters
[in,out]pool– pointer to the thread-pool;
[in,out]future– pointer to a variable that will store the callable result;
[in]callable– function that will be run by thread-pool;
Returns
0, if future was created correctly and callable function has been added to thread-pool. Non-zero value, if errors occurred.
Here is the call graph for this function:

◆ await()

void* await ( future_t future)

Wait for future to finish.

Sleep on semaphore until the future is calculated.

Parameters
[in,out]future– pointer to a variable that will store the callable result;

◆ fun()

static void fun ( void *  arg,
size_t size   __attribute__(unused) 
)
static

Wrap callable function in runnable.

Write the result of the function to variable. Unlock the semaphore to let user know that the task is finished.

Parameters
[in,out]arg– array of arguments;

◆ fun_with_wait()

static void fun_with_wait ( void *  arg,
size_t size   __attribute__(unused) 
)
static

Helper function for map.

Wait until from is finished, then run the function and write the result to future.

Parameters
[in,out]arg– array of arguments;

◆ map()

int map ( thread_pool_t pool,
future_t future,
future_t from,
void *(*)(void *, size_t, size_t *)  function 
)

Create a future variable that will store the result of callable.

Create runnable function that will run callable function and add a new task to the thread-pool.

Parameters
[in,out]pool– pointer to the thread-pool;
[in,out]future– pointer to the future variable that stores the arguments to the function;
[in,out]from– pointer to a variable that will store the callable result;
[in,out]function– function that will be run by thread-pool;
Returns
0, if future variables were mapped correctly. Non-zero value, if errors occurred.
Here is the call graph for this function: