]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Add ao2_trylock() to go along with ao2_lock() and ao2_unlock()
authorRussell Bryant <russell@russellbryant.com>
Tue, 25 Nov 2008 17:34:50 +0000 (17:34 +0000)
committerRussell Bryant <russell@russellbryant.com>
Tue, 25 Nov 2008 17:34:50 +0000 (17:34 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@159158 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/astobj2.h
main/astobj2.c

index 87c7f7353f32b3fd91f467bb704e1b1e4d3146f9..1374e29656bc71c51779bf87ac0b09c8797c7a48 100644 (file)
@@ -192,6 +192,13 @@ int ao2_lock(void *a);
 int _ao2_lock(void *a, const char *file, const char *func, int line, const char *var);
 #endif
 
+#ifndef DEBUG_THREADS
+int ao2_trylock(void *a);
+#else
+#define ao2_trylock(a) _ao2_trylock(a, __FILE__, __PRETTY_FUNCTION__, __LINE__, #a)
+int _ao2_trylock(void *a, const char *file, const char *func, int line, const char *var);
+#endif
+
 /*!
  * Unlock an object.
  * 
index 2614cf7b41307b4aba5c2e807604769a43997b1c..efebaf42f93316d2065c412e952ba4a915722f8b 100644 (file)
@@ -147,6 +147,33 @@ int _ao2_lock(void *user_data, const char *file, const char *func, int line, con
 #endif
 }
 
+#ifndef DEBUG_THREADS
+int ao2_trylock(void *user_data)
+#else
+int _ao2_trylock(void *user_data, const char *file, const char *func, int line, const char *var)
+#endif
+{
+       struct astobj2 *p = INTERNAL_OBJ(user_data);
+       int res;
+
+       if (p == NULL)
+               return -1;
+
+#ifndef DEBUG_THREADS
+       res = ast_mutex_trylock(&p->priv_data.lock);
+#else
+       res = __ast_pthread_mutex_trylock(file, line, func, var, &p->priv_data.lock);
+#endif
+
+#ifdef AO2_DEBUG
+       if (!res) {
+               ast_atomic_fetchadd_int(&ao2.total_locked, 1);
+       }
+#endif
+
+       return res;
+}
+
 #ifndef DEBUG_THREADS
 int ao2_unlock(void *user_data)
 #else