You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bc...@apache.org on 2018/03/08 18:19:39 UTC

[trafficserver] branch master updated: Remove cppapi mutex support since it is redundant with C++11 Thread support lib.

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

bcall 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 a6cb336  Remove cppapi mutex support since it is redundant with C++11 Thread support lib.
a6cb336 is described below

commit a6cb336412ff9769dfeae60a1c260a780993e3d9
Author: Walt Karas <wk...@yahoo-inc.com>
AuthorDate: Tue Mar 6 00:18:00 2018 +0000

    Remove cppapi mutex support since it is redundant with C++11 Thread support lib.
---
 lib/cppapi/InterceptPlugin.cc                    |  31 ++-
 lib/cppapi/Makefile.am                           |   2 -
 lib/cppapi/Mutex.cc                              |  34 ---
 lib/cppapi/TransactionPlugin.cc                  |   3 +-
 lib/cppapi/include/atscppapi/Async.h             |  24 +-
 lib/cppapi/include/atscppapi/Mutex.h             | 284 -----------------------
 lib/cppapi/include/atscppapi/TransactionPlugin.h |  16 +-
 lib/cppapi/utils_internal.cc                     |   3 +-
 8 files changed, 55 insertions(+), 342 deletions(-)

diff --git a/lib/cppapi/InterceptPlugin.cc b/lib/cppapi/InterceptPlugin.cc
index b1f0881..4e53d86 100644
--- a/lib/cppapi/InterceptPlugin.cc
+++ b/lib/cppapi/InterceptPlugin.cc
@@ -135,7 +135,7 @@ InterceptPlugin::~InterceptPlugin()
 bool
 InterceptPlugin::produce(const void *data, int data_size)
 {
-  ScopedSharedMutexLock lock(getMutex());
+  std::lock_guard<Mutex> lock(*getMutex());
   if (!state_->net_vc_) {
     LOG_ERROR("Intercept not operational");
     return false;
@@ -159,7 +159,7 @@ InterceptPlugin::produce(const void *data, int data_size)
 bool
 InterceptPlugin::setOutputComplete()
 {
-  ScopedSharedMutexLock scopedLock(getMutex());
+  std::lock_guard<Mutex> scopedLock(*getMutex());
   if (!state_->net_vc_) {
     LOG_ERROR("Intercept not operational");
     return false;
@@ -317,6 +317,29 @@ InterceptPlugin::handleEvent(int abstract_event, void *edata)
 
 namespace
 {
+class TryLockGuard
+{
+public:
+  TryLockGuard(Mutex &m) : _m(m), _isLocked(m.try_lock()) {}
+
+  bool
+  isLocked() const
+  {
+    return _isLocked;
+  }
+
+  ~TryLockGuard()
+  {
+    if (_isLocked) {
+      _m.unlock();
+    }
+  }
+
+private:
+  std::recursive_mutex &_m;
+  const bool _isLocked;
+};
+
 int
 handleEvents(TSCont cont, TSEvent pristine_event, void *pristine_edata)
 {
@@ -329,8 +352,8 @@ handleEvents(TSCont cont, TSEvent pristine_event, void *pristine_edata)
     return 0;
   }
 
-  ScopedSharedMutexTryLock scopedTryLock(state->plugin_mutex_);
-  if (!scopedTryLock.hasLock()) {
+  TryLockGuard scopedTryLock(*(state->plugin_mutex_));
+  if (!scopedTryLock.isLocked()) {
     LOG_ERROR("Couldn't get plugin lock. Will retry");
     if (event != TS_EVENT_TIMEOUT) { // save only "non-retry" info
       state->saved_event_ = event;
diff --git a/lib/cppapi/Makefile.am b/lib/cppapi/Makefile.am
index 988fef3..87716fc 100644
--- a/lib/cppapi/Makefile.am
+++ b/lib/cppapi/Makefile.am
@@ -33,7 +33,6 @@ libatscppapi_la_SOURCES = \
 	HttpVersion.cc \
 	InterceptPlugin.cc \
 	Logger.cc \
-	Mutex.cc \
 	Plugin.cc \
 	RemapPlugin.cc \
 	Request.cc \
@@ -64,7 +63,6 @@ library_include_HEADERS = \
 	$(base_include_folder)/HttpVersion.h \
 	$(base_include_folder)/InterceptPlugin.h \
 	$(base_include_folder)/Logger.h \
-	$(base_include_folder)/Mutex.h \
 	$(base_include_folder)/Plugin.h \
 	$(base_include_folder)/PluginInit.h \
 	$(base_include_folder)/RemapPlugin.h \
diff --git a/lib/cppapi/Mutex.cc b/lib/cppapi/Mutex.cc
deleted file mode 100644
index 3b20eef..0000000
--- a/lib/cppapi/Mutex.cc
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
-  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.
- */
-
-/**
- * @file Mutex.cc
- */
-
-#include "atscppapi/Mutex.h"
-
-#include <ts/ts.h>
-
-atscppapi::ScopedContinuationLock::ScopedContinuationLock(TSCont contp) : mutex_(TSContMutexGet(contp))
-{
-  TSMutexLock(mutex_);
-}
-atscppapi::ScopedContinuationLock::~ScopedContinuationLock()
-{
-  TSMutexUnlock(mutex_);
-}
diff --git a/lib/cppapi/TransactionPlugin.cc b/lib/cppapi/TransactionPlugin.cc
index 017d2c1..7f6f651 100644
--- a/lib/cppapi/TransactionPlugin.cc
+++ b/lib/cppapi/TransactionPlugin.cc
@@ -25,7 +25,6 @@
 #include <cassert>
 #include <ts/ts.h>
 #include "atscppapi/TransactionPlugin.h"
-#include "atscppapi/Mutex.h"
 #include "utils_internal.h"
 #include "atscppapi/noncopyable.h"
 #include "logging_internal.h"
@@ -40,7 +39,7 @@ struct atscppapi::TransactionPluginState : noncopyable {
   TSCont cont_ = nullptr;
   TSHttpTxn ats_txn_handle_;
   std::shared_ptr<Mutex> mutex_;
-  TransactionPluginState(TSHttpTxn ats_txn_handle) : ats_txn_handle_(ats_txn_handle), mutex_(new Mutex(Mutex::TYPE_RECURSIVE)) {}
+  TransactionPluginState(TSHttpTxn ats_txn_handle) : ats_txn_handle_(ats_txn_handle), mutex_(new Mutex) {}
 };
 
 namespace
diff --git a/lib/cppapi/include/atscppapi/Async.h b/lib/cppapi/include/atscppapi/Async.h
index 7f0231c..8c87f2a 100644
--- a/lib/cppapi/include/atscppapi/Async.h
+++ b/lib/cppapi/include/atscppapi/Async.h
@@ -22,15 +22,22 @@
  */
 
 #pragma once
-#ifndef ATSCPPAPI_ASYNC_H_
-#define ATSCPPAPI_ASYNC_H_
+
 #include <list>
 #include <memory>
-#include <atscppapi/Mutex.h>
+#include <mutex>
+
 #include <atscppapi/noncopyable.h>
 
 namespace atscppapi
 {
+#if !defined(ATSCPPAPI_MUTEX_DEFINED_)
+#define ATSCPPAPI_MUTEX_DEFINED_
+
+using Mutex = std::recursive_mutex;
+
+#endif
+
 /**
  * @private
  *
@@ -118,7 +125,7 @@ public:
   dispatch()
   {
     bool ret = false;
-    ScopedSharedMutexLock scopedLock(dispatch_mutex_);
+    std::lock_guard<Mutex> scopedLock(*dispatch_mutex_);
     if (event_receiver_) {
       event_receiver_->handleAsyncComplete(static_cast<AsyncProviderType &>(*provider_));
       ret = true;
@@ -129,7 +136,7 @@ public:
   void
   disable()
   {
-    ScopedSharedMutexLock scopedLock(dispatch_mutex_);
+    std::lock_guard<Mutex> scopedLock(*dispatch_mutex_);
     event_receiver_ = nullptr;
   }
 
@@ -178,7 +185,7 @@ public:
 
   ~AsyncReceiverPromise()
   {
-    ScopedSharedMutexLock scopedLock(dispatch_controller_->dispatch_mutex_);
+    std::lock_guard<Mutex> scopedLock(*(dispatch_controller_->dispatch_mutex_));
     dispatch_controller_->event_receiver_ = nullptr;
   }
 
@@ -233,7 +240,7 @@ public:
   execute(AsyncReceiver<AsyncProviderType> *event_receiver, AsyncProviderType *provider, std::shared_ptr<Mutex> mutex)
   {
     if (!mutex.get()) {
-      mutex.reset(new Mutex(Mutex::TYPE_RECURSIVE));
+      mutex.reset(new Mutex);
     }
     std::shared_ptr<AsyncDispatchController<AsyncReceiver<AsyncProviderType>, AsyncProviderType>> dispatcher(
       new AsyncDispatchController<AsyncReceiver<AsyncProviderType>, AsyncProviderType>(event_receiver, provider, mutex));
@@ -243,6 +250,5 @@ public:
     provider->doRun(dispatcher);
   }
 };
-}
 
-#endif /* ATSCPPAPI_ASYNC_H_ */
+} // end namespace atscppapi
diff --git a/lib/cppapi/include/atscppapi/Mutex.h b/lib/cppapi/include/atscppapi/Mutex.h
deleted file mode 100644
index c222e90..0000000
--- a/lib/cppapi/include/atscppapi/Mutex.h
+++ /dev/null
@@ -1,284 +0,0 @@
-/**
-  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.
- */
-
-/**
- * @file Mutex.h
- * @brief Contains Mutex related classes for creating a Mutex and locking a Mutex in a specific scope.
- */
-
-#pragma once
-#ifndef ATSCPPAPI_MUTEX_H_
-#define ATSCPPAPI_MUTEX_H_
-
-#include <memory>
-#include <pthread.h>
-#include <atscppapi/noncopyable.h>
-
-// Import in name only.
-typedef struct tsapi_mutex *TSMutex;
-typedef struct tsapi_cont *TSCont;
-
-namespace atscppapi
-{
-/**
- * @brief A mutex is mutual exclusion: a blocking lock.
- *
- * The Mutex class uses pthreads for its implmentation.
- *
- * @see ScopedMutexLock
- * @see ScopedMutexTryLock
- * @see ScopedSharedMutexLock
- * @see ScopedSharedMutexTryLock
- */
-class Mutex : noncopyable
-{
-public:
-  /**
-   * The available types of Mutexes.
-   */
-  enum Type {
-    TYPE_NORMAL = 0, /**< This type of Mutex will deadlock if locked by a thread already holding the lock */
-    TYPE_RECURSIVE,  /**< This type of Mutex will allow a thread holding the lock to lock it again; however, it must be unlocked the
-                        same number of times */
-    TYPE_ERROR_CHECK /**< This type of Mutex will return errno = EDEADLCK if a thread would deadlock by taking the lock after it
-                        already holds it */
-  };
-
-  /**
-   * Create a mutex
-   *
-   * @param type The Type of Mutex to create, the default is TYPE_NORMAL.
-   * @see Type
-   */
-  Mutex(Type type = TYPE_NORMAL)
-  {
-    pthread_mutexattr_t attr;
-    pthread_mutexattr_init(&attr);
-
-    switch (type) {
-    case TYPE_RECURSIVE:
-      pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-      break;
-    case TYPE_ERROR_CHECK:
-      pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
-      break;
-    case TYPE_NORMAL:
-    default:
-      pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
-      break;
-    }
-
-    pthread_mutex_init(&mutex, &attr);
-  }
-
-  ~Mutex() { pthread_mutex_destroy(&mutex); }
-  /**
-   * Try to take the lock, this call will NOT block if the mutex cannot be taken.
-   * @return Returns true if the lock was taken, false if it was not. This call obviously will not block.
-   */
-  bool
-  tryLock()
-  {
-    return !pthread_mutex_trylock(&mutex);
-  }
-
-  /**
-   * Block until the lock is taken, when this call returns the thread will be holding the lock.
-   */
-  void
-  lock()
-  {
-    pthread_mutex_lock(&mutex);
-  }
-
-  /**
-   * Unlock the lock, this call is nonblocking.
-   */
-  void
-  unlock()
-  {
-    pthread_mutex_unlock(&mutex);
-  }
-
-private:
-  pthread_mutex_t mutex; /**< Internal mutex identifier */
-};
-
-/**
- * @brief Take a Mutex reference and lock inside a scope and unlock when the scope is exited.
- *
- * This is an RAII implementation which will lock a mutex at the start of the
- * scope and unlock it when the scope is exited.
- *
- * @see Mutex
- */
-class ScopedMutexLock : noncopyable
-{
-public:
-  /**
-   * Create the scoped mutex lock, once this object is constructed the lock will be held by the thread.
-   * @param mutex a reference to a Mutex.
-   */
-  explicit ScopedMutexLock(Mutex &mutex) : mutex_(mutex) { mutex_.lock(); }
-  /**
-   * Unlock the mutex.
-   */
-  ~ScopedMutexLock() { mutex_.unlock(); }
-
-private:
-  Mutex &mutex_;
-};
-
-/**
- * @brief Take a std::shared_ptr to a Mutex and lock inside a scope and unlock when the scope is exited.
- *
- * This is an RAII implementation which will lock a mutex at the start of the
- * scope and unlock it when the scope is exited.
- *
- * @see Mutex
- */
-class ScopedSharedMutexLock : noncopyable
-{
-public:
-  /**
-   * Create the scoped mutex lock, once this object is constructed the lock will be held by the thread.
-   * @param mutex a shared pointer to a Mutex.
-   */
-  explicit ScopedSharedMutexLock(std::shared_ptr<Mutex> mutex) : mutex_(mutex) { mutex_->lock(); }
-  /**
-   * Unlock the mutex.
-   */
-  ~ScopedSharedMutexLock() { mutex_->unlock(); }
-
-private:
-  std::shared_ptr<Mutex> mutex_;
-};
-
-/**
- * @brief Take a Mutex reference and try to lock inside a scope and unlock when the scope is exited (if the lock was taken).
- *
- * This is an RAII implementation which will lock a mutex at the start of the
- * scope and unlock it when the scope is exited if the lock was taken.
- *
- * @see Mutex
- */
-class ScopedMutexTryLock : noncopyable
-{
-public:
-  /**
-   * Try to create the scoped mutex lock, if you should check hasLock() to determine if this object was successfully able to take
-   * the lock.
-   * @param mutex a shared pointer to a Mutex.
-   */
-  explicit ScopedMutexTryLock(Mutex &mutex) : mutex_(mutex), has_lock_(false) { has_lock_ = mutex_.tryLock(); }
-  /**
-   * Unlock the mutex (if we hold the lock)
-   */
-  ~ScopedMutexTryLock()
-  {
-    if (has_lock_) {
-      mutex_.unlock();
-    }
-  }
-
-  /**
-   * @return True if the lock was taken, False if it was not taken.
-   */
-  bool
-  hasLock()
-  {
-    return has_lock_;
-  }
-
-private:
-  Mutex &mutex_;
-  bool has_lock_;
-};
-
-/**
- * @brief Take a std::shared_ptr to a Mutex and try to lock inside a scope and unlock when the scope is exited (if the lock was
- * taken).
- *
- * This is an RAII implementation which will lock a mutex at the start of the
- * scope and unlock it when the scope is exited if the lock was taken.
- *
- * @see Mutex
- */
-class ScopedSharedMutexTryLock : noncopyable
-{
-public:
-  /**
-   * Try to create the scoped mutex lock, if you should check hasLock() to determine if this object was successfully able to take
-   * the lock.
-   * @param mutex a shared pointer to a Mutex.
-   */
-  explicit ScopedSharedMutexTryLock(std::shared_ptr<Mutex> mutex) : mutex_(mutex), has_lock_(false)
-  {
-    has_lock_ = mutex_->tryLock();
-  }
-  /**
-   * Unlock the mutex (if we hold the lock)
-   */
-  ~ScopedSharedMutexTryLock()
-  {
-    if (has_lock_) {
-      mutex_->unlock();
-    }
-  }
-
-  /**
-   * @return True if the lock was taken, False if it was not taken.
-   */
-  bool
-  hasLock()
-  {
-    return has_lock_;
-  }
-
-private:
-  std::shared_ptr<Mutex> mutex_;
-  bool has_lock_;
-};
-
-/**
- * @brief Lock a TS Continuation by acquiring and releasing its lock in the current scope.
- *
- * This is an RAII implementation which will lock a mutex for the continuation at the declaration of an instance
- * and unlock it when the instance goes out of scope.
- */
-class ScopedContinuationLock : noncopyable
-{
-public:
-  /**
-   * Create the scoped mutex lock, once this object is constructed the lock will be held by the thread.
-   * @param cont The TS continuation.
-   */
-  explicit ScopedContinuationLock(TSCont contp);
-
-  /**
-   * Unlock the mutex.
-   */
-  ~ScopedContinuationLock();
-
-private:
-  TSMutex mutex_;
-};
-
-} /* atscppapi */
-
-#endif /* ATSCPPAPI_MUTEX_H_ */
diff --git a/lib/cppapi/include/atscppapi/TransactionPlugin.h b/lib/cppapi/include/atscppapi/TransactionPlugin.h
index 8455938..5551f50 100644
--- a/lib/cppapi/include/atscppapi/TransactionPlugin.h
+++ b/lib/cppapi/include/atscppapi/TransactionPlugin.h
@@ -21,16 +21,22 @@
  */
 
 #pragma once
-#ifndef ATSCPPAPI_TRANSACTIONPLUGIN_H_
-#define ATSCPPAPI_TRANSACTIONPLUGIN_H_
 
 #include <memory>
+#include <mutex>
+
 #include <atscppapi/Plugin.h>
 #include <atscppapi/Transaction.h>
-#include <atscppapi/Mutex.h>
 
 namespace atscppapi
 {
+#if !defined(ATSCPPAPI_MUTEX_DEFINED_)
+#define ATSCPPAPI_MUTEX_DEFINED_
+
+using Mutex = std::recursive_mutex;
+
+#endif
+
 namespace utils
 {
   class internal;
@@ -113,6 +119,4 @@ private:
   friend class utils::internal;
 };
 
-} /* atscppapi */
-
-#endif /* ATSCPPAPI_TRANSACTIONPLUGIN_H_ */
+} // end namespace atscppapi
diff --git a/lib/cppapi/utils_internal.cc b/lib/cppapi/utils_internal.cc
index c0165c5..aaae1e3 100644
--- a/lib/cppapi/utils_internal.cc
+++ b/lib/cppapi/utils_internal.cc
@@ -26,6 +26,7 @@
 #include <cstdlib>
 #include <cassert>
 #include <cstddef>
+#include <mutex>
 #include "atscppapi/Plugin.h"
 #include "atscppapi/GlobalPlugin.h"
 #include "atscppapi/Transaction.h"
@@ -218,7 +219,7 @@ utils::internal::convertInternalTransformationTypeToTsHook(TransformationPlugin:
 void
 utils::internal::invokePluginForEvent(TransactionPlugin *plugin, TSHttpTxn ats_txn_handle, TSEvent event)
 {
-  ScopedSharedMutexLock scopedLock(plugin->getMutex());
+  std::lock_guard<Mutex> scopedLock(*(plugin->getMutex()));
   ::invokePluginForEvent(static_cast<Plugin *>(plugin), ats_txn_handle, event);
 }
 

-- 
To stop receiving notification emails like this one, please contact
bcall@apache.org.