tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

threadprogress.h (3300B)


      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 */
     43 typedef struct ThreadProgress {
     44    atomic_int progress;
     45    unsigned   init;
     46    AVMutex progress_mutex;
     47    AVCond  progress_cond;
     48 } ThreadProgress;
     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 */
     57 int 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 */
     65 void ff_thread_progress_destroy(ThreadProgress *pro);
     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 */
     72 static inline void ff_thread_progress_reset(ThreadProgress *pro)
     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 */
     82 void 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 */
     90 void ff_thread_progress_await(const ThreadProgress *pro, int progress);
     91 
     92 #endif /* AVCODEC_THREADPROGRESS_H */