You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ok...@apache.org on 2017/11/10 03:10:09 UTC

[trafficserver] branch master updated: Optimize: update ink_thread_create with thread-safe support

This is an automated email from the ASF dual-hosted git repository.

oknet 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 ab7b372  Optimize: update ink_thread_create with thread-safe support
ab7b372 is described below

commit ab7b37238cb4c31e6831488947c4226f8514d448
Author: Oknet Xu <xu...@skyguard.com.cn>
AuthorDate: Thu Nov 9 23:33:49 2017 +0800

    Optimize: update ink_thread_create with thread-safe support
---
 cmd/traffic_manager/MgmtHandlers.cc    |  2 +-
 cmd/traffic_manager/traffic_manager.cc |  6 +++---
 iocore/eventsystem/I_Thread.h          |  2 +-
 iocore/eventsystem/Thread.cc           | 18 +++---------------
 lib/records/RecLocal.cc                |  4 ++--
 lib/ts/ink_thread.h                    | 12 +++++++-----
 lib/ts/signals.cc                      |  2 +-
 lib/ts/test_freelist.cc                |  2 +-
 mgmt/ProcessManager.cc                 |  2 +-
 mgmt/api/CoreAPIRemote.cc              |  4 ++--
 mgmt/api/NetworkUtilsRemote.cc         |  4 ++--
 proxy/InkIOCoreAPI.cc                  |  4 +++-
 12 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/cmd/traffic_manager/MgmtHandlers.cc b/cmd/traffic_manager/MgmtHandlers.cc
index dafef1e..b2e3951 100644
--- a/cmd/traffic_manager/MgmtHandlers.cc
+++ b/cmd/traffic_manager/MgmtHandlers.cc
@@ -266,7 +266,7 @@ mgmt_synthetic_main(void *)
       mgmt_log("[SyntheticHealthServer] Connect by disallowed client %s, closing\n", inet_ntoa(clientInfo.sin_addr));
       close_socket(clientFD);
     } else {
-      ink_thread_create(synthetic_thread, (void *)&clientFD, 1, 0, nullptr);
+      ink_thread_create(nullptr, synthetic_thread, (void *)&clientFD, 1, 0, nullptr);
     }
   }
 
diff --git a/cmd/traffic_manager/traffic_manager.cc b/cmd/traffic_manager/traffic_manager.cc
index 159f4ae..2d286ba 100644
--- a/cmd/traffic_manager/traffic_manager.cc
+++ b/cmd/traffic_manager/traffic_manager.cc
@@ -642,7 +642,7 @@ main(int argc, const char **argv)
   // can keep a consistent euid when create mgmtapi/eventapi unix
   // sockets in mgmt_synthetic_main thread.
   //
-  synthThrId = ink_thread_create(mgmt_synthetic_main, nullptr, 0, 0, nullptr); /* Spin web agent thread */
+  ink_thread_create(&synthThrId, mgmt_synthetic_main, nullptr, 0, 0, nullptr); /* Spin web agent thread */
   Debug("lm", "Created Web Agent thread (%" PRId64 ")", (int64_t)synthThrId);
 
   // Setup the API and event sockets
@@ -670,8 +670,8 @@ main(int argc, const char **argv)
   }
 
   umask(oldmask);
-  ink_thread_create(ts_ctrl_main, &mgmtapiFD, 0, 0, nullptr);
-  ink_thread_create(event_callback_main, &eventapiFD, 0, 0, nullptr);
+  ink_thread_create(nullptr, ts_ctrl_main, &mgmtapiFD, 0, 0, nullptr);
+  ink_thread_create(nullptr, event_callback_main, &eventapiFD, 0, 0, nullptr);
 
   mgmt_log("[TrafficManager] Setup complete\n");
 
diff --git a/iocore/eventsystem/I_Thread.h b/iocore/eventsystem/I_Thread.h
index f053dec..26573c4 100644
--- a/iocore/eventsystem/I_Thread.h
+++ b/iocore/eventsystem/I_Thread.h
@@ -151,7 +151,7 @@ public:
       @c nullptr a stack of size @a stacksize is allocated and used. If @a f is present and valid it
       is called in the thread context. Otherwise the method @c execute is invoked.
   */
-  ink_thread start(const char *name, void *stack, size_t stacksize, ThreadFunction const &f = ThreadFunction());
+  void start(const char *name, void *stack, size_t stacksize, ThreadFunction const &f = ThreadFunction());
 
   virtual void execute() = 0;
 
diff --git a/iocore/eventsystem/Thread.cc b/iocore/eventsystem/Thread.cc
index 4eff26b..cfb51e0 100644
--- a/iocore/eventsystem/Thread.cc
+++ b/iocore/eventsystem/Thread.cc
@@ -68,7 +68,6 @@ Thread::~Thread()
 struct thread_data_internal {
   ThreadFunction f;                  ///< Function to excecute in the thread.
   Thread *me;                        ///< The class instance.
-  ink_mutex mutex;                   ///< Startup mutex.
   char name[MAX_THREAD_NAME_LENGTH]; ///< Name for the thread.
 };
 
@@ -77,11 +76,6 @@ spawn_thread_internal(void *a)
 {
   auto *p = static_cast<thread_data_internal *>(a);
 
-  { // force wait until parent thread is ready.
-    ink_scoped_mutex_lock lock(p->mutex);
-  }
-  ink_mutex_destroy(&p->mutex);
-
   p->me->set_specific();
   ink_set_thread_name(p->name);
 
@@ -95,21 +89,15 @@ spawn_thread_internal(void *a)
   return nullptr;
 }
 
-ink_thread
+void
 Thread::start(const char *name, void *stack, size_t stacksize, ThreadFunction const &f)
 {
-  auto *p = new thread_data_internal{f, this, {}, {0}};
+  auto *p = new thread_data_internal{f, this, ""};
 
   ink_zero(p->name);
   ink_strlcpy(p->name, name, MAX_THREAD_NAME_LENGTH);
-  ink_mutex_init(&p->mutex);
   if (stacksize == 0) {
     stacksize = DEFAULT_STACKSIZE;
   }
-  { // must force assignment to complete before thread touches "this".
-    ink_scoped_mutex_lock lock(&p->mutex);
-    tid = ink_thread_create(spawn_thread_internal, p, 0, stacksize, stack);
-  }
-
-  return tid;
+  ink_thread_create(&tid, spawn_thread_internal, p, 0, stacksize, stack);
 }
diff --git a/lib/records/RecLocal.cc b/lib/records/RecLocal.cc
index 6ec4b72..ef88e91 100644
--- a/lib/records/RecLocal.cc
+++ b/lib/records/RecLocal.cc
@@ -215,8 +215,8 @@ RecLocalInitMessage()
 int
 RecLocalStart(FileManager *configFiles)
 {
-  ink_thread_create(sync_thr, configFiles, 0, 0, nullptr);
-  ink_thread_create(config_update_thr, nullptr, 0, 0, nullptr);
+  ink_thread_create(nullptr, sync_thr, configFiles, 0, 0, nullptr);
+  ink_thread_create(nullptr, config_update_thr, nullptr, 0, 0, nullptr);
   return REC_ERR_OKAY;
 }
 
diff --git a/lib/ts/ink_thread.h b/lib/ts/ink_thread.h
index 501170f..af4362f 100644
--- a/lib/ts/ink_thread.h
+++ b/lib/ts/ink_thread.h
@@ -127,13 +127,17 @@ ink_thread_key_delete(ink_thread_key key)
   ink_assert(!pthread_key_delete(key));
 }
 
-static inline ink_thread
-ink_thread_create(void *(*f)(void *), void *a, int detached, size_t stacksize, void *stack)
+static inline void
+ink_thread_create(ink_thread *tid, void *(*f)(void *), void *a, int detached, size_t stacksize, void *stack)
 {
   ink_thread t;
   int ret;
   pthread_attr_t attr;
 
+  if (tid == nullptr) {
+    tid = &t;
+  }
+
   pthread_attr_init(&attr);
   pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
 
@@ -149,13 +153,11 @@ ink_thread_create(void *(*f)(void *), void *a, int detached, size_t stacksize, v
     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
   }
 
-  ret = pthread_create(&t, &attr, f, a);
+  ret = pthread_create(tid, &attr, f, a);
   if (ret != 0) {
     ink_abort("pthread_create() failed: %s (%d)", strerror(ret), ret);
   }
   pthread_attr_destroy(&attr);
-
-  return t;
 }
 
 static inline void
diff --git a/lib/ts/signals.cc b/lib/ts/signals.cc
index 3980d27..b650cc9 100644
--- a/lib/ts/signals.cc
+++ b/lib/ts/signals.cc
@@ -116,7 +116,7 @@ check_signal_thread(void *ptr)
 void
 signal_start_check_thread(signal_handler_t handler)
 {
-  ink_thread_create(check_signal_thread, (void *)handler, 0, 0, nullptr);
+  ink_thread_create(nullptr, check_signal_thread, (void *)handler, 0, 0, nullptr);
 }
 
 bool
diff --git a/lib/ts/test_freelist.cc b/lib/ts/test_freelist.cc
index 19d269b..a356155 100644
--- a/lib/ts/test_freelist.cc
+++ b/lib/ts/test_freelist.cc
@@ -74,7 +74,7 @@ main(int /* argc ATS_UNUSED */, char * /*argv ATS_UNUSED */ [])
 
   for (i = 0; i < NTHREADS; i++) {
     fprintf(stderr, "Create thread %d\n", i);
-    ink_thread_create(test, (void *)((intptr_t)i), 0, 0, nullptr);
+    ink_thread_create(nullptr, test, (void *)((intptr_t)i), 0, 0, nullptr);
   }
 
   test((void *)NTHREADS);
diff --git a/mgmt/ProcessManager.cc b/mgmt/ProcessManager.cc
index 7a249a4..e382316 100644
--- a/mgmt/ProcessManager.cc
+++ b/mgmt/ProcessManager.cc
@@ -100,7 +100,7 @@ ProcessManager::start(std::function<void()> const &cb)
 
   ink_release_assert(running == 0);
   ink_atomic_increment(&running, 1);
-  poll_thread = ink_thread_create(processManagerThread, nullptr, 0, 0, nullptr);
+  ink_thread_create(&poll_thread, processManagerThread, nullptr, 0, 0, nullptr);
 }
 
 void
diff --git a/mgmt/api/CoreAPIRemote.cc b/mgmt/api/CoreAPIRemote.cc
index fb3d540..59e82e2 100644
--- a/mgmt/api/CoreAPIRemote.cc
+++ b/mgmt/api/CoreAPIRemote.cc
@@ -227,7 +227,7 @@ Init(const char *socket_path, TSInitOptionT options)
 
   // if connected, create event thread that listens for events from TM
   if (0 == (ts_init_options & TS_MGMT_OPT_NO_EVENTS)) {
-    ts_event_thread = ink_thread_create(event_poll_thread_main, &event_socket_fd, 0, 0, nullptr);
+    ink_thread_create(&ts_event_thread, event_poll_thread_main, &event_socket_fd, 0, 0, nullptr);
   } else {
     ts_event_thread = ink_thread_null();
   }
@@ -237,7 +237,7 @@ END:
   // create thread that periodically checks the socket connection
   // with TM alive - reconnects if not alive
   if (0 == (ts_init_options & TS_MGMT_OPT_NO_SOCK_TESTS)) {
-    ts_test_thread = ink_thread_create(socket_test_thread, nullptr, 0, 0, nullptr);
+    ink_thread_create(&ts_test_thread, socket_test_thread, nullptr, 0, 0, nullptr);
   } else {
     ts_test_thread = ink_thread_null();
   }
diff --git a/mgmt/api/NetworkUtilsRemote.cc b/mgmt/api/NetworkUtilsRemote.cc
index ca5914d..4629ae1 100644
--- a/mgmt/api/NetworkUtilsRemote.cc
+++ b/mgmt/api/NetworkUtilsRemote.cc
@@ -238,7 +238,7 @@ reconnect()
 
   // relaunch a new event thread since socket_fd changed
   if (0 == (ts_init_options & TS_MGMT_OPT_NO_EVENTS)) {
-    ts_event_thread = ink_thread_create(event_poll_thread_main, &event_socket_fd, 0, 0, nullptr);
+    ink_thread_create(&ts_event_thread, event_poll_thread_main, &event_socket_fd, 0, 0, nullptr);
     // reregister the callbacks on the TM side for this new client connection
     if (remote_event_callbacks) {
       err = send_register_all_callbacks(event_socket_fd, remote_event_callbacks);
@@ -652,7 +652,7 @@ event_poll_thread_main(void *arg)
     event->description = desc;
 
     // got event notice; spawn new thread to handle the event's callback functions
-    ink_thread_create(event_callback_thread, (void *)event, 0, 0, nullptr);
+    ink_thread_create(nullptr, event_callback_thread, (void *)event, 0, 0, nullptr);
   }
 
   ink_thread_exit(nullptr);
diff --git a/proxy/InkIOCoreAPI.cc b/proxy/InkIOCoreAPI.cc
index 1cfd300..200cea4 100644
--- a/proxy/InkIOCoreAPI.cc
+++ b/proxy/InkIOCoreAPI.cc
@@ -143,6 +143,7 @@ TSThread
 TSThreadCreate(TSThreadFunc func, void *data)
 {
   INKThreadInternal *thread;
+  ink_thread tid = 0;
 
   thread = new INKThreadInternal;
 
@@ -152,7 +153,8 @@ TSThreadCreate(TSThreadFunc func, void *data)
   thread->func = func;
   thread->data = data;
 
-  if (!(ink_thread_create(ink_thread_trampoline, (void *)thread, 1, 0, nullptr))) {
+  ink_thread_create(&tid, ink_thread_trampoline, (void *)thread, 1, 0, nullptr);
+  if (!tid) {
     return (TSThread) nullptr;
   }
 

-- 
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>'].