- assert( vlc_internals( p_this )->i_refcount > 0 );
- vlc_internals( p_this )->i_refcount--;
- b_should_destroy = (vlc_internals( p_this )->i_refcount == 0);
+ if( internals->i_refcount > 1 )
+ {
+ /* Fast path */
+ /* There are still other references to the object */
+ internals->i_refcount--;
+ vlc_spin_unlock( &internals->ref_spin );
+ return;
+ }
+ vlc_spin_unlock( &internals->ref_spin );
+
+ /* Slow path */
+ /* Remember that we cannot hold the spin while waiting on the mutex */
+ vlc_mutex_lock( &structure_lock );
+ /* Take the spin again. Note that another thread may have yielded the
+ * object in the (very short) mean time. */
+ vlc_spin_lock( &internals->ref_spin );
+ b_should_destroy = --internals->i_refcount == 0;
+ vlc_spin_unlock( &internals->ref_spin );