This is an automated email from the ASF dual-hosted git repository.
bneradt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new fcabc256ef Pull INKAPIInternal out of InkAPI.cc as inkcont (#10114)
fcabc256ef is described below
commit fcabc256ef40d43c24dddaf273c2927f95715193
Author: JosiahWI <[email protected]>
AuthorDate: Thu Aug 3 12:47:27 2023 -0500
Pull INKAPIInternal out of InkAPI.cc as inkcont (#10114)
This is the first of three parts of a refactoring to break out
FeatureAPIHooks,
making it possible to unit test hook callbacks.
This moves the implementation of INKAPIInternal from InkAPI.cc to
InkAPIInternal.cc, still in the traffic_server directory. For CMake,
I wrapped it in a new library, inkcont, as a convenient way to document
its dependencies and link it into unit tests. For autotools, I have
just added the new source file to the binary
---
src/traffic_server/CMakeLists.txt | 11 +++
src/traffic_server/InkAPI.cc | 125 +------------------------
src/traffic_server/InkContInternal.cc | 166 ++++++++++++++++++++++++++++++++++
src/traffic_server/Makefile.inc | 1 +
4 files changed, 179 insertions(+), 124 deletions(-)
diff --git a/src/traffic_server/CMakeLists.txt
b/src/traffic_server/CMakeLists.txt
index 35325b9026..7dc6100f09 100644
--- a/src/traffic_server/CMakeLists.txt
+++ b/src/traffic_server/CMakeLists.txt
@@ -15,6 +15,16 @@
#
#######################
+add_library(inkcont STATIC InkContInternal.cc)
+add_library(ts::inkcont ALIAS inkcont)
+
+target_link_libraries(inkcont
+ PRIVATE
+ ts::http_remap
+ ts::inkevent
+ ts::tscore
+)
+
add_executable(traffic_server
Crash.cc
EventName.cc
@@ -36,6 +46,7 @@ target_link_libraries(traffic_server
PRIVATE
ts::tscore
ts::tsapi
+ ts::inkcont
ts::http
ts::http_remap
ts::http2
diff --git a/src/traffic_server/InkAPI.cc b/src/traffic_server/InkAPI.cc
index 7ee1d8f45e..0f2def7fd7 100644
--- a/src/traffic_server/InkAPI.cc
+++ b/src/traffic_server/InkAPI.cc
@@ -394,7 +394,7 @@ static int ts_minor_version = 0;
static int ts_patch_version = 0;
static ClassAllocator<APIHook> apiHookAllocator("apiHookAllocator");
-static ClassAllocator<INKContInternal> INKContAllocator("INKContAllocator");
+extern ClassAllocator<INKContInternal> INKContAllocator;
static ClassAllocator<INKVConnInternal> INKVConnAllocator("INKVConnAllocator");
static ClassAllocator<MIMEFieldSDKHandle>
mHandleAllocator("MIMEFieldSDKHandle");
@@ -1039,129 +1039,6 @@ FileImpl::fgets(char *buf, size_t length)
//
////////////////////////////////////////////////////////////////////
-INKContInternal::INKContInternal()
- : DummyVConnection(nullptr),
- mdata(nullptr),
- m_event_func(nullptr),
- m_event_count(0),
- m_closed(1),
- m_deletable(0),
- m_deleted(0),
- m_context(0),
- m_free_magic(INKCONT_INTERN_MAGIC_ALIVE)
-{
-}
-
-INKContInternal::INKContInternal(TSEventFunc funcp, TSMutex mutexp)
- : DummyVConnection((ProxyMutex *)mutexp),
- mdata(nullptr),
- m_event_func(funcp),
- m_event_count(0),
- m_closed(1),
- m_deletable(0),
- m_deleted(0),
- m_context(0),
- m_free_magic(INKCONT_INTERN_MAGIC_ALIVE)
-{
- SET_HANDLER(&INKContInternal::handle_event);
-}
-
-void
-INKContInternal::init(TSEventFunc funcp, TSMutex mutexp, void *context)
-{
- SET_HANDLER(&INKContInternal::handle_event);
-
- mutex = (ProxyMutex *)mutexp;
- m_event_func = funcp;
- m_context = context;
-}
-
-void
-INKContInternal::clear()
-{
-}
-
-void
-INKContInternal::free()
-{
- clear();
- this->mutex.clear();
- m_free_magic = INKCONT_INTERN_MAGIC_DEAD;
- THREAD_FREE(this, INKContAllocator, this_thread());
-}
-
-void
-INKContInternal::destroy()
-{
- if (m_free_magic == INKCONT_INTERN_MAGIC_DEAD) {
- ink_release_assert(!"Plugin tries to use a continuation which is deleted");
- }
- m_deleted = 1;
- if (m_deletable) {
- free();
- } else {
- // TODO: Should this schedule on some other "thread" ?
- // TODO: we don't care about the return action?
- if (ink_atomic_increment((int *)&m_event_count, 1) < 0) {
- ink_assert(!"not reached");
- }
- EThread *p = this_ethread();
-
- // If this_thread() returns null, the EThread object for the current
thread has been destroyed (or it never existed).
- // Presumably this will only happen during destruction of
statically-initialized objects at TS shutdown, so no further
- // action is needed.
- //
- if (p) {
- p->schedule_imm(this);
- }
- }
-}
-
-void
-INKContInternal::handle_event_count(int event)
-{
- if ((event == EVENT_IMMEDIATE) || (event == EVENT_INTERVAL) || event ==
TS_EVENT_HTTP_TXN_CLOSE) {
- int val = ink_atomic_increment((int *)&m_event_count, -1);
- if (val <= 0) {
- ink_assert(!"not reached");
- }
-
- m_deletable = (m_closed != 0) && (val == 1);
- }
-}
-
-int
-INKContInternal::handle_event(int event, void *edata)
-{
- if (m_free_magic == INKCONT_INTERN_MAGIC_DEAD) {
- ink_release_assert(!"Plugin tries to use a continuation which is deleted");
- }
- handle_event_count(event);
- if (m_deleted) {
- if (m_deletable) {
- free();
- } else {
- Debug("plugin", "INKCont Deletable but not deleted %d", m_event_count);
- }
- } else {
- /* set the plugin context */
- auto *previousContext = pluginThreadContext;
- pluginThreadContext = reinterpret_cast<PluginThreadContext *>(m_context);
- int retval = m_event_func((TSCont)this, (TSEvent)event, edata);
- pluginThreadContext = previousContext;
- if (edata && event == EVENT_INTERVAL) {
- Event *e = reinterpret_cast<Event *>(edata);
- if (e->period != 0) {
- // In the interval case, we must re-increment the m_event_count for
- // the next go around. Otherwise, our event count will go negative.
- ink_release_assert(ink_atomic_increment((int *)&this->m_event_count,
1) >= 0);
- }
- }
- return retval;
- }
- return EVENT_DONE;
-}
-
////////////////////////////////////////////////////////////////////
//
// INKVConnInternal
diff --git a/src/traffic_server/InkContInternal.cc
b/src/traffic_server/InkContInternal.cc
new file mode 100644
index 0000000000..e1a782d31e
--- /dev/null
+++ b/src/traffic_server/InkContInternal.cc
@@ -0,0 +1,166 @@
+/** @file
+
+ Internal SDK stuff
+
+ @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.
+ */
+
+#include "ts/apidefs.h"
+#include "ts/InkAPIPrivateIOCore.h"
+
+#include "tscore/Allocator.h"
+#include "tscore/Diags.h"
+#include "tscore/ink_assert.h"
+#include "tscore/ink_atomic.h"
+
+// http_remap
+#include "RemapPluginInfo.h"
+
+// inkevent
+#include "I_Continuation.h"
+#include "I_EThread.h"
+#include "I_Event.h"
+#include "I_Lock.h"
+#include "I_ProxyAllocator.h"
+#include "I_VConnection.h"
+
+ClassAllocator<INKContInternal> INKContAllocator("INKContAllocator");
+
+INKContInternal::INKContInternal()
+ : DummyVConnection(nullptr),
+ mdata(nullptr),
+ m_event_func(nullptr),
+ m_event_count(0),
+ m_closed(1),
+ m_deletable(0),
+ m_deleted(0),
+ m_context(0),
+ m_free_magic(INKCONT_INTERN_MAGIC_ALIVE)
+{
+}
+
+INKContInternal::INKContInternal(TSEventFunc funcp, TSMutex mutexp)
+ : DummyVConnection((ProxyMutex *)mutexp),
+ mdata(nullptr),
+ m_event_func(funcp),
+ m_event_count(0),
+ m_closed(1),
+ m_deletable(0),
+ m_deleted(0),
+ m_context(0),
+ m_free_magic(INKCONT_INTERN_MAGIC_ALIVE)
+{
+ SET_HANDLER(&INKContInternal::handle_event);
+}
+
+void
+INKContInternal::init(TSEventFunc funcp, TSMutex mutexp, void *context)
+{
+ SET_HANDLER(&INKContInternal::handle_event);
+
+ mutex = (ProxyMutex *)mutexp;
+ m_event_func = funcp;
+ m_context = context;
+}
+
+void
+INKContInternal::clear()
+{
+}
+
+void
+INKContInternal::free()
+{
+ this->clear();
+ this->mutex.clear();
+ m_free_magic = INKCONT_INTERN_MAGIC_DEAD;
+ THREAD_FREE(this, INKContAllocator, this_thread());
+}
+
+void
+INKContInternal::destroy()
+{
+ if (m_free_magic == INKCONT_INTERN_MAGIC_DEAD) {
+ ink_release_assert(!"Plugin tries to use a continuation which is deleted");
+ }
+ m_deleted = 1;
+ if (m_deletable) {
+ this->free();
+ } else {
+ // TODO: Should this schedule on some other "thread" ?
+ // TODO: we don't care about the return action?
+ if (ink_atomic_increment((int *)&m_event_count, 1) < 0) {
+ ink_assert(!"not reached");
+ }
+ EThread *p = this_ethread();
+
+ // If this_thread() returns null, the EThread object for the current
thread has been destroyed (or it never existed).
+ // Presumably this will only happen during destruction of
statically-initialized objects at TS shutdown, so no further
+ // action is needed.
+ //
+ if (p) {
+ p->schedule_imm(this);
+ }
+ }
+}
+
+void
+INKContInternal::handle_event_count(int event)
+{
+ if ((event == EVENT_IMMEDIATE) || (event == EVENT_INTERVAL) || event ==
TS_EVENT_HTTP_TXN_CLOSE) {
+ int val = ink_atomic_increment((int *)&m_event_count, -1);
+ if (val <= 0) {
+ ink_assert(!"not reached");
+ }
+
+ m_deletable = (m_closed != 0) && (val == 1);
+ }
+}
+
+int
+INKContInternal::handle_event(int event, void *edata)
+{
+ if (m_free_magic == INKCONT_INTERN_MAGIC_DEAD) {
+ ink_release_assert(!"Plugin tries to use a continuation which is deleted");
+ }
+ this->handle_event_count(event);
+ if (m_deleted) {
+ if (m_deletable) {
+ this->free();
+ } else {
+ Debug("plugin", "INKCont Deletable but not deleted %d", m_event_count);
+ }
+ } else {
+ /* set the plugin context */
+ auto *previousContext = pluginThreadContext;
+ pluginThreadContext = reinterpret_cast<PluginThreadContext *>(m_context);
+ int retval = m_event_func((TSCont)this, (TSEvent)event, edata);
+ pluginThreadContext = previousContext;
+ if (edata && event == EVENT_INTERVAL) {
+ Event *e = reinterpret_cast<Event *>(edata);
+ if (e->period != 0) {
+ // In the interval case, we must re-increment the m_event_count for
+ // the next go around. Otherwise, our event count will go negative.
+ ink_release_assert(ink_atomic_increment((int *)&this->m_event_count,
1) >= 0);
+ }
+ }
+ return retval;
+ }
+ return EVENT_DONE;
+}
diff --git a/src/traffic_server/Makefile.inc b/src/traffic_server/Makefile.inc
index 4926ce79e6..ad09b56422 100644
--- a/src/traffic_server/Makefile.inc
+++ b/src/traffic_server/Makefile.inc
@@ -51,6 +51,7 @@ traffic_server_traffic_server_SOURCES = \
traffic_server/FetchSM.h \
traffic_server/HostStatus.cc \
traffic_server/InkAPI.cc \
+ traffic_server/InkContInternal.cc \
traffic_server/InkIOCoreAPI.cc \
traffic_server/SocksProxy.cc \
traffic_server/RpcAdminPubHandlers.cc \