GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_threads.h
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz>
4 */
5
6 /*
7
8 A code to ease multithreaded filters and more.
9
10 */
11
12#ifndef CORE_GP_THREADS_H
13#define CORE_GP_THREADS_H
14
15#include <pthread.h>
16
18#include <core/gp_types.h>
19
20/*
21 * Sets default number of threads the library uses
22 * (changes the behavior of gp_nr_threads()).
23 *
24 * 0 == auto
25 * Most of the time, if the image is not too small, this makes
26 * the filter run number of processors (as seen by the operating system)
27 * threads.
28 *
29 * 1 == one thread
30 * Everything runs in exactly one thread. This is default value.
31 *
32 * >= 2
33 * Runs exactly n threads unless the image is too small.
34 *
35 * This value may also be overriden by the GP_THREADS enviroment variable.
36 *
37 * Moreover the value may be changed for a single call by settings in progres
38 * callback structure.
39 */
40void gp_nr_threads_set(unsigned int nr);
41
42/*
43 * Returns a number of threads to use.
44 */
45unsigned int gp_nr_threads(gp_size w, gp_size h, gp_progress_cb *callback);
46
47/*
48 * Multithreaded progress callback priv data guarded by a mutex.
49 */
50struct gp_progress_cb_mp_priv {
51 float max;
52 int abort;
53 pthread_mutex_t mutex;
54 gp_progress_cb *orig_callback;
55};
56
57/*
58 * Creates and initalizes a on-the-stack progress callback
59 *
60 * The intended usage is:
61 *
62 * GP_PROGRESS_CALLBACK_MP(callback_mp, orig_callback);
63 *
64 * ...
65 *
66 * for n threads:
67 * run_filter(..., callback ? &callback_mp : NULL);
68 */
69#define GP_PROGRESS_CALLBACK_MP(name, callback) \
70 struct gp_progress_cb_mp_priv name_priv = { \
71 .max = 0, \
72 .abort = 0, \
73 .mutex = PTHREAD_MUTEX_INITIALIZER, \
74 .orig_callback = callback, \
75 }; \
76 GP_PROGRESS_CALLBACK(name, gp_progress_cb_mp, &name_priv);
77
78/*
79 * Multithreaded callback function itself.
80 */
81int gp_progress_cb_mp(gp_progress_cb *self);
82
83#endif /* CORE_GP_THREADS_H */
A common types.
unsigned int gp_size
Integer type for sizes i.e. w, h, ...
Definition gp_types.h:24
A progress callback.
Progress callback.