FFmpeg
Loading...
Searching...
No Matches
threadprogress.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef AVCODEC_THREADPROGRESS_H
22#define AVCODEC_THREADPROGRESS_H
23
24/**
25 * ThreadProgress is an API to easily notify other threads about progress
26 * of any kind as long as it can be packaged into an int and is consistent
27 * with the natural ordering of integers.
28 *
29 * Each initialized ThreadProgress can be in one of two modes: No-op mode
30 * or ordinary mode. In the former mode, ff_thread_report_progress() and
31 * ff_thread_await_progress() are no-ops to simply support usecases like
32 * non-frame-threading. Only in the latter case perform these functions
33 * what their name already implies.
34 */
35
36#include <limits.h>
37#include <stdatomic.h>
38#include "libavutil/thread.h"
39
40/**
41 * This struct should be treated as opaque by users.
42 */
49
50/**
51 * Initialize a ThreadProgress.
52 *
53 * @param init_mode If zero, the ThreadProgress will be initialized
54 * to be in no-op mode as described above. Otherwise
55 * it is initialized to be in ordinary mode.
56 */
57int ff_thread_progress_init(ThreadProgress *pro, int init_mode);
58
59/**
60 * Destroy a ThreadProgress. Can be called on a ThreadProgress that
61 * has never been initialized provided that the ThreadProgress struct
62 * has been initially zeroed. Must be called even if ff_thread_progress_init()
63 * failed.
64 */
66
67/**
68 * Reset the ::ThreadProgress.progress counter; must only be called
69 * if the ThreadProgress is not in use in any way (e.g. no thread
70 * may wait on it via ff_thread_progress_await()).
71 */
73{
74 atomic_init(&pro->progress, pro->init ? -1 : INT_MAX);
75}
76
77/**
78 * This function is a no-op in no-op mode; otherwise it notifies
79 * other threads that a certain level of progress has been reached.
80 * Later calls with lower values of progress have no effect.
81 */
82void ff_thread_progress_report(ThreadProgress *pro, int progress);
83
84/**
85 * This function is a no-op in no-op mode; otherwise it waits
86 * until other threads have reached a certain level of progress:
87 * This function will return after another thread has called
88 * ff_thread_progress_report() with the same or higher value for progress.
89 */
90void ff_thread_progress_await(const ThreadProgress *pro, int progress);
91
92#endif /* AVCODEC_THREADPROGRESS_H */
intptr_t atomic_int
Definition stdatomic.h:55
#define atomic_init(obj, value)
Definition stdatomic.h:33
#define AVCond
Definition thread.h:192
#define AVMutex
Definition thread.h:184
ThreadProgress is an API to easily notify other threads about progress of any kind as long as it can ...
atomic_int progress
AVMutex progress_mutex
static void ff_thread_progress_reset(ThreadProgress *pro)
Reset the ThreadProgress.progress counter; must only be called if the ThreadProgress is not in use in...
void ff_thread_progress_destroy(ThreadProgress *pro)
Destroy a ThreadProgress.
int ff_thread_progress_init(ThreadProgress *pro, int init_mode)
Initialize a ThreadProgress.
void ff_thread_progress_report(ThreadProgress *pro, int progress)
This function is a no-op in no-op mode; otherwise it notifies other threads that a certain level of p...
void ff_thread_progress_await(const ThreadProgress *pro, int progress)
This function is a no-op in no-op mode; otherwise it waits until other threads have reached a certain...