diff --git a/iocore/eventsystem/I_Lock.h b/iocore/eventsystem/I_Lock.h index 4850199251d..c5b8373bdd5 100644 --- a/iocore/eventsystem/I_Lock.h +++ b/iocore/eventsystem/I_Lock.h @@ -256,7 +256,7 @@ Mutex_trylock( #ifdef DEBUG const SourceLocation &location, const char *ahandler, #endif - Ptr &m, EThread *t) + ProxyMutex *m, EThread *t) { ink_assert(t != nullptr); ink_assert(t == reinterpret_cast(this_thread())); @@ -295,12 +295,26 @@ Mutex_trylock( return true; } +inline bool +Mutex_trylock( +#ifdef DEBUG + const SourceLocation &location, const char *ahandler, +#endif + Ptr &m, EThread *t) +{ + return Mutex_trylock( +#ifdef DEBUG + location, ahandler, +#endif + m.get(), t); +} + inline int Mutex_lock( #ifdef DEBUG const SourceLocation &location, const char *ahandler, #endif - Ptr &m, EThread *t) + ProxyMutex *m, EThread *t) { ink_assert(t != nullptr); if (m->thread_holding != t) { @@ -327,8 +341,22 @@ Mutex_lock( return true; } +inline int +Mutex_lock( +#ifdef DEBUG + const SourceLocation &location, const char *ahandler, +#endif + Ptr &m, EThread *t) +{ + return Mutex_lock( +#ifdef DEBUG + location, ahandler, +#endif + m.get(), t); +} + inline void -Mutex_unlock(Ptr &m, EThread *t) +Mutex_unlock(ProxyMutex *m, EThread *t) { if (m->nthread_holding) { ink_assert(t == m->thread_holding); @@ -351,6 +379,12 @@ Mutex_unlock(Ptr &m, EThread *t) } } +inline void +Mutex_unlock(Ptr &m, EThread *t) +{ + Mutex_unlock(m.get(), t); +} + class WeakMutexLock { private: diff --git a/src/traffic_server/InkIOCoreAPI.cc b/src/traffic_server/InkIOCoreAPI.cc index 25618ef05e0..1740ee1dc50 100644 --- a/src/traffic_server/InkIOCoreAPI.cc +++ b/src/traffic_server/InkIOCoreAPI.cc @@ -235,14 +235,17 @@ TSEventThreadSelf(void) //////////////////////////////////////////////////////////////////// // -// Mutexes +// Mutexes: For TSMutexCreate and TSMutexDestroy, the refcount of the +// ProxyMutex object is not incremented or decremented. If the resulting +// ProxyMutex is passed to a INKContInternal, it's mutex smart pointer +// will take ownership of the ProxyMutex and delete it when the last +// reference is removed. TSMutexDestroy should not be called in that case. // //////////////////////////////////////////////////////////////////// TSMutex TSMutexCreate() { ProxyMutex *mutexp = new_ProxyMutex(); - mutexp->refcount_inc(); // TODO: Remove this when allocations can never fail. sdk_assert(sdk_sanity_check_mutex((TSMutex)mutexp) == TS_SUCCESS); @@ -255,9 +258,9 @@ TSMutexDestroy(TSMutex m) { sdk_assert(sdk_sanity_check_mutex(m) == TS_SUCCESS); ProxyMutex *mutexp = reinterpret_cast(m); - // Decrement the refcount added in TSMutexCreate. Delete if this - // was the last ref count - if (mutexp && mutexp->refcount_dec() == 0) { + + if (mutexp) { + ink_release_assert(mutexp->refcount() == 0); mutexp->free(); } } @@ -296,7 +299,7 @@ void TSMutexLock(TSMutex mutexp) { sdk_assert(sdk_sanity_check_mutex(mutexp) == TS_SUCCESS); - Ptr proxy_mutex(reinterpret_cast(mutexp)); + ProxyMutex *proxy_mutex = reinterpret_cast(mutexp); MUTEX_TAKE_LOCK(proxy_mutex, this_ethread()); } @@ -304,7 +307,7 @@ TSReturnCode TSMutexLockTry(TSMutex mutexp) { sdk_assert(sdk_sanity_check_mutex(mutexp) == TS_SUCCESS); - Ptr proxy_mutex(reinterpret_cast(mutexp)); + ProxyMutex *proxy_mutex = reinterpret_cast(mutexp); return (MUTEX_TAKE_TRY_LOCK(proxy_mutex, this_ethread()) ? TS_SUCCESS : TS_ERROR); } @@ -312,7 +315,7 @@ void TSMutexUnlock(TSMutex mutexp) { sdk_assert(sdk_sanity_check_mutex(mutexp) == TS_SUCCESS); - Ptr proxy_mutex(reinterpret_cast(mutexp)); + ProxyMutex *proxy_mutex(reinterpret_cast(mutexp)); MUTEX_UNTAKE_LOCK(proxy_mutex, this_ethread()); }