summaryrefslogtreecommitdiff
path: root/libavcodec/pthread_internal.h
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-09-02 13:00:42 +0200
committerAndreas Rheinhardt <andreas.rheinhardt@outlook.com>2021-09-04 08:03:19 +0200
commita4f7fabc2684890ea9fbcfaefb7503ab6633479f (patch)
treeb07736e20e462152da7aaee8cb114f230d853c05 /libavcodec/pthread_internal.h
parentbd95f2f599a6605dafa73b95ca7590aa6a5e941d (diff)
avcodec/pthread_frame: Move (init|free)_pthread() to pthread.c
We have more mutexes/condition variables whose initialization is unchecked. Also use a proper namespace for these functions. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Diffstat (limited to 'libavcodec/pthread_internal.h')
-rw-r--r--libavcodec/pthread_internal.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/libavcodec/pthread_internal.h b/libavcodec/pthread_internal.h
index d2115cbbaf..d0b6a7a673 100644
--- a/libavcodec/pthread_internal.h
+++ b/libavcodec/pthread_internal.h
@@ -31,4 +31,36 @@ void ff_slice_thread_free(AVCodecContext *avctx);
int ff_frame_thread_init(AVCodecContext *avctx);
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count);
+#define THREAD_SENTINEL 0 // This forbids putting a mutex/condition variable at the front.
+/**
+ * Initialize/destroy a list of mutexes/conditions contained in a structure.
+ * The positions of these mutexes/conditions in the structure are given by
+ * their offsets. Because it is undefined behaviour to destroy
+ * an uninitialized mutex/condition, ff_pthread_init() stores the number
+ * of successfully initialized mutexes and conditions in the object itself
+ * and ff_pthread_free() uses this number to destroy exactly the mutexes and
+ * condition variables that have been successfully initialized.
+ *
+ * @param obj The object containing the mutexes/conditions.
+ * @param[in] offsets An array of offsets. Its first member gives the offset
+ * of the variable that contains the count of successfully
+ * initialized mutexes/condition variables; said variable
+ * must be an unsigned int. Two arrays of offsets, each
+ * delimited by a THREAD_SENTINEL follow. The first
+ * contains the offsets of all the mutexes, the second
+ * contains the offsets of all the condition variables.
+ */
+int ff_pthread_init(void *obj, const unsigned offsets[]);
+void ff_pthread_free(void *obj, const unsigned offsets[]);
+
+/**
+ * Macros to help creating the above lists. mutexes and conds need
+ * to be parentheses-enclosed lists of offsets in the containing structure.
+ */
+#define OFFSET_ARRAY(...) __VA_ARGS__, THREAD_SENTINEL
+#define DEFINE_OFFSET_ARRAY(type, name, cnt_variable, mutexes, conds) \
+static const unsigned name ## _offsets[] = { offsetof(type, cnt_variable), \
+ OFFSET_ARRAY mutexes, \
+ OFFSET_ARRAY conds }
+
#endif // AVCODEC_PTHREAD_INTERNAL_H