[asterisk-scf-commits] asterisk-scf/release/ice-util-cpp.git branch "master" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Fri Sep 30 13:24:52 CDT 2011


branch "master" has been updated
       via  a69578535af992b0e3a082832c3f90a934c7bc58 (commit)
      from  a92c362e79a13611485a666b656e8a3ea3b60500 (commit)

Summary of changes:
 include/AsteriskSCF/PJLib/ThreadHook.h |  150 ++++++++++++++++++++++++++++++++
 src/CMakeLists.txt                     |    9 ++
 src/PJLib/CMakeLists.txt               |    1 +
 src/PJLib/ThreadHook.cpp               |   89 +++++++++++++++++++
 4 files changed, 249 insertions(+), 0 deletions(-)
 create mode 100644 include/AsteriskSCF/PJLib/ThreadHook.h
 create mode 100644 src/PJLib/CMakeLists.txt
 create mode 100644 src/PJLib/ThreadHook.cpp


- Log -----------------------------------------------------------------
commit a69578535af992b0e3a082832c3f90a934c7bc58
Author: Kevin P. Fleming <kpfleming at digium.com>
Date:   Fri Sep 30 13:24:01 2011 -0500

    Add a common implementation of a thread hook used for tracking threads in a
    component that also uses PJLIB.
    
    * Adds a new library, astscf-ice-util-cpp-pjlib.
    
    Review: https://code.asterisk.org/code/cru/CR-ASTSCF-168

diff --git a/include/AsteriskSCF/PJLib/ThreadHook.h b/include/AsteriskSCF/PJLib/ThreadHook.h
new file mode 100644
index 0000000..dbae437
--- /dev/null
+++ b/include/AsteriskSCF/PJLib/ThreadHook.h
@@ -0,0 +1,150 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#pragma once
+
+#include <exception>
+#include <map>
+#include <string>
+
+#include <Ice/Ice.h>
+
+#include <pjlib.h>
+
+#include <boost/shared_ptr.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/locks.hpp>
+
+namespace AsteriskSCF
+{
+
+namespace PJLib
+{
+
+class ASTSCF_DLL_EXPORT PJLibInitializationFailed : public std::exception
+{
+public:
+    PJLibInitializationFailed(pj_status_t res) : result(res)
+    {
+    }
+
+    virtual const char* what() throw()
+    {
+        return "pj_init() returned failure.";
+    }
+
+    const pj_status_t result;
+};
+
+class ASTSCF_DLL_EXPORT PJLibUtilInitializationFailed : public std::exception
+{
+public:
+    PJLibUtilInitializationFailed(pj_status_t res) : result(res)
+    {
+    }
+
+    virtual const char* what() throw()
+    {
+        return "pjlib_util_init() returned failure.";
+    }
+
+    const pj_status_t result;
+};
+
+class ASTSCF_DLL_EXPORT ThreadRegistrationFailed : public std::exception
+{
+public:
+    ThreadRegistrationFailed(pj_status_t res) : result(res)
+    {
+    }
+
+    virtual const char* what() throw()
+    {
+        return "pj_thread_register() returned failure.";
+    }
+
+    const pj_status_t result;
+};
+
+class ASTSCF_DLL_EXPORT ThreadHook : public virtual Ice::ThreadNotification
+{
+public:
+    ThreadHook(const std::string&);
+
+    /**
+     * Implementation of the start function which is called when a thread is being started.
+     */
+    void start();
+
+    /**
+     * Implementation of the stop function which is called when a thread is being stopped.
+     */
+    void stop();
+
+    /**
+     * Wrapper class around pj_thread_desc. This is necessary so that we can ensure that
+     * the pj_thread_desc object is initialized properly, and also so that we can wrap
+     * a boost::shared_ptr<> around it to manage its lifetime.
+     */
+    class ThreadDescWrapper
+    {
+    public:
+        ThreadDescWrapper()
+        {
+            memset(mDesc, 0, sizeof(mDesc));
+        }
+
+	/**
+	 * pjthread thread description information, must persist for the life of the thread
+	 */
+	pj_thread_desc mDesc;
+    };
+
+    /**
+     * Type definition used to create a smart pointer for the above.
+     */
+    typedef boost::shared_ptr<ThreadDescWrapper> ThreadDescWrapperPtr;
+
+private:
+    /**
+     * A map containing thread lifetime persistent data.
+     */
+    std::map<pj_thread_t*, ThreadDescWrapperPtr> mpjThreads;
+
+    /**
+     * Mutex to protect the map
+     */
+    boost::mutex mMapLock;
+
+    /**
+     * A flag to indicate whether the PJ libraries have already been initialized by an instance
+     * of this class.
+     */
+    static bool mpjInitialized;
+
+    /**
+     * Mutex to protect the flag
+     */
+    static boost::mutex mInitLock;
+
+    /**
+     * A string indicating the group that threads being tracked by this hook belong to.
+     */
+    const std::string& mThreadGroup;
+};
+
+} // end namespace PJLib
+} // end namespace AsteriskSCF
+
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ae964db..30d5c21 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -14,3 +14,12 @@ astscf_component_add_slice_collection_libraries(astscf-ice-util-cpp ASTSCF)
 astscf_component_build_library(astscf-ice-util-cpp)
 target_link_libraries(astscf-ice-util-cpp logging-client)
 astscf_component_install(astscf-ice-util-cpp)
+
+astscf_component_init(astscf-ice-util-cpp-pjlib)
+add_subdirectory(PJLib)
+astscf_component_add_boost_libraries(astscf-ice-util-cpp core)
+astscf_component_build_library(astscf-ice-util-cpp-pjlib)
+target_link_libraries(astscf-ice-util-cpp logging-client)
+astscf_component_install(astscf-ice-util-cpp-pjlib)
+pjproject_link(astscf-ice-util-cpp-pjlib pjlib)
+pjproject_link(astscf-ice-util-cpp-pjlib pjlib-util)
diff --git a/src/PJLib/CMakeLists.txt b/src/PJLib/CMakeLists.txt
new file mode 100644
index 0000000..be5232e
--- /dev/null
+++ b/src/PJLib/CMakeLists.txt
@@ -0,0 +1 @@
+astscf_component_add_files(astscf-ice-util-cpp-pjlib ThreadHook.cpp)
diff --git a/src/PJLib/ThreadHook.cpp b/src/PJLib/ThreadHook.cpp
new file mode 100644
index 0000000..93a7bab
--- /dev/null
+++ b/src/PJLib/ThreadHook.cpp
@@ -0,0 +1,89 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+
+#include <AsteriskSCF/PJLib/ThreadHook.h>
+
+#include <pjlib-util.h>
+
+namespace AsteriskSCF
+{
+
+namespace PJLib
+{
+
+bool ThreadHook::mpjInitialized;
+boost::mutex ThreadHook::mInitLock;
+
+/**
+ * Constructor which initializes the PJSIP libraries.
+ */
+ThreadHook::ThreadHook(const std::string& threadGroup) : mThreadGroup(threadGroup)
+{
+    boost::lock_guard<boost::mutex> lock(mInitLock);
+
+    if(!mpjInitialized)
+    {
+	pj_status_t status = pj_init();
+	if(status != PJ_SUCCESS)
+	{
+	    throw PJLibInitializationFailed(status);
+	}
+
+	status = pjlib_util_init();
+	if(status != PJ_SUCCESS)
+	{
+	    throw PJLibUtilInitializationFailed(status);
+	}
+
+	mpjInitialized = true;
+    }
+}
+
+/**
+ * Implementation of the start function which is called when a thread is started.
+ */
+void ThreadHook::start()
+{
+    ThreadDescWrapperPtr wrapper(new ThreadDescWrapper());
+    pj_thread_t *thread;
+    std::string threadDescription = mThreadGroup + " Thread";
+
+    pj_status_t status = pj_thread_register(threadDescription.c_str(), wrapper->mDesc, &thread);
+    if (status != PJ_SUCCESS)
+    {
+        throw ThreadRegistrationFailed(status);
+    }
+    else
+    {
+        boost::lock_guard<boost::mutex> lock(mMapLock);
+        mpjThreads.insert(std::make_pair(thread, wrapper));
+    }
+}
+
+/**
+ * Implementation of the stop function which is called when a thread is being stopped.
+ */
+void ThreadHook::stop()
+{
+    if (pj_thread_is_registered())
+    {
+        boost::lock_guard<boost::mutex> lock(mMapLock);
+        mpjThreads.erase(pj_thread_this());
+    }
+}
+
+} // end namespace PJLib
+} // end namespace AsteriskSCF

-----------------------------------------------------------------------


-- 
asterisk-scf/release/ice-util-cpp.git



More information about the asterisk-scf-commits mailing list