Add ao2_trylock() to go along with ao2_lock() and ao2_unlock()

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@159158 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2008-11-25 17:34:50 +00:00
parent e8f9274ca7
commit 07741a3261
2 changed files with 34 additions and 0 deletions

View 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); int _ao2_lock(void *a, const char *file, const char *func, int line, const char *var);
#endif #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. * Unlock an object.
* *

View File

@@ -147,6 +147,33 @@ int _ao2_lock(void *user_data, const char *file, const char *func, int line, con
#endif #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 #ifndef DEBUG_THREADS
int ao2_unlock(void *user_data) int ao2_unlock(void *user_data)
#else #else