diff --git a/iocore/eventsystem/I_Continuation.h b/iocore/eventsystem/I_Continuation.h index b34f78d2525..4eba90f8ed9 100644 --- a/iocore/eventsystem/I_Continuation.h +++ b/iocore/eventsystem/I_Continuation.h @@ -39,6 +39,7 @@ #include "tscore/List.h" #include "I_Lock.h" #include "tscore/ContFlags.h" +#include "I_ThreadAffinity.h" class Continuation; class ContinuationQueue; @@ -94,7 +95,7 @@ class force_VFPT_to_top */ -class Continuation : private force_VFPT_to_top +class Continuation : private force_VFPT_to_top, public ThreadAffinity { public: /** @@ -145,30 +146,6 @@ class Continuation : private force_VFPT_to_top */ ContFlags control_flags; - EThread *thread_affinity = nullptr; - - bool - setThreadAffinity(EThread *ethread) - { - if (ethread != nullptr) { - thread_affinity = ethread; - return true; - } - return false; - } - - EThread * - getThreadAffinity() - { - return thread_affinity; - } - - void - clearThreadAffinity() - { - thread_affinity = nullptr; - } - /** Receives the event code and data for an Event. diff --git a/iocore/eventsystem/I_Lock.h b/iocore/eventsystem/I_Lock.h index d599be26b06..d70ed20b030 100644 --- a/iocore/eventsystem/I_Lock.h +++ b/iocore/eventsystem/I_Lock.h @@ -26,6 +26,7 @@ #include "tscore/ink_platform.h" #include "tscore/Diags.h" #include "I_Thread.h" +#include "I_ThreadAffinity.h" #define MAX_LOCK_TIME HRTIME_MSECONDS(200) #define THREAD_MUTEX_THREAD_HOLDING (-1024 * 1024) @@ -143,7 +144,7 @@ inkcoreapi extern void lock_taken(const SourceLocation &, const char *handler); allow you to lock/unlock the underlying mutex object. */ -class ProxyMutex : public RefCountObj +class ProxyMutex : public RefCountObj, public ThreadAffinity { public: /** @@ -247,6 +248,9 @@ Mutex_trylock( ink_assert(t != nullptr); ink_assert(t == reinterpret_cast(this_thread())); if (m->thread_holding != t) { + if (m->getThreadAffinity() != nullptr) { + ink_release_assert(t == m->getThreadAffinity()); + } if (!ink_mutex_try_acquire(&m->the_mutex)) { #ifdef DEBUG lock_waiting(m->srcloc, m->handler); @@ -290,6 +294,9 @@ Mutex_lock( { ink_assert(t != nullptr); if (m->thread_holding != t) { + if (m->getThreadAffinity() != nullptr) { + ink_release_assert(t == m->getThreadAffinity()); + } ink_mutex_acquire(&m->the_mutex); m->thread_holding = t; ink_assert(m->thread_holding); diff --git a/iocore/eventsystem/I_ThreadAffinity.h b/iocore/eventsystem/I_ThreadAffinity.h new file mode 100644 index 00000000000..a563857372c --- /dev/null +++ b/iocore/eventsystem/I_ThreadAffinity.h @@ -0,0 +1,54 @@ +/** @file + + A brief file description + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + */ + +#pragma once + +class ThreadAffinity +{ +public: + bool + setThreadAffinity(EThread *ethread) + { + if (ethread != nullptr) { + _thread_affinity = ethread; + return true; + } + return false; + } + + EThread * + getThreadAffinity() const + { + return _thread_affinity; + } + + void + clearThreadAffinity() + { + _thread_affinity = nullptr; + } + +private: + EThread *_thread_affinity = nullptr; +}; \ No newline at end of file diff --git a/iocore/net/UnixNet.cc b/iocore/net/UnixNet.cc index c2eded5e1cf..56e9262b3ad 100644 --- a/iocore/net/UnixNet.cc +++ b/iocore/net/UnixNet.cc @@ -225,6 +225,7 @@ initialize_thread_for_net(EThread *thread) new (reinterpret_cast(get_PollCont(thread))) PollCont(thread->mutex, nh); nh->mutex = new_ProxyMutex(); nh->thread = thread; + nh->mutex->setThreadAffinity(thread); PollCont *pc = get_PollCont(thread); PollDescriptor *pd = pc->pollDescriptor;