[asterisk-commits] gtjoseph: trunk r412977 - in /trunk: ./ include/asterisk/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 23 15:13:39 CDT 2014


Author: gtjoseph
Date: Wed Apr 23 15:13:30 2014
New Revision: 412977

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=412977
Log:
This patch adds support for spinlocks in Asterisk.

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

Added:
    trunk/include/asterisk/spinlock.h
      - copied unchanged from r412976, branches/12/include/asterisk/spinlock.h
Modified:
    trunk/   (props changed)
    trunk/configure
    trunk/configure.ac
    trunk/include/asterisk/autoconfig.h.in

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-12-merged' - no diff available.

Modified: trunk/configure.ac
URL: http://svnview.digium.com/svn/asterisk/trunk/configure.ac?view=diff&rev=412977&r1=412976&r2=412977
==============================================================================
--- trunk/configure.ac (original)
+++ trunk/configure.ac Wed Apr 23 15:13:30 2014
@@ -818,6 +818,22 @@
 AC_MSG_RESULT(no)
 )
 
+AC_MSG_CHECKING(for PTHREAD_MUTEX_ADAPTIVE_NP in pthread.h)
+AC_LINK_IFELSE(
+[AC_LANG_PROGRAM([#include <pthread.h>], [int a = PTHREAD_MUTEX_ADAPTIVE_NP;])],
+AC_MSG_RESULT(yes)
+AC_DEFINE([HAVE_PTHREAD_MUTEX_ADAPTIVE_NP], 1, [Define to 1 if your system defines PTHREAD_MUTEX_ADAPTIVE_NP in pthread.h]),
+AC_MSG_RESULT(no)
+)
+
+AC_MSG_CHECKING(for pthread_spinlock_t in pthread.h)
+AC_LINK_IFELSE(
+[AC_LANG_PROGRAM([#include <pthread.h>], [pthread_spinlock_t spin;])],
+AC_MSG_RESULT(yes)
+AC_DEFINE([HAVE_PTHREAD_SPINLOCK], 1, [Define to 1 if your system has pthread_spinlock_t in pthread.h]),
+AC_MSG_RESULT(no)
+)
+
 save_LIBS="$LIBS"
 save_CFLAGS="$CFLAGS"
 LIBS="$PTHREAD_LIBS $LIBS"

Modified: trunk/include/asterisk/autoconfig.h.in
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/autoconfig.h.in?view=diff&rev=412977&r1=412976&r2=412977
==============================================================================
--- trunk/include/asterisk/autoconfig.h.in (original)
+++ trunk/include/asterisk/autoconfig.h.in Wed Apr 23 15:13:30 2014
@@ -667,6 +667,10 @@
 /* Define if you have POSIX threads libraries and header files. */
 #undef HAVE_PTHREAD
 
+/* Define to 1 if your system defines PTHREAD_MUTEX_ADAPTIVE_NP in pthread.h
+   */
+#undef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
+
 /* Define to 1 if your system defines PTHREAD_MUTEX_RECURSIVE_NP in pthread.h
    */
 #undef HAVE_PTHREAD_MUTEX_RECURSIVE_NP
@@ -684,6 +688,9 @@
 
 /* Define if your system has pthread_rwlock_timedwrlock() */
 #undef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
+
+/* Define to 1 if your system has pthread_spinlock_t in pthread.h */
+#undef HAVE_PTHREAD_SPINLOCK
 
 /* Define to 1 if the system has the type `ptrdiff_t'. */
 #undef HAVE_PTRDIFF_T




More information about the asterisk-commits mailing list