Skip to content
Snippets Groups Projects
  • George Joseph's avatar
    64045f0b
    This patch adds support for spinlocks in Asterisk. · 64045f0b
    George Joseph authored
    There are cases in Asterisk where it might be desirable to lock
    a short critical code section but not incur the context switch
    and yield penalty of a mutex or rwlock.  The primary spinlock
    implementations execute exclusively in userspace and therefore
    don't incur those penalties.  Spinlocks are NOT meant to be a
    general replacement for mutexes.  They should be used only for
    protecting short blocks of critical code such as simple compares
    and assignments.  Operations that may block, hold a lock, or
    cause the thread to give up it's timeslice should NEVER be
    attempted in a spinlock.
    
    The first use case for spinlocks is in astobj2 - internal_ao2_ref.
    Currently the manipulation of the reference counter is done with
    an ast_atomic_fetchadd_int which works fine.  When weak reference
    containers are introduced however, there's an additional comparison
    and assignment that'll need to be done while the lock is held.
    A mutex would be way too expensive here, hence the spinlock.
    Given that lock contention in this situation would be infrequent,
    the overhead of the spinlock is only a few more machine instructions
    than the current ast_atomic_fetchadd_int call.
    
    ASTERISK-23553 #close
    Review: https://reviewboard.asterisk.org/r/3405/
    ........
    
    Merged revisions 412976 from http://svn.asterisk.org/svn/asterisk/branches/12
    
    
    git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@412977 65c4cc65-6c06-0410-ace0-fbb531ad65f3
    64045f0b
    History
    This patch adds support for spinlocks in Asterisk.
    George Joseph authored
    There are cases in Asterisk where it might be desirable to lock
    a short critical code section but not incur the context switch
    and yield penalty of a mutex or rwlock.  The primary spinlock
    implementations execute exclusively in userspace and therefore
    don't incur those penalties.  Spinlocks are NOT meant to be a
    general replacement for mutexes.  They should be used only for
    protecting short blocks of critical code such as simple compares
    and assignments.  Operations that may block, hold a lock, or
    cause the thread to give up it's timeslice should NEVER be
    attempted in a spinlock.
    
    The first use case for spinlocks is in astobj2 - internal_ao2_ref.
    Currently the manipulation of the reference counter is done with
    an ast_atomic_fetchadd_int which works fine.  When weak reference
    containers are introduced however, there's an additional comparison
    and assignment that'll need to be done while the lock is held.
    A mutex would be way too expensive here, hence the spinlock.
    Given that lock contention in this situation would be infrequent,
    the overhead of the spinlock is only a few more machine instructions
    than the current ast_atomic_fetchadd_int call.
    
    ASTERISK-23553 #close
    Review: https://reviewboard.asterisk.org/r/3405/
    ........
    
    Merged revisions 412976 from http://svn.asterisk.org/svn/asterisk/branches/12
    
    
    git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@412977 65c4cc65-6c06-0410-ace0-fbb531ad65f3