| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | #include <limits.h> | ||
| 22 | #include <stdatomic.h> | ||
| 23 | |||
| 24 | #include "pthread_internal.h" | ||
| 25 | #include "threadprogress.h" | ||
| 26 | #include "libavutil/attributes.h" | ||
| 27 | #include "libavutil/thread.h" | ||
| 28 | |||
| 29 | DEFINE_OFFSET_ARRAY(ThreadProgress, thread_progress, init, | ||
| 30 | (offsetof(ThreadProgress, progress_mutex)), | ||
| 31 | (offsetof(ThreadProgress, progress_cond))); | ||
| 32 | |||
| 33 | 3988 | av_cold int ff_thread_progress_init(ThreadProgress *pro, int init_mode) | |
| 34 | { | ||
| 35 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3964 times.
|
3988 | atomic_init(&pro->progress, init_mode ? -1 : INT_MAX); |
| 36 | #if HAVE_THREADS | ||
| 37 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3964 times.
|
3988 | if (init_mode) |
| 38 | 24 | return ff_pthread_init(pro, thread_progress_offsets); | |
| 39 | #endif | ||
| 40 | 3964 | pro->init = init_mode; | |
| 41 | 3964 | return 0; | |
| 42 | } | ||
| 43 | |||
| 44 | 3988 | av_cold void ff_thread_progress_destroy(ThreadProgress *pro) | |
| 45 | { | ||
| 46 | #if HAVE_THREADS | ||
| 47 | 3988 | ff_pthread_free(pro, thread_progress_offsets); | |
| 48 | #else | ||
| 49 | pro->init = 0; | ||
| 50 | #endif | ||
| 51 | 3988 | } | |
| 52 | |||
| 53 | 102742 | void ff_thread_progress_report(ThreadProgress *pro, int n) | |
| 54 | { | ||
| 55 |
2/2✓ Branch 0 taken 100954 times.
✓ Branch 1 taken 1788 times.
|
102742 | if (atomic_load_explicit(&pro->progress, memory_order_relaxed) >= n) |
| 56 | 100954 | return; | |
| 57 | |||
| 58 | 1788 | ff_mutex_lock(&pro->progress_mutex); | |
| 59 | 1788 | atomic_store_explicit(&pro->progress, n, memory_order_release); | |
| 60 | 1788 | ff_cond_broadcast(&pro->progress_cond); | |
| 61 | 1788 | ff_mutex_unlock(&pro->progress_mutex); | |
| 62 | } | ||
| 63 | |||
| 64 | 2676092 | void ff_thread_progress_await(const ThreadProgress *pro_c, int n) | |
| 65 | { | ||
| 66 | /* Casting const away here is safe, because we only read from progress | ||
| 67 | * and will leave pro_c in the same state upon leaving the function | ||
| 68 | * as it had at the beginning. */ | ||
| 69 | 2676092 | ThreadProgress *pro = (ThreadProgress*)pro_c; | |
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 2675983 times.
✓ Branch 1 taken 109 times.
|
2676092 | if (atomic_load_explicit(&pro->progress, memory_order_acquire) >= n) |
| 72 | 2675983 | return; | |
| 73 | |||
| 74 | 109 | ff_mutex_lock(&pro->progress_mutex); | |
| 75 |
2/2✓ Branch 0 taken 109 times.
✓ Branch 1 taken 109 times.
|
218 | while (atomic_load_explicit(&pro->progress, memory_order_relaxed) < n) |
| 76 | 109 | ff_cond_wait(&pro->progress_cond, &pro->progress_mutex); | |
| 77 | 109 | ff_mutex_unlock(&pro->progress_mutex); | |
| 78 | } | ||
| 79 |