[asterisk-dev] [Code Review] 3405: Add ast_spinlock capability to lock.h

George Joseph reviewboard at asterisk.org
Fri Mar 28 11:56:18 CDT 2014


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/3405/
-----------------------------------------------------------

(Updated March 28, 2014, 10:56 a.m.)


Review request for Asterisk Developers.


Changes
-------

Added link to test harness and test results.


Bugs: ASTERISK-23553
    https://issues.asterisk.org/jira/browse/ASTERISK-23553


Repository: Asterisk


Description (updated)
-------

Still testing but I'd like some initial feedback.

In some circumstances the atomic fetch/add/test calls are not quite flexible enough but a full fledged mutex or rwlock is too expensive.

Spin locks are a good solution.  They should be used only for protecting very short blocks of critical code such as simple compares combined with integer math.  Operations that may block, hold a lock, or cause the thread to give up it's timeslice should NEVER be attempted in a spin lock.

So,

Add the following APIs to lock.h

ast_spinlock_init
ast_spinlock_lock
ast_spinlock_trylock
ast_spinlock_unlock

Depending on the capabilities determined by configure, the following implementations will be chosen in order of preference...  OSX Atomics (for OSX only), GCC Atomics, Pthread Spinlock and as a final fallback.. Pthread Mutex.  

Performance...

Test harness: https://github.com/gtjoseph/spintest
Results are in spintest.csv

pthread adaptive mutexes are supposed to give you the best of both spin and mutex lock but testing shows that in this scenario, it's always worse than mutex.  Although it does have less kernel time than plain mutexes, I removed the implementation from this patch.

pthread_mutex is universally supported but shows the effect of context switching when there's lock contention.  It's the last resort and maybe should be removed in favor of a #error stating that no spinlock implementation could be found.

pthread_spinlock is gaining support but is not in all pthread implementations (OSX for one).  No kernel time at all.

gcc_atomics is also gaining support and seems to be more widely supported than pthread_spinlock.  No kernel time at all.

With infrequent lock contention, both gcc_atomics and pthread_spinlock are comparable to ast_atomic_fetchadd_int in performance. 

Although I don't have any empirical data to back me up (yet), I believe with osx_atomics, gcc_atomics and pthread_spinlock all major platforms are supported.

EDIT:  I forgot to mention I'm working on a GAS fallback.


Diffs
-----

  branches/12/include/asterisk/lock.h 411364 
  branches/12/include/asterisk/autoconfig.h.in 411364 
  branches/12/configure.ac 411364 
  branches/12/configure UNKNOWN 

Diff: https://reviewboard.asterisk.org/r/3405/diff/


Testing
-------


Thanks,

George Joseph

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20140328/8c229748/attachment-0001.html>


More information about the asterisk-dev mailing list