]> git.sesse.net Git - vlc/commit
Allocate variable and inherit value before the variables lock
authorRémi Denis-Courmont <remi@remlab.net>
Tue, 29 Dec 2009 20:16:32 +0000 (22:16 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Tue, 29 Dec 2009 20:26:19 +0000 (22:26 +0200)
commit8b045d0d882c4509b5af9cc43597579679ec74bf
tree36834afea700a834b0cc1225ee905c95dcde46e2
parent7868eb4ae83b5d195412eb6bca032ca37dd8d30d
Allocate variable and inherit value before the variables lock

The initial value of the variable must be correct when the variables
lock is released after the variable was created. Hence we could not
release the lock between Insert() and InheritValue(). If we did, there
would be a race where another thread could see the variable with the
generic default value (0, "", 0., ...) instead of the inherited value.

So instead, we inherit the value in a temporary variable on the stack,
before we take the variables lock. Then we can create the variable with
the correct value without taking the lock for the duration of
InheritValue().

This adds overhead when an existing variable is re-created (i.e.
reference count is increased but no new variable is created). But it
dramatically reduces contention on the variables lock. More importantly,
it allows calling InheritValue() without the variables lock. So when we
fix InheritValue(), which is currently not thread-safe, we won't have
any problem with nested locking.
src/misc/variables.c