summaryrefslogtreecommitdiffstats
path: root/options
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2019-11-29 12:49:15 +0100
committerwm4 <wm4@nowhere>2019-11-29 12:49:15 +0100
commitb16cea750f527088be79772e7cd601f86ce62ef2 (patch)
tree7bca2775a35f501baacafdbab63060d95ec99469 /options
parent5e2658c98196f8fd3558a2ffe40bd789ed27e8a3 (diff)
downloadmpv-b16cea750f527088be79772e7cd601f86ce62ef2.tar.bz2
mpv-b16cea750f527088be79772e7cd601f86ce62ef2.tar.xz
player: change m_config to use new option handling mechanisms
Instead of making m_config a special-case, it more or less uses the underlying m_config_cache/m_config_shadow APIs properly. This makes the player core a (relatively) equivalent user of the core option API. In particular, this means that other threads can change core options with m_config_cache_write_opt() calls (before this commit, this merely led to diverging option values). An important change is that before this commit, mpctx->opts contained the "master copy" of all option data. Now it's just another copy of the option data, and the shadow copy is considered the master. This is why whenever mpctx->opts is written, the change needs to be copied to the master (thus why this commits add a bunch of m_config_notify... calls). If another thread (e.g. a VO) changes an option, async_change_cb is now invoked, which funnels the change notification through the player's layers. The new self_notification parameter on mp_option_change_callback is so that m_config_notify... doesn't trigger recursion, and it's used in cases where the change was already "processed". It's still needed to trigger libmpv property updates. (I considered using an extra m_config_cache for that, but it'd only cause problems with no advantages.) I think the recent changes actually forgot to send libmpv property updates in some cases. This should fix this anyway. In some cases, property updates are reworked, and the potential for bugs should be lower (probably). The primary point of this change is to allow external updates, for example by a VO writing the fullscreen option if the window state is changed by the window manager (rather than mpv changing it). This is not used yet, but the following commits will.
Diffstat (limited to 'options')
-rw-r--r--options/m_config.c144
-rw-r--r--options/m_config.h18
2 files changed, 95 insertions, 67 deletions
diff --git a/options/m_config.c b/options/m_config.c
index 52a9b2f6d8..a28ae4438b 100644
--- a/options/m_config.c
+++ b/options/m_config.c
@@ -142,6 +142,9 @@ struct m_opt_backup {
void *backup;
};
+static struct m_config_cache *m_config_cache_alloc_internal(void *ta_parent,
+ struct m_config_shadow *shadow,
+ const struct m_sub_options *group);
static void add_sub_group(struct m_config_shadow *shadow, const char *name_prefix,
int parent_group_index, int parent_ptr,
const struct m_sub_options *subopts);
@@ -446,7 +449,7 @@ static void config_destroy(void *p)
struct m_config *config = p;
m_config_restore_backups(config);
- talloc_free(config->data);
+ talloc_free(config->cache);
talloc_free(config->shadow);
}
@@ -460,9 +463,9 @@ struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log,
config->shadow = m_config_shadow_new(root);
if (root->size) {
- config->data = allocate_option_data(config, config->shadow, 0,
- config->shadow->data);
- config->optstruct = config->data->gdata[0].udata;
+ config->cache =
+ m_config_cache_alloc_internal(config, config->shadow, root);
+ config->optstruct = config->cache->opts;
}
struct opt_iterate_state it;
@@ -475,8 +478,9 @@ struct m_config *m_config_new(void *talloc_ctx, struct mp_log *log,
.is_hidden = !!it.opt->deprecation_message,
};
- struct m_group_data *gdata =
- config->data ? m_config_gdata(config->data, it.group_index) : NULL;
+ struct m_group_data *gdata = config->cache
+ ? m_config_gdata(config->cache->internal->data, it.group_index)
+ : NULL;
if (gdata && co.opt->offset >= 0)
co.data = gdata->udata + co.opt->offset;
@@ -939,6 +943,67 @@ static int m_config_handle_special_options(struct m_config *config,
return M_OPT_UNKNOWN;
}
+// This notification happens when anyone other than m_config->cache (i.e. not
+// through m_config_set_option_raw() or related) changes any options.
+static void async_change_cb(void *p)
+{
+ struct m_config *config = p;
+
+ void *ptr;
+ while (m_config_cache_get_next_changed(config->cache, &ptr)) {
+ // Regrettable linear search, might degenerate to quadratic.
+ for (int n = 0; n < config->num_opts; n++) {
+ struct m_config_option *co = &config->opts[n];
+ if (co->data == ptr) {
+ if (config->option_change_callback) {
+ config->option_change_callback(
+ config->option_change_callback_ctx, co,
+ config->cache->change_flags, false);
+ }
+ break;
+ }
+ }
+ config->cache->change_flags = 0;
+ }
+}
+
+void m_config_set_update_dispatch_queue(struct m_config *config,
+ struct mp_dispatch_queue *dispatch)
+{
+ m_config_cache_set_dispatch_change_cb(config->cache, dispatch,
+ async_change_cb, config);
+}
+
+// Normally m_config_cache will not send notifications when _we_ change our
+// own stuff. For whatever funny reasons, we need that, though.
+static void force_self_notify_change_opt(struct m_config *config,
+ struct m_config_option *co,
+ bool self_notification)
+{
+ int changed =
+ get_option_change_mask(config->shadow, co->group_index, 0, co->opt);
+
+ if (config->option_change_callback) {
+ config->option_change_callback(config->option_change_callback_ctx, co,
+ changed, self_notification);
+ }
+}
+
+void m_config_notify_change_opt_ptr(struct m_config *config, void *ptr)
+{
+ for (int n = 0; n < config->num_opts; n++) {
+ struct m_config_option *co = &config->opts[n];
+ if (co->data == ptr) {
+ if (m_config_cache_write_opt(config->cache, co->data))
+ force_self_notify_change_opt(config, co, true);
+ return;
+ }
+ }
+ // ptr doesn't point to any config->optstruct field declared in the
+ // option list?
+ assert(false);
+}
+
int m_config_set_option_raw(struct m_config *config,
struct m_config_option *co,
void *data, int flags)
@@ -959,10 +1024,11 @@ int m_config_set_option_raw(struct m_config *config,
if (!co->data)
return flags & M_SETOPT_FROM_CMDLINE ? 0 : M_OPT_UNKNOWN;
- m_option_copy(co->opt, co->data, data);
-
m_config_mark_co_flags(co, flags);
- m_config_notify_change_co(config, co);
+
+ m_option_copy(co->opt, co->data, data);
+ if (m_config_cache_write_opt(config->cache, co->data))
+ force_self_notify_change_opt(config, co, false);
return 0;
}
@@ -1356,11 +1422,10 @@ static void cache_destroy(void *p)
m_config_cache_set_dispatch_change_cb(cache, NULL, NULL, NULL);
}
-struct m_config_cache *m_config_cache_alloc(void *ta_parent,
- struct mpv_global *global,
+static struct m_config_cache *m_config_cache_alloc_internal(void *ta_parent,
+ struct m_config_shadow *shadow,
const struct m_sub_options *group)
{
- struct m_config_shadow *shadow = global->config;
int group_index = -1;
for (int n = 0; n < shadow->num_groups; n++) {
@@ -1397,6 +1462,13 @@ struct m_config_cache *m_config_cache_alloc(void *ta_parent,
return cache;
}
+struct m_config_cache *m_config_cache_alloc(void *ta_parent,
+ struct mpv_global *global,
+ const struct m_sub_options *group)
+{
+ return m_config_cache_alloc_internal(ta_parent, global->config, group);
+}
+
static void update_next_option(struct m_config_cache *cache, void **p_opt)
{
struct config_cache *in = cache->internal;
@@ -1577,54 +1649,6 @@ bool m_config_cache_write_opt(struct m_config_cache *cache, void *ptr)
return changed;
}
-void m_config_notify_change_co(struct m_config *config,
- struct m_config_option *co)
-{
- struct m_config_shadow *shadow = config->shadow;
- assert(co->data);
-
- if (shadow) {
- pthread_mutex_lock(&shadow->lock);
-
- struct m_config_data *data = shadow->data;
- struct m_group_data *gdata = m_config_gdata(data, co->group_index);
- assert(gdata);
-
- gdata->ts = atomic_fetch_add(&shadow->ts, 1) + 1;
-
- m_option_copy(co->opt, gdata->udata + co->opt->offset, co->data);
-
- for (int n = 0; n < shadow->num_listeners; n++) {
- struct config_cache *cache = shadow->listeners[n];
- if (cache->wakeup_cb && m_config_gdata(cache->data, co->group_index))
- cache->wakeup_cb(cache->wakeup_cb_ctx);
- }
-
- pthread_mutex_unlock(&shadow->lock);
- }
-
- int changed = get_option_change_mask(shadow, co->group_index, 0, co->opt);
-
- if (config->option_change_callback) {
- config->option_change_callback(config->option_change_callback_ctx, co,
- changed);
- }
-}
-
-void m_config_notify_change_opt_ptr(struct m_config *config, void *ptr)
-{
- for (int n = 0; n < config->num_opts; n++) {
- struct m_config_option *co = &config->opts[n];
- if (co->data == ptr) {
- m_config_notify_change_co(config, co);
- return;
- }
- }
- // ptr doesn't point to any config->optstruct field declared in the
- // option list?
- assert(false);
-}
-
void m_config_cache_set_wakeup_cb(struct m_config_cache *cache,
void (*cb)(void *ctx), void *cb_ctx)
{
diff --git a/options/m_config.h b/options/m_config.h
index f62bc9670f..25d26e05ee 100644
--- a/options/m_config.h
+++ b/options/m_config.h
@@ -74,8 +74,11 @@ typedef struct m_config {
// Notification after an option was successfully written to.
// Uses flags as set in UPDATE_OPTS_MASK.
+ // self_update==true means the update was caused by a call to
+ // m_config_notify_change_opt_ptr(). If false, it's caused either by
+ // m_config_set_option_*() (and similar) calls or external updates.
void (*option_change_callback)(void *ctx, struct m_config_option *co,
- int flags);
+ int flags, bool self_update);
void *option_change_callback_ctx;
// For the command line parser
@@ -84,7 +87,8 @@ typedef struct m_config {
void *optstruct; // struct mpopts or other
// Private. Non-NULL if data was allocated. m_config_option.data uses it.
- struct m_config_data *data;
+ // API users call m_config_set_update_dispatch_queue() to get async updates.
+ struct m_config_cache *cache;
// Private. Thread-safe shadow memory; only set for the main m_config.
struct m_config_shadow *shadow;
@@ -189,11 +193,7 @@ const char *m_config_get_positional_option(const struct m_config *config, int n)
int m_config_option_requires_param(struct m_config *config, bstr name);
// Notify m_config_cache users that the option has (probably) changed its value.
-void m_config_notify_change_co(struct m_config *config,
- struct m_config_option *co);
-// Like m_config_notify_change_co(), but automatically find the option by its
-// pointer within the global option struct (config->optstruct). In practice,
-// it means it works only on fields in MPContext.opts.
+// This will force a self-notification back to config->option_change_callback.
void m_config_notify_change_opt_ptr(struct m_config *config, void *ptr);
// Return all (visible) option names as NULL terminated string list.
@@ -253,6 +253,10 @@ int m_config_set_profile(struct m_config *config, char *name, int flags);
struct mpv_node m_config_get_profiles(struct m_config *config);
+// Run async option updates here. This will call option_change_callback() on it.
+void m_config_set_update_dispatch_queue(struct m_config *config,
+ struct mp_dispatch_queue *dispatch);
+
// This can be used to create and synchronize per-thread option structs,
// which then can be read without synchronization. No concurrent access to
// the cache itself is allowed.