do {
res = ast_mutex_trylock(&pri->lock);
if (res) {
- ast_mutex_unlock(&pvt->lock);
- /* Release the lock and try again */
- usleep(1);
- ast_mutex_lock(&pvt->lock);
+ DEADLOCK_AVOIDANCE(&pvt->lock);
}
} while (res);
/* Then break the poll */
ast_channel_lock(c0);
while (ast_channel_trylock(c1)) {
- ast_channel_unlock(c0);
- usleep(1);
- ast_channel_lock(c0);
+ CHANNEL_DEADLOCK_AVOIDANCE(c0);
}
p0 = c0->tech_pvt;
ast_channel_lock(c0);
while (ast_channel_trylock(c1)) {
- ast_channel_unlock(c0);
- usleep(1);
- ast_channel_lock(c0);
+ CHANNEL_DEADLOCK_AVOIDANCE(c0);
}
p0 = c0->tech_pvt;
the private structure -- not especially easy or clean */
while (p->subs[SUB_THREEWAY].owner && ast_channel_trylock(p->subs[SUB_THREEWAY].owner)) {
/* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
- ast_mutex_unlock(&p->lock);
- ast_channel_unlock(ast);
- usleep(1);
+ DLA_UNLOCK(&p->lock);
+ CHANNEL_DEADLOCK_AVOIDANCE(ast);
/* We can grab ast and p in that order, without worry. We should make sure
nothing seriously bad has happened though like some sort of bizarre double
masquerade! */
- ast_channel_lock(ast);
- ast_mutex_lock(&p->lock);
+ DLA_LOCK(&p->lock);
if (p->owner != ast) {
ast_log(LOG_WARNING, "This isn't good...\n");
return NULL;
for (;;) {
if (p->subs[a].owner) {
if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
- ast_mutex_unlock(&p->lock);
- usleep(1);
- ast_mutex_lock(&p->lock);
+ DEADLOCK_AVOIDANCE(&p->lock);
} else {
ast_queue_frame(p->subs[a].owner, &null);
ast_mutex_unlock(&p->subs[a].owner->lock);
ast_mutex_lock(&pvt->lock);
/* Don't hold pvt lock while trying to lock the channel */
while(pvt->owner && ast_channel_trylock(pvt->owner)) {
- ast_mutex_unlock(&pvt->lock);
- usleep(1);
- ast_mutex_lock(&pvt->lock);
+ DEADLOCK_AVOIDANCE(&pvt->lock);
}
if (pvt->owner) {
if (owner) {
if (ast_channel_trylock(owner)) {
ast_debug(3, "Avoiding IAX destroy deadlock\n");
- ast_mutex_unlock(&iaxsl[callno]);
- usleep(1);
- ast_mutex_lock(&iaxsl[callno]);
+ DEADLOCK_AVOIDANCE(&iaxsl[callno]);
goto retry;
}
}
/* Ensure that we have both channels locked */
while (other && ast_channel_trylock(other)) {
- ast_mutex_unlock(&p->lock);
- if (us && us_locked) {
- ast_channel_unlock(us);
- }
- usleep(1);
+ DLA_UNLOCK(&p->lock);
if (us && us_locked) {
- ast_channel_lock(us);
+ CHANNEL_DEADLOCK_AVOIDANCE(us);
+ } else {
+ usleep(1);
}
- ast_mutex_lock(&p->lock);
+ DLA_LOCK(&p->lock);
other = isoutbound ? p->owner : p->chan;
}
} \
} while (0)
+#define DLA_UNLOCK(lock) \
+ do { \
+ const char *__filename, *__func, *__mutex_name; \
+ int __lineno; \
+ int __res = ast_find_lock_info(lock, &__filename, &__lineno, &__func, &__mutex_name); \
+ ast_mutex_unlock(lock);
+
+#define DLA_LOCK(lock) \
+ if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
+ ast_mutex_lock(lock); \
+ } else { \
+ __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
+ } \
+ } while (0)
+
static inline void ast_reentrancy_lock(struct ast_lock_track *lt)
{
pthread_mutex_lock(<->reentr_mutex);
usleep(1); \
ast_mutex_unlock(lock);
+#define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
+
+#define DLA_LOCK(lock) ast_mutex_lock(lock)
+
typedef pthread_mutex_t ast_mutex_t;
#define AST_MUTEX_INIT_VALUE ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)