Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input, dec_sub: avoid unnecessary recursive locks and don't use recursive mutex #13818

Merged
merged 8 commits into from
Apr 17, 2024

Conversation

na-na-hi
Copy link
Contributor

@na-na-hi na-na-hi commented Apr 4, 2024

This restructures code by establishing a call hierarchy between public and private functions, so that any lock is called no more than necessary and recursive locks are no longer necessary for input.c and dec_sub.c, which means they no longer need recursive mutex. All uses of recursive mutex are now removed except vdpau.c, where we call a function outside of our control inside a lock.

See also: #9727 (comment)

This is a public function, yet its access to ictx through
get_bind_section is not locked.

Fixes: 4614d43
This makes it easy to eyeball check the call hierarchy between public
and private functions.
Copy link

github-actions bot commented Apr 4, 2024

Download the artifacts for this pull request:

Windows
macOS

input/input.c Outdated Show resolved Hide resolved
input/input.c Outdated Show resolved Hide resolved
input/input.c Show resolved Hide resolved
input/input.c Show resolved Hide resolved
The absense of a call hierarchy between public and private functions
results in many unnecessary recursive locks: public functions require
locks, which are also called by other public and private functions in this
file. Fortunately, since the lock is private to this file, this situation
can be avoided by establishing a call hierarchy:

- Public functions must lock, and can only call private functions in
  this file
- Private functions must not lock, and can only call private functions
  in this file
- No function can call any public function in this file, the only
  exception being mp_input_wakeup and mp_input_parse_cmd.

This arrangement ensures that there will be no locks more than necessary:
All public function calls will lock only once, and never recursively.
Previous commits made sure that the lock will never be called for more
than once for all public functions. Thus deadlock is impossible, so
recursive mutex is unneeded and can be converted to a normal mutex.
These public functions should use locks to keep its usage
consistent with input.c.

Fixes: 024e0cd
92a9f11 added locking for dec_sub.
At that time, because the lock was exposed to the outside world,
a recursive mutex was used. However, this is no longer true after
e9e883e, when the public locking
functions were removed. This means that the lock is now private.

Unlike input.c, dec_sub already enforces said call hierarchy, so
combined with the aforementioned change, the lock is only ever
called once and never recursively. Thus the lock can be converted to
a normal mutex.
It's currently always a meaningless 1. Make it so it returns 0 is cmd
is NULL. Remove the unused return value from queue_cmd.
sub->sd can be destroyed and recreated when update_segment is called
inside a lock.

Fixes: f9918b5
@na-na-hi
Copy link
Contributor Author

na-na-hi commented Apr 7, 2024

Added a fix for another locking issue I found in dec_sub.c.

@kasper93 kasper93 added this to the Release v0.39.0 milestone Apr 9, 2024
@kasper93 kasper93 merged commit 715feea into mpv-player:master Apr 17, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants