[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "pjlib-thread-hook" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Wed Sep 28 13:51:10 CDT 2011


branch "pjlib-thread-hook" has been updated
       via  139ce5a78d1e73772e8b54ca4b73f619a64ee247 (commit)
      from  b09018e286d33f717e2c3656f5fd4e9dd1884a21 (commit)

Summary of changes:
 include/AsteriskSCF/PJLib/ThreadHook.h |   65 ++++++++++++++++++++++++++------
 src/CMakeLists.txt                     |    1 +
 src/PJLib/ThreadHook.cpp               |   37 ++++++++++++++++-
 3 files changed, 88 insertions(+), 15 deletions(-)


- Log -----------------------------------------------------------------
commit 139ce5a78d1e73772e8b54ca4b73f619a64ee247
Author: Kevin P. Fleming <kpfleming at digium.com>
Date:   Wed Sep 28 13:43:47 2011 -0500

    Various improvements:
    
    * Add pjlib-util initialization.
    * Ensure that only one instance of ThreadHook will attempt to call pj_init()
      and pjlib_util_init().
    * Add a 'group' parameter to the ThreadHook constructor, so that instances of
      ThreadHook that are not being used to trace Ice threads can properly describe
      the threads they are tracking.
    * Add exceptions that can be thrown by ThreadHook's contructor if pj_init()
      or pjlib_util_init() fail.

diff --git a/include/AsteriskSCF/PJLib/ThreadHook.h b/include/AsteriskSCF/PJLib/ThreadHook.h
index b3e79b6..6300fd7 100644
--- a/include/AsteriskSCF/PJLib/ThreadHook.h
+++ b/include/AsteriskSCF/PJLib/ThreadHook.h
@@ -17,6 +17,7 @@
 
 #include <exception>
 #include <map>
+#include <string>
 
 #include <Ice/Ice.h>
 
@@ -32,6 +33,36 @@ 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:
@@ -44,19 +75,16 @@ public:
         return "pj_thread_register() returned failure.";
     }
 
-    pj_status_t result;
+    const pj_status_t result;
 };
 
 class ASTSCF_DLL_EXPORT ThreadHook : public virtual Ice::ThreadNotification
 {
 public:
-    ThreadHook()
-    {
-            pj_init();
-    }
+    ThreadHook(const std::string&);
 
     /**
-     * Implementation of the start function which is called when a thread is starting.
+     * Implementation of the start function which is called when a thread is being started.
      */
     void start();
 
@@ -66,7 +94,9 @@ public:
     void stop();
 
     /**
-     * Wrapper class around pj_thread_desc.
+     * 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
     {
@@ -76,10 +106,10 @@ public:
             memset(mDesc, 0, sizeof(mDesc));
         }
 
-    /**
-     * pjthread thread description information, must persist for the life of the thread
-     */
-    pj_thread_desc mDesc;
+	/**
+	 * pjthread thread description information, must persist for the life of the thread
+	 */
+	pj_thread_desc mDesc;
     };
 
     /**
@@ -94,9 +124,20 @@ private:
     std::map<pj_thread_t*, ThreadDescWrapperPtr> mpjThreads;
 
     /**
-     * Mutex to protect the map
+     * 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 map and the flag
      */
     boost::mutex mLock;
+
+    /**
+     * A string indicating the group that threads being tracked by this hook belong to.
+     */
+    const std::string& mThreadGroup;
 };
 
 } // end namespace PJLib
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b2712ce..30d5c21 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -22,3 +22,4 @@ 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/ThreadHook.cpp b/src/PJLib/ThreadHook.cpp
index b09fb46..8d8443b 100644
--- a/src/PJLib/ThreadHook.cpp
+++ b/src/PJLib/ThreadHook.cpp
@@ -16,12 +16,41 @@
 
 #include <AsteriskSCF/PJLib/ThreadHook.h>
 
+#include <pjlib-util.h>
+
 namespace AsteriskSCF
 {
 
 namespace PJLib
 {
 
+bool ThreadHook::mpjInitialized;
+
+/**
+ * Constructor which initializes the PJSIP libraries.
+ */
+ThreadHook::ThreadHook(const std::string& threadGroup) : mThreadGroup(threadGroup)
+{
+    boost::lock_guard<boost::mutex> lock(mLock);
+
+    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.
  */
@@ -29,10 +58,12 @@ void ThreadHook::start()
 {
     ThreadDescWrapperPtr wrapper(new ThreadDescWrapper());
     pj_thread_t *thread;
-    pj_status_t win = pj_thread_register("ICE Thread", wrapper->mDesc, &thread);
-    if (win != PJ_SUCCESS)
+    std::string threadDescription = mThreadGroup + " Thread";
+
+    pj_status_t status = pj_thread_register(threadDescription.c_str(), wrapper->mDesc, &thread);
+    if (status != PJ_SUCCESS)
     {
-        throw ThreadRegistrationFailed(win);
+        throw ThreadRegistrationFailed(status);
     }
     else
     {

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


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



More information about the asterisk-scf-commits mailing list