diff options
author | Kacper Michajłow <kasper93@gmail.com> | 2023-10-21 04:55:41 +0200 |
---|---|---|
committer | Dudemanguy <random342@airmail.cc> | 2023-11-05 17:36:17 +0000 |
commit | 174df99ffa53f1091589eaa4fa0c16cdd55a9326 (patch) | |
tree | 3a60d45615f18beed98a9b08267c28ed7e05dd5f /common | |
parent | 3a8b107f6216b38a151d5ca1e9d4f2727e3418f5 (diff) | |
download | mpv-174df99ffa53f1091589eaa4fa0c16cdd55a9326.tar.bz2 mpv-174df99ffa53f1091589eaa4fa0c16cdd55a9326.tar.xz |
ALL: use new mp_thread abstraction
Diffstat (limited to 'common')
-rw-r--r-- | common/av_log.c | 20 | ||||
-rw-r--r-- | common/encode_lavc.c | 32 | ||||
-rw-r--r-- | common/encode_lavc.h | 4 | ||||
-rw-r--r-- | common/msg.c | 143 | ||||
-rw-r--r-- | common/stats.c | 64 | ||||
-rw-r--r-- | common/stats.h | 2 |
6 files changed, 125 insertions, 140 deletions
diff --git a/common/av_log.c b/common/av_log.c index 15d17aff07..b6bad041e1 100644 --- a/common/av_log.c +++ b/common/av_log.c @@ -22,13 +22,13 @@ #include <stdlib.h> #include <stdio.h> #include <stdbool.h> -#include <pthread.h> #include "av_log.h" -#include "config.h" #include "common/common.h" #include "common/global.h" #include "common/msg.h" +#include "config.h" +#include "osdep/threads.h" #include <libavutil/avutil.h> #include <libavutil/log.h> @@ -51,7 +51,7 @@ // Needed because the av_log callback does not provide a library-safe message // callback. -static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER; +static mp_static_mutex log_lock = MP_STATIC_MUTEX_INITIALIZER; static struct mpv_global *log_mpv_instance; static struct mp_log *log_root, *log_decaudio, *log_decvideo, *log_demuxer; static bool log_print_prefix = true; @@ -113,10 +113,10 @@ static void mp_msg_av_log_callback(void *ptr, int level, const char *fmt, int mp_level = av_log_level_to_mp_level(level); // Note: mp_log is thread-safe, but destruction of the log instances is not. - pthread_mutex_lock(&log_lock); + mp_mutex_lock(&log_lock); if (!log_mpv_instance) { - pthread_mutex_unlock(&log_lock); + mp_mutex_unlock(&log_lock); // Fallback to stderr vfprintf(stderr, fmt, vl); return; @@ -138,12 +138,12 @@ static void mp_msg_av_log_callback(void *ptr, int level, const char *fmt, mp_msg(log, mp_level, "%s", buffer); } - pthread_mutex_unlock(&log_lock); + mp_mutex_unlock(&log_lock); } void init_libav(struct mpv_global *global) { - pthread_mutex_lock(&log_lock); + mp_mutex_lock(&log_lock); if (!log_mpv_instance) { log_mpv_instance = global; log_root = mp_log_new(NULL, global->log, "ffmpeg"); @@ -152,7 +152,7 @@ void init_libav(struct mpv_global *global) log_demuxer = mp_log_new(log_root, log_root, "demuxer"); av_log_set_callback(mp_msg_av_log_callback); } - pthread_mutex_unlock(&log_lock); + mp_mutex_unlock(&log_lock); avformat_network_init(); @@ -163,13 +163,13 @@ void init_libav(struct mpv_global *global) void uninit_libav(struct mpv_global *global) { - pthread_mutex_lock(&log_lock); + mp_mutex_lock(&log_lock); if (log_mpv_instance == global) { av_log_set_callback(av_log_default_callback); log_mpv_instance = NULL; talloc_free(log_root); } - pthread_mutex_unlock(&log_lock); + mp_mutex_unlock(&log_lock); } #define V(x) AV_VERSION_MAJOR(x), \ diff --git a/common/encode_lavc.c b/common/encode_lavc.c index 47e9883e0c..898545de50 100644 --- a/common/encode_lavc.c +++ b/common/encode_lavc.c @@ -104,7 +104,7 @@ struct encode_lavc_context *encode_lavc_init(struct mpv_global *global) .priv = talloc_zero(ctx, struct encode_priv), .log = mp_log_new(ctx, global->log, "encode"), }; - pthread_mutex_init(&ctx->lock, NULL); + mp_mutex_init(&ctx->lock); struct encode_priv *p = ctx->priv; p->log = ctx->log; @@ -157,7 +157,7 @@ void encode_lavc_set_metadata(struct encode_lavc_context *ctx, { struct encode_priv *p = ctx->priv; - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); if (ctx->options->copy_metadata) { p->metadata = mp_tags_dup(ctx, metadata); @@ -184,7 +184,7 @@ void encode_lavc_set_metadata(struct encode_lavc_context *ctx, } } - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); } bool encode_lavc_free(struct encode_lavc_context *ctx) @@ -220,7 +220,7 @@ bool encode_lavc_free(struct encode_lavc_context *ctx) res = !p->failed; - pthread_mutex_destroy(&ctx->lock); + mp_mutex_destroy(&ctx->lock); talloc_free(ctx); return res; @@ -313,7 +313,7 @@ void encode_lavc_expect_stream(struct encode_lavc_context *ctx, { struct encode_priv *p = ctx->priv; - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); enum AVMediaType codec_type = mp_to_av_stream_type(type); @@ -337,7 +337,7 @@ void encode_lavc_expect_stream(struct encode_lavc_context *ctx, MP_TARRAY_APPEND(p, p->streams, p->num_streams, dst); done: - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); } // Signal that you are ready to encode (you provide the codec params etc. too). @@ -351,7 +351,7 @@ static void encode_lavc_add_stream(struct encoder_context *enc, { struct encode_priv *p = ctx->priv; - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); struct mux_stream *dst = find_mux_stream(ctx, info->codecpar->codec_type); if (!dst) { @@ -387,7 +387,7 @@ static void encode_lavc_add_stream(struct encoder_context *enc, maybe_init_muxer(ctx); done: - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); } // Write a packet. This will take over ownership of `pkt` @@ -398,7 +398,7 @@ static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt) assert(dst->st); - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); if (p->failed) goto done; @@ -435,7 +435,7 @@ static void encode_lavc_add_packet(struct mux_stream *dst, AVPacket *pkt) pkt = NULL; done: - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); if (pkt) av_packet_unref(pkt); } @@ -450,9 +450,9 @@ void encode_lavc_discontinuity(struct encode_lavc_context *ctx) if (!ctx) return; - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); ctx->discontinuity_pts_offset = MP_NOPTS_VALUE; - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); } static void encode_lavc_printoptions(struct mp_log *log, const void *obj, @@ -673,7 +673,7 @@ int encode_lavc_getstatus(struct encode_lavc_context *ctx, float minutes, megabytes, fps, x; float f = MPMAX(0.0001, relative_position); - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); if (p->failed) { snprintf(buf, bufsize, "(failed)\n"); @@ -697,7 +697,7 @@ int encode_lavc_getstatus(struct encode_lavc_context *ctx, buf[bufsize - 1] = 0; done: - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); return 0; } @@ -705,9 +705,9 @@ bool encode_lavc_didfail(struct encode_lavc_context *ctx) { if (!ctx) return false; - pthread_mutex_lock(&ctx->lock); + mp_mutex_lock(&ctx->lock); bool fail = ctx->priv->failed; - pthread_mutex_unlock(&ctx->lock); + mp_mutex_unlock(&ctx->lock); return fail; } diff --git a/common/encode_lavc.h b/common/encode_lavc.h index ae89c6b572..8517726529 100644 --- a/common/encode_lavc.h +++ b/common/encode_lavc.h @@ -22,7 +22,6 @@ #ifndef MPLAYER_ENCODE_LAVC_H #define MPLAYER_ENCODE_LAVC_H -#include <pthread.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> @@ -33,6 +32,7 @@ #include "common/common.h" #include "encode.h" +#include "osdep/threads.h" #include "video/csputils.h" struct encode_lavc_context { @@ -47,7 +47,7 @@ struct encode_lavc_context { // All entry points must be guarded with the lock. Functions called by // the playback core lock this automatically, but ao_lavc.c and vo_lavc.c // must lock manually before accessing state. - pthread_mutex_t lock; + mp_mutex lock; // anti discontinuity mode double next_in_pts; diff --git a/common/msg.c b/common/msg.c index 66a7a6f1ff..dc27562182 100644 --- a/common/msg.c +++ b/common/msg.c @@ -16,7 +16,6 @@ */ #include <assert.h> -#include <pthread.h> #include <stdarg.h> #include <stdatomic.h> #include <stdint.h> @@ -55,9 +54,9 @@ struct mp_log_root { struct mpv_global *global; - pthread_mutex_t lock; - pthread_mutex_t log_file_lock; - pthread_cond_t log_file_wakeup; + mp_mutex lock; + mp_mutex log_file_lock; + mp_cond log_file_wakeup; // --- protected by lock char **msg_levels; bool use_terminal; // make accesses to stderr/stdout @@ -83,7 +82,7 @@ struct mp_log_root { // --- owner thread only (caller of mp_msg_init() etc.) char *log_path; char *stats_path; - pthread_t log_file_thread; + mp_thread log_file_thread; // --- owner thread only, but frozen while log_file_thread is running FILE *log_file; struct mp_log_buffer *log_file_buffer; @@ -104,7 +103,7 @@ struct mp_log { struct mp_log_buffer { struct mp_log_root *root; - pthread_mutex_t lock; + mp_mutex lock; // --- protected by lock struct mp_log_buffer_entry **entries; // ringbuffer int capacity; // total space in entries[] @@ -133,7 +132,7 @@ static bool match_mod(const char *name, const char *mod) static void update_loglevel(struct mp_log *log) { struct mp_log_root *root = log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); log->level = MSGL_STATUS + root->verbose; // default log level if (root->really_quiet) log->level = -1; @@ -155,7 +154,7 @@ static void update_loglevel(struct mp_log *log) log->level = MPMAX(log->level, MSGL_STATS); log->level = MPMIN(log->level, log->max_level); atomic_store(&log->reload_counter, atomic_load(&log->root->reload_counter)); - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); } // Set (numerically) the maximum level that should still be output for this log @@ -164,9 +163,9 @@ void mp_msg_set_max_level(struct mp_log *log, int lev) { if (!log->root) return; - pthread_mutex_lock(&log->root->lock); + mp_mutex_lock(&log->root->lock); log->max_level = MPCLAMP(lev, -1, MSGL_MAX); - pthread_mutex_unlock(&log->root->lock); + mp_mutex_unlock(&log->root->lock); update_loglevel(log); } @@ -233,9 +232,9 @@ static void flush_status_line(struct mp_log_root *root) void mp_msg_flush_status_line(struct mp_log *log) { if (log->root) { - pthread_mutex_lock(&log->root->lock); + mp_mutex_lock(&log->root->lock); flush_status_line(log->root); - pthread_mutex_unlock(&log->root->lock); + mp_mutex_unlock(&log->root->lock); } } @@ -243,18 +242,18 @@ void mp_msg_set_term_title(struct mp_log *log, const char *title) { if (log->root && title) { // Lock because printf to terminal is not necessarily atomic. - pthread_mutex_lock(&log->root->lock); + mp_mutex_lock(&log->root->lock); fprintf(stderr, "\e]0;%s\007", title); - pthread_mutex_unlock(&log->root->lock); + mp_mutex_unlock(&log->root->lock); } } bool mp_msg_has_status_line(struct mpv_global *global) { struct mp_log_root *root = global->log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); bool r = root->status_lines > 0; - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); return r; } @@ -352,7 +351,7 @@ static void write_msg_to_buffers(struct mp_log *log, int lev, char *text) for (int n = 0; n < root->num_buffers; n++) { struct mp_log_buffer *buffer = root->buffers[n]; bool wakeup = false; - pthread_mutex_lock(&buffer->lock); + mp_mutex_lock(&buffer->lock); int buffer_level = buffer->level; if (buffer_level == MP_LOG_BUFFER_MSGL_TERM) buffer_level = log->terminal_level; @@ -366,16 +365,16 @@ static void write_msg_to_buffers(struct mp_log *log, int lev, char *text) while (buffer->num_entries == buffer->capacity && !dead) { // Temporary unlock is OK; buffer->level is immutable, and // buffer can't go away because the global log lock is held. - pthread_mutex_unlock(&buffer->lock); - pthread_mutex_lock(&root->log_file_lock); + mp_mutex_unlock(&buffer->lock); + mp_mutex_lock(&root->log_file_lock); if (root->log_file_thread_active) { - pthread_cond_wait(&root->log_file_wakeup, + mp_cond_wait(&root->log_file_wakeup, &root->log_file_lock); } else { dead = true; } - pthread_mutex_unlock(&root->log_file_lock); - pthread_mutex_lock(&buffer->lock); + mp_mutex_unlock(&root->log_file_lock); + mp_mutex_lock(&buffer->lock); } } if (buffer->num_entries == buffer->capacity) { @@ -395,7 +394,7 @@ static void write_msg_to_buffers(struct mp_log *log, int lev, char *text) if (buffer->wakeup_cb && !buffer->silent) wakeup = true; } - pthread_mutex_unlock(&buffer->lock); + mp_mutex_unlock(&buffer->lock); if (wakeup) buffer->wakeup_cb(buffer->wakeup_cb_ctx); } @@ -415,7 +414,7 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) struct mp_log_root *root = log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); root->buffer.len = 0; @@ -461,7 +460,7 @@ void mp_msg_va(struct mp_log *log, int lev, const char *format, va_list va) } } - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); } static void destroy_log(void *ptr) @@ -527,9 +526,9 @@ void mp_msg_init(struct mpv_global *global) .reload_counter = 1, }; - pthread_mutex_init(&root->lock, NULL); - pthread_mutex_init(&root->log_file_lock, NULL); - pthread_cond_init(&root->log_file_wakeup, NULL); + mp_mutex_init(&root->lock); + mp_mutex_init(&root->log_file_lock); + mp_cond_init(&root->log_file_wakeup); struct mp_log dummy = { .root = root }; struct mp_log *log = mp_log_new(root, &dummy, ""); @@ -537,44 +536,44 @@ void mp_msg_init(struct mpv_global *global) global->log = log; } -static void *log_file_thread(void *p) +static MP_THREAD_VOID log_file_thread(void *p) { struct mp_log_root *root = p; - mpthread_set_name("log"); + mp_thread_set_name("log"); - pthread_mutex_lock(&root->log_file_lock); + mp_mutex_lock(&root->log_file_lock); while (root->log_file_thread_active) { struct mp_log_buffer_entry *e = mp_msg_log_buffer_read(root->log_file_buffer); if (e) { - pthread_mutex_unlock(&root->log_file_lock); + mp_mutex_unlock(&root->log_file_lock); fprintf(root->log_file, "[%8.3f][%c][%s] %s", mp_time_sec(), mp_log_levels[e->level][0], e->prefix, e->text); fflush(root->log_file); - pthread_mutex_lock(&root->log_file_lock); + mp_mutex_lock(&root->log_file_lock); talloc_free(e); // Multiple threads might be blocked if the log buffer was full. - pthread_cond_broadcast(&root->log_file_wakeup); + mp_cond_broadcast(&root->log_file_wakeup); } else { - pthread_cond_wait(&root->log_file_wakeup, &root->log_file_lock); + mp_cond_wait(&root->log_file_wakeup, &root->log_file_lock); } } - pthread_mutex_unlock(&root->log_file_lock); + mp_mutex_unlock(&root->log_file_lock); - return NULL; + MP_THREAD_RETURN(); } static void wakeup_log_file(void *p) { struct mp_log_root *root = p; - pthread_mutex_lock(&root->log_file_lock); - pthread_cond_broadcast(&root->log_file_wakeup); - pthread_mutex_unlock(&root->log_file_lock); + mp_mutex_lock(&root->log_file_lock); + mp_cond_broadcast(&root->log_file_wakeup); + mp_mutex_unlock(&root->log_file_lock); } // Only to be called from the main thread. @@ -582,16 +581,16 @@ static void terminate_log_file_thread(struct mp_log_root *root) { bool wait_terminate = false; - pthread_mutex_lock(&root->log_file_lock); + mp_mutex_lock(&root->log_file_lock); if (root->log_file_thread_active) { root->log_file_thread_active = false; - pthread_cond_broadcast(&root->log_file_wakeup); + mp_cond_broadcast(&root->log_file_wakeup); wait_terminate = true; } - pthread_mutex_unlock(&root->log_file_lock); + mp_mutex_unlock(&root->log_file_lock); if (wait_terminate) - pthread_join(root->log_file_thread, NULL); + mp_thread_join(root->log_file_thread); mp_msg_log_buffer_destroy(root->log_file_buffer); root->log_file_buffer = NULL; @@ -631,7 +630,7 @@ void mp_msg_update_msglevels(struct mpv_global *global, struct MPOpts *opts) { struct mp_log_root *root = global->log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); root->verbose = opts->verbose; root->really_quiet = opts->msg_really_quiet; @@ -645,7 +644,7 @@ void mp_msg_update_msglevels(struct mpv_global *global, struct MPOpts *opts) m_option_type_msglevels.copy(NULL, &root->msg_levels, &opts->msg_levels); atomic_fetch_add(&root->reload_counter, 1); - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); if (check_new_path(global, opts->log_file, &root->log_path)) { terminate_log_file_thread(root); @@ -655,11 +654,11 @@ void mp_msg_update_msglevels(struct mpv_global *global, struct MPOpts *opts) // if a logfile is created and the early filebuf still exists, // flush and destroy the early buffer - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); struct mp_log_buffer *earlybuf = root->early_filebuffer; if (earlybuf) root->early_filebuffer = NULL; // but it still logs msgs - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); if (earlybuf) { // flush, destroy before creating the normal logfile buf, @@ -681,7 +680,7 @@ void mp_msg_update_msglevels(struct mpv_global *global, struct MPOpts *opts) mp_msg_log_buffer_new(global, FILE_BUF, MP_LOG_BUFFER_MSGL_LOGFILE, wakeup_log_file, root); root->log_file_thread_active = true; - if (pthread_create(&root->log_file_thread, NULL, log_file_thread, + if (mp_thread_create(&root->log_file_thread, log_file_thread, root)) { root->log_file_thread_active = false; @@ -697,7 +696,7 @@ void mp_msg_update_msglevels(struct mpv_global *global, struct MPOpts *opts) if (check_new_path(global, opts->dump_stats, &root->stats_path)) { bool open_error = false; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); if (root->stats_file) fclose(root->stats_file); root->stats_file = NULL; @@ -705,7 +704,7 @@ void mp_msg_update_msglevels(struct mpv_global *global, struct MPOpts *opts) root->stats_file = fopen(root->stats_path, "wb"); open_error = !root->stats_file; } - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); if (open_error) { mp_err(global->log, "Failed to open stats file '%s'\n", @@ -718,9 +717,9 @@ void mp_msg_force_stderr(struct mpv_global *global, bool force_stderr) { struct mp_log_root *root = global->log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); root->force_stderr = force_stderr; - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); } // Only to be called from the main thread. @@ -743,9 +742,9 @@ void mp_msg_uninit(struct mpv_global *global) talloc_free(root->stats_path); talloc_free(root->log_path); m_option_type_msglevels.free(&root->msg_levels); - pthread_mutex_destroy(&root->lock); - pthread_mutex_destroy(&root->log_file_lock); - pthread_cond_destroy(&root->log_file_wakeup); + mp_mutex_destroy(&root->lock); + mp_mutex_destroy(&root->log_file_lock); + mp_cond_destroy(&root->log_file_wakeup); talloc_free(root); global->log = NULL; } @@ -773,26 +772,26 @@ static void mp_msg_set_early_logging_raw(struct mpv_global *global, bool enable, int size, int level) { struct mp_log_root *root = global->log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); if (enable != !!*root_logbuf) { if (enable) { - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); struct mp_log_buffer *buf = mp_msg_log_buffer_new(global, size, level, NULL, NULL); - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); assert(!*root_logbuf); // no concurrent calls to this function *root_logbuf = buf; } else { struct mp_log_buffer *buf = *root_logbuf; *root_logbuf = NULL; - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); mp_msg_log_buffer_destroy(buf); return; } } - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); } void mp_msg_set_early_logging(struct mpv_global *global, bool enable) @@ -814,7 +813,7 @@ struct mp_log_buffer *mp_msg_log_buffer_new(struct mpv_global *global, { struct mp_log_root *root = global->log->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); if (level == MP_LOG_BUFFER_MSGL_TERM) { size = TERM_BUF; @@ -828,7 +827,7 @@ struct mp_log_buffer *mp_msg_log_buffer_new(struct mpv_global *global, root->early_buffer = NULL; buffer->wakeup_cb = wakeup_cb; buffer->wakeup_cb_ctx = wakeup_cb_ctx; - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); return buffer; } } @@ -845,21 +844,21 @@ struct mp_log_buffer *mp_msg_log_buffer_new(struct mpv_global *global, .wakeup_cb_ctx = wakeup_cb_ctx, }; - pthread_mutex_init(&buffer->lock, NULL); + mp_mutex_init(&buffer->lock); MP_TARRAY_APPEND(root, root->buffers, root->num_buffers, buffer); atomic_fetch_add(&root->reload_counter, 1); - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); return buffer; } void mp_msg_log_buffer_set_silent(struct mp_log_buffer *buffer, bool silent) { - pthread_mutex_lock(&buffer->lock); + mp_mutex_lock(&buffer->lock); buffer->silent = silent; - pthread_mutex_unlock(&buffer->lock); + mp_mutex_unlock(&buffer->lock); } void mp_msg_log_buffer_destroy(struct mp_log_buffer *buffer) @@ -869,7 +868,7 @@ void mp_msg_log_buffer_destroy(struct mp_log_buffer *buffer) struct mp_log_root *root = buffer->root; - pthread_mutex_lock(&root->lock); + mp_mutex_lock(&root->lock); for (int n = 0; n < root->num_buffers; n++) { if (root->buffers[n] == buffer) { @@ -885,11 +884,11 @@ found: while (buffer->num_entries) talloc_free(log_buffer_read(buffer)); - pthread_mutex_destroy(&buffer->lock); + mp_mutex_destroy(&buffer->lock); talloc_free(buffer); atomic_fetch_add(&root->reload_counter, 1); - pthread_mutex_unlock(&root->lock); + mp_mutex_unlock(&root->lock); } // Return a queued message, or if the buffer is empty, NULL. @@ -898,7 +897,7 @@ struct mp_log_buffer_entry *mp_msg_log_buffer_read(struct mp_log_buffer *buffer) { struct mp_log_buffer_entry *res = NULL; - pthread_mutex_lock(&buffer->lock); + mp_mutex_lock(&buffer->lock); if (!buffer->silent && buffer->num_entries) { if (buffer->dropped) { @@ -916,7 +915,7 @@ struct mp_log_buffer_entry *mp_msg_log_buffer_read(struct mp_log_buffer *buffer) } } - pthread_mutex_unlock(&buffer->lock); + mp_mutex_unlock(&buffer->lock); return res; } diff --git a/common/stats.c b/common/stats.c index 50984c2719..cb32ba48e9 100644 --- a/common/stats.c +++ b/common/stats.c @@ -9,6 +9,7 @@ #include "misc/node.h" #include "msg.h" #include "options/m_option.h" +#include "osdep/threads.h" #include "osdep/timer.h" #include "stats.h" @@ -17,7 +18,7 @@ struct stats_base { atomic_bool active; - pthread_mutex_t lock; + mp_mutex lock; struct { struct stats_ctx *head, *tail; @@ -60,27 +61,12 @@ struct stat_entry { int64_t val_th; int64_t time_start_ns; int64_t cpu_start_ns; - pthread_t thread; + mp_thread thread; }; #define IS_ACTIVE(ctx) \ (atomic_load_explicit(&(ctx)->base->active, memory_order_relaxed)) -// Overflows only after I'm dead. -static int64_t get_thread_cpu_time_ns(pthread_t thread) -{ -#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(_POSIX_THREAD_CPUTIME) - clockid_t id; - struct timespec tv; - if (pthread_getcpuclockid(thread, &id) == 0 && - clock_gettime(id, &tv) == 0) - { - return tv.tv_sec * (1000LL * 1000LL * 1000LL) + tv.tv_nsec; - } -#endif - return 0; -} - static void stats_destroy(void *p) { struct stats_base *stats = p; @@ -88,7 +74,7 @@ static void stats_destroy(void *p) // All entries must have been destroyed before this. assert(!stats->list.head); - pthread_mutex_destroy(&stats->lock); + mp_mutex_destroy(&stats->lock); } void stats_global_init(struct mpv_global *global) @@ -96,7 +82,7 @@ void stats_global_init(struct mpv_global *global) assert(!global->stats); struct stats_base *stats = talloc_zero(global, struct stats_base); ta_set_destructor(stats, stats_destroy); - pthread_mutex_init(&stats->lock, NULL); + mp_mutex_init(&stats->lock); global->stats = stats; stats->global = global; @@ -126,7 +112,7 @@ void stats_global_query(struct mpv_global *global, struct mpv_node *out) struct stats_base *stats = global->stats; assert(stats); - pthread_mutex_lock(&stats->lock); + mp_mutex_lock(&stats->lock); atomic_store(&stats->active, true); @@ -195,7 +181,7 @@ void stats_global_query(struct mpv_global *global, struct mpv_node *out) break; } case VAL_THREAD_CPU_TIME: { - int64_t t = get_thread_cpu_time_ns(e->thread); + int64_t t = mp_thread_cpu_time_ns(e->thread); if (!e->cpu_start_ns) e->cpu_start_ns = t; double t_msec = MP_TIME_NS_TO_MS(t - e->cpu_start_ns); @@ -207,17 +193,17 @@ void stats_global_query(struct mpv_global *global, struct mpv_node *out) } } - pthread_mutex_unlock(&stats->lock); + mp_mutex_unlock(&stats->lock); } static void stats_ctx_destroy(void *p) { struct stats_ctx *ctx = p; - pthread_mutex_lock(&ctx->base->lock); + mp_mutex_lock(&ctx->base->lock); LL_REMOVE(list, &ctx->base->list, ctx); ctx->base->num_entries = 0; // invalidate - pthread_mutex_unlock(&ctx->base->lock); + mp_mutex_unlock(&ctx->base->lock); } struct stats_ctx *stats_ctx_create(void *ta_parent, struct mpv_global *global, @@ -231,10 +217,10 @@ struct stats_ctx *stats_ctx_create(void *ta_parent, struct mpv_global *global, ctx->prefix = talloc_strdup(ctx, prefix); ta_set_destructor(ctx, stats_ctx_destroy); - pthread_mutex_lock(&base->lock); + mp_mutex_lock(&base->lock); LL_APPEND(list, &base->list, ctx); base->num_entries = 0; // invalidate - pthread_mutex_unlock(&base->lock); + mp_mutex_unlock(&base->lock); return ctx; } @@ -263,11 +249,11 @@ static void static_value(struct stats_ctx *ctx, const char *name, double val, { if (!IS_ACTIVE(ctx)) return; - pthread_mutex_lock(&ctx->base->lock); + mp_mutex_lock(&ctx->base->lock); struct stat_entry *e = find_entry(ctx, name); e->val_d = val; e->type = type; - pthread_mutex_unlock(&ctx->base->lock); + mp_mutex_unlock(&ctx->base->lock); } void stats_value(struct stats_ctx *ctx, const char *name, double val) @@ -285,11 +271,11 @@ void stats_time_start(struct stats_ctx *ctx, const char *name) MP_STATS(ctx->base->global, "start %s", name); if (!IS_ACTIVE(ctx)) return; - pthread_mutex_lock(&ctx->base->lock); + mp_mutex_lock(&ctx->base->lock); struct stat_entry *e = find_entry(ctx, name); - e->cpu_start_ns = get_thread_cpu_time_ns(pthread_self()); + e->cpu_start_ns = mp_thread_cpu_time_ns(mp_thread_self()); e->time_start_ns = mp_time_ns(); - pthread_mutex_unlock(&ctx->base->lock); + mp_mutex_unlock(&ctx->base->lock); } void stats_time_end(struct stats_ctx *ctx, const char *name) @@ -297,36 +283,36 @@ void stats_time_end(struct stats_ctx *ctx, const char *name) MP_STATS(ctx->base->global, "end %s", name); if (!IS_ACTIVE(ctx)) return; - pthread_mutex_lock(&ctx->base->lock); + mp_mutex_lock(&ctx->base->lock); struct stat_entry *e = find_entry(ctx, name); if (e->time_start_ns) { e->type = VAL_TIME; e->val_rt += mp_time_ns() - e->time_start_ns; - e->val_th += get_thread_cpu_time_ns(pthread_self()) - e->cpu_start_ns; + e->val_th += mp_thread_cpu_time_ns(mp_thread_self()) - e->cpu_start_ns; e->time_start_ns = 0; } - pthread_mutex_unlock(&ctx->base->lock); + mp_mutex_unlock(&ctx->base->lock); } void stats_event(struct stats_ctx *ctx, const char *name) { if (!IS_ACTIVE(ctx)) return; - pthread_mutex_lock(&ctx->base->lock); + mp_mutex_lock(&ctx->base->lock); struct stat_entry *e = find_entry(ctx, name); e->val_d += 1; e->type = VAL_INC; - pthread_mutex_unlock(&ctx->base->lock); + mp_mutex_unlock(&ctx->base->lock); } static void register_thread(struct stats_ctx *ctx, const char *name, enum val_type type) { - pthread_mutex_lock(&ctx->base->lock); + mp_mutex_lock(&ctx->base->lock); struct stat_entry *e = find_entry(ctx, name); e->type = type; - e->thread = pthread_self(); - pthread_mutex_unlock(&ctx->base->lock); + e->thread = mp_thread_self(); + mp_mutex_unlock(&ctx->base->lock); } void stats_register_thread_cputime(struct stats_ctx *ctx, const char *name) diff --git a/common/stats.h b/common/stats.h index caa002a407..03b16769f0 100644 --- a/common/stats.h +++ b/common/stats.h @@ -30,5 +30,5 @@ void stats_event(struct stats_ctx *ctx, const char *name); // or stats_unregister_thread() is called, otherwise UB will occur. void stats_register_thread_cputime(struct stats_ctx *ctx, const char *name); -// Remove reference to pthread_self(). +// Remove reference to mp_thread_self(). void stats_unregister_thread(struct stats_ctx *ctx, const char *name); |