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.
Showing
Please register or sign in to comment