<html>
 <body>
  <div style="font-family: Verdana, Arial, Helvetica, Sans-Serif;">
   <table bgcolor="#f9f3c9" width="100%" cellpadding="8" style="border: 1px #c9c399 solid;">
    <tr>
     <td>
      This is an automatically generated e-mail. To reply, visit:
      <a href="https://reviewboard.asterisk.org/r/3405/">https://reviewboard.asterisk.org/r/3405/</a>
     </td>
    </tr>
   </table>
   <br />




<table bgcolor="#fefadf" width="100%" cellspacing="0" cellpadding="8" style="background-image: url('https://reviewboard.asterisk.org/static/rb/images/review_request_box_top_bg.ab6f3b1072c9.png'); background-position: left top; background-repeat: repeat-x; border: 1px black solid;">
 <tr>
  <td>

<div>Review request for Asterisk Developers.</div>
<div>By George Joseph.</div>


<p style="color: grey;"><i>Updated March 28, 2014, 10:56 a.m.</i></p>



<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Changes</h1>
<table width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="10" style="border: 1px solid #b8b5a0">
 <tr>
  <td>
   <pre style="margin: 0; padding: 0; white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Added link to test harness and test results.</pre>
  </td>
 </tr>
</table>





<div style="margin-top: 1.5em;">
 <b style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Bugs: </b>


 <a href="https://issues.asterisk.org/jira/browse/ASTERISK-23553">ASTERISK-23553</a>


</div>



<div style="margin-top: 1.5em;">
 <b style="color: #575012; font-size: 10pt;">Repository: </b>
Asterisk
</div>


<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Description  (updated)</h1>
 <table width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="10" style="border: 1px solid #b8b5a0">
 <tr>
  <td>
   <pre style="margin: 0; padding: 0; white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">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.
</pre>
  </td>
 </tr>
</table>



<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Diffs</b> </h1>
<ul style="margin-left: 3em; padding-left: 0;">

 <li>branches/12/include/asterisk/lock.h <span style="color: grey">(411364)</span></li>

 <li>branches/12/include/asterisk/autoconfig.h.in <span style="color: grey">(411364)</span></li>

 <li>branches/12/configure.ac <span style="color: grey">(411364)</span></li>

 <li>branches/12/configure <span style="color: grey">(UNKNOWN)</span></li>

</ul>

<p><a href="https://reviewboard.asterisk.org/r/3405/diff/" style="margin-left: 3em;">View Diff</a></p>







  </td>
 </tr>
</table>




  </div>
 </body>
</html>