[hydra-commits] ken.hunt: branch techdemo/ken.hunt/embeddedLuaTest r629 - in /techdemo/team/k...

SVN commits to the Hydra project hydra-commits at lists.digium.com
Thu May 20 10:12:21 CDT 2010


Author: ken.hunt
Date: Thu May 20 10:12:20 2010
New Revision: 629

URL: https://origsvn.digium.com/svn-view/hydra?view=rev&rev=629
Log:
lua thread testing

Added:
    techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/bin/
    techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/threadtest/
    techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp   (with props)
    techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h   (with props)
Modified:
    techdemo/team/ken.hunt/embeddedLuaTest/source/CMakeLists.txt
    techdemo/team/ken.hunt/embeddedLuaTest/source/Server.cpp

Added: techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt Thu May 20 10:12:20 2010
@@ -1,0 +1,9 @@
+cmake_minimum_required(VERSION 2.6)
+
+include(cmake/Hydra_v3.cmake)
+
+hydra_project(embedLuaTest 3.4 CXX CSharp)
+
+add_subdirectory(slice)
+add_subdirectory(source)
+add_subdirectory(threadtest)

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/CMakeLists.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat Thu May 20 10:12:20 2010
@@ -1,0 +1,15 @@
+
+echo off
+copy /Y /B ..\build\source\Release\Routing.exe .
+
+copy /Y /B ..\build\slice\Release\RoutingServiceIf_CXX.dll
+copy /Y /B ..\build\slice\Channel\Release\ChannelServiceIf_CXX.dll
+copy /Y /B ..\build\slice\Channel\Release\EndpointIf_CXX.dll
+copy /Y /B ..\build\slice\Channel\Release\SignalIf_CXX.dll
+copy /Y /B ..\build\slice\Media\Release\MediaSinkIf_CXX.dll
+copy /Y /B ..\build\slice\Media\Release\MediaTranslationIf_CXX.dll
+
+
+copy /Y /B ..\build\threadtest\Release\threadtest.exe
+
+

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/bin/install_release.bat
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua Thu May 20 10:12:20 2010
@@ -1,0 +1,5 @@
+
+function lookup(ext)
+    return ext+1
+end
+

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/bin/lookup.lua
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: techdemo/team/ken.hunt/embeddedLuaTest/source/CMakeLists.txt
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/source/CMakeLists.txt?view=diff&rev=629&r1=628&r2=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/source/CMakeLists.txt (original)
+++ techdemo/team/ken.hunt/embeddedLuaTest/source/CMakeLists.txt Thu May 20 10:12:20 2010
@@ -13,4 +13,5 @@
 hydra_component_add_ice_libraries(Routing IceStorm)
 hydra_component_add_boost_libraries(Routing core)
 hydra_component_build_standalone(Routing)
+target_link_libraries(Routing ${LUA_LIBRARIES})
 hydra_component_install(Routing)

Modified: techdemo/team/ken.hunt/embeddedLuaTest/source/Server.cpp
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/source/Server.cpp?view=diff&rev=629&r1=628&r2=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/source/Server.cpp (original)
+++ techdemo/team/ken.hunt/embeddedLuaTest/source/Server.cpp Thu May 20 10:12:20 2010
@@ -2,9 +2,8 @@
 #include <boost/cast.hpp>
 #include <boost/shared_ptr.hpp>
 #include <typeinfo>
-#include "lua.h"
-#include "lualib.h"
-#include "lauxlib.h"
+#include <stdio.h>
+#include "lua.hpp"
 
 #include "RoutingServiceIf.h"
 
@@ -23,56 +22,44 @@
 {
 	cout << "Looking up: " <<  destination << std::endl;
 
-   PyObject *pName, *pModule;
+   try
+   {
+   lua_State* L = lua_open();
 
-   // TEMP...Changed to a simple python script. Make
-   // sure my pythonScripts folder is in  PYTHONPATH
-   pName = PyString_FromString("lookup");
-   pModule = PyImport_Import(pName);
-   Py_DECREF(pName);
+   luaL_openlibs(L);
 
-   if (pModule != 0)
+   // Load script
+   if (luaL_dofile(L, "lookup.lua"))
    {
-      PyObject *pFunc = PyObject_GetAttrString(pModule, "lookup");
-      if (pFunc != 0 && PyCallable_Check(pFunc))
-      {
-         PyObject *pArgs = PyTuple_New(1);
-         PyObject *pValue = PyInt_FromLong(atoi(destination.c_str()));
-         if (!pValue)
-         {
-            Py_DECREF(pArgs);
-            Py_DECREF(pFunc);
-            Py_DECREF(pModule);
-            return 0;
-         }
-         PyTuple_SetItem(pArgs, 0, pValue); // pValue ref given up here.
+      cout << lua_tostring(L, -1) << std::endl;
+      return 0;
+   }
 
-         pValue = PyObject_CallObject(pFunc, pArgs);
-         Py_DECREF(pArgs);
+   //lua_getfield(L, LUA_GLOBALSINDEX, "LUA_PATH");
+   //size_t len;
+   //const char *s = lua_tolstring(L, -1, &len);
 
-         if (pValue != 0)
-         {
-            cout << "Found: " <<  PyInt_AsLong(pValue) << std::endl;
-            Py_DECREF(pValue);
-         }
-         else
-         {
-            PyErr_Print();
-         }
-       
-         Py_DECREF(pFunc);
-         Py_DECREF(pModule);
-      }
-      else if (PyErr_Occurred())
-      {
-         PyErr_Print();
-      }
+   // Set function name.
+	lua_getglobal(L, "lookup");
+
+   // Push input arg
+	lua_pushnumber(L, atoi(destination.c_str() ));
+
+   // Call function. One arg, one result...
+   lua_call(L, 1, 1);
+
+   int val = (int)lua_tonumber(L, -1);
+	lua_pop(L, 1);
+
+   cout << "Results in: " <<  val << std::endl;
+ 
+   lua_close(L);
    }
-   else
+   catch (std::exception &e)
    {
-      PyErr_Print();
+      cout << e.what() << endl;
    }
-   Py_Finalize();
+
    return 0;
 }
 
@@ -81,8 +68,6 @@
 {
     int status = 0;
     Ice::CommunicatorPtr ic;
-
-    Py_Initialize();
 
     try 
     {

Added: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt Thu May 20 10:12:20 2010
@@ -1,0 +1,12 @@
+
+hydra_component_init(threadtest CXX)
+INCLUDE(FindLua51)
+get_filename_component(LUA_LOC ${LUA_LIBRARIES} PATH)
+LINK_DIRECTORIES(${LUA_LOC})
+include_directories(${LUA_INCLUDE_DIR})
+hydra_component_add_file(threadtest  threadtest.cpp)
+hydra_component_add_file(threadtest  workqueue.cpp)
+hydra_component_add_boost_libraries(threadtest core)
+hydra_component_build_standalone(threadtest)
+target_link_libraries(threadtest ${LUA_LIBRARIES})
+hydra_component_install(threadtest)

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/CMakeLists.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h Thu May 20 10:12:20 2010
@@ -1,0 +1,33 @@
+#pragma once
+#ifndef _IWORKQUEUE_H
+#define _IWORKQUEUE_H
+
+#include "boost/shared_ptr.hpp"
+
+namespace hydra
+{
+class IWorkQueue
+{
+public:
+
+   class IWork
+   {
+   public:
+      virtual void DoWork() = 0;
+   };
+ 
+   typedef boost::shared_ptr<IWork> IWorkPtr;
+
+   virtual void Enqueue(IWorkPtr w) = 0;
+   virtual void Terminate() = 0;
+   virtual void Join() = 0;
+
+protected:
+   IWorkQueue() {};  // Hide the constructor for interface
+};
+typedef IWorkQueue::IWorkPtr IWorkPtr;
+
+};
+
+#endif
+

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/iworkqueue.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp Thu May 20 10:12:20 2010
@@ -1,0 +1,131 @@
+#include <iostream>
+#include <string>
+#include <boost/cast.hpp>
+#include <boost/shared_ptr.hpp>
+#include <stdio.h>
+#include "workqueue.h"
+#include "lua.hpp"
+
+using namespace hydra;
+
+/**
+ * OS Portable sleep util function.
+ */
+void sleep(int secs)
+{
+	boost::posix_time::seconds workTime(secs);  
+    boost::this_thread::sleep(workTime); 
+}
+
+class TestWork : public IWorkQueue::IWork
+{
+public:
+    TestWork(std::string id, lua_State* L, int val) : mTestVal(val), mL(L), mId(id) {}
+    ~TestWork() 
+    {
+       std::cout << "TestWork::~TestWork: Deleting " << mId << std::endl;
+    }
+
+    void DoWork() 
+    {   
+         try
+         {
+            std::cout << "Looking up " << mTestVal << std::endl;
+            // Set function name.
+   	      lua_getglobal(mL, "lookup");
+
+            // Push input arg
+	         lua_pushnumber(mL, mTestVal);
+
+            // Call function. One arg, one result...
+            lua_call(mL, 1, 1);
+
+            int val = (int)lua_tonumber(mL, -1);
+	         lua_pop(mL, 1);
+
+            std::cout << "Result=" << val << std::endl;
+         }
+         catch (std::exception &e)
+         {
+            std::cout << e.what() << std::endl;
+         }
+    }
+
+private:
+   int mTestVal;
+   lua_State* mL;
+   std::string mId;
+};
+
+lua_State *InitLuaEnv(std::string scriptname)
+{
+   lua_State *L(lua_open());
+   luaL_openlibs(L);
+   if (luaL_dofile(L, scriptname.c_str()))
+   {
+      std::cout << lua_tostring(L, -1) << std::endl;
+   }
+   return L;
+}
+
+int main(int argc, char* argv[])
+{
+
+   lua_State *L1 = InitLuaEnv("lookup.lua");
+   lua_State *L2 = InitLuaEnv("lookup.lua");
+
+   WorkQueue wqueue1("wq1", true);
+   WorkQueue wqueue2("wq2", true);
+
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w1(new TestWork("WorkOnQueue1", L1, 110));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w2(new TestWork("WorkOnQueue1", L1, 120));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w3(new TestWork("WorkOnQueue1", L1, 130));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w4(new TestWork("WorkOnQueue1", L1, 140));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w5(new TestWork("WorkOnQueue1", L1, 150));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w6(new TestWork("WorkOnQueue1", L1, 160));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w7(new TestWork("WorkOnQueue1", L1, 170));
+   boost::shared_ptr<IWorkQueue::IWork> wq1_w8(new TestWork("WorkOnQueue1", L1, 180));
+
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w1(new TestWork("WorkOnQueue2", L2, 210));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w2(new TestWork("WorkOnQueue2", L2, 220));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w3(new TestWork("WorkOnQueue2", L2, 230));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w4(new TestWork("WorkOnQueue2", L2, 240));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w5(new TestWork("WorkOnQueue2", L2, 250));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w6(new TestWork("WorkOnQueue2", L2, 260));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w7(new TestWork("WorkOnQueue2", L2, 270));
+   boost::shared_ptr<IWorkQueue::IWork> wq2_w8(new TestWork("WorkOnQueue2", L2, 280));
+
+   wqueue1.Enqueue(wq1_w1);
+   wqueue2.Enqueue(wq2_w1);
+
+   wqueue1.Enqueue(wq1_w2);
+   wqueue2.Enqueue(wq2_w2);
+
+   wqueue1.Enqueue(wq1_w3);
+   wqueue2.Enqueue(wq2_w3);
+
+   wqueue1.Enqueue(wq1_w4);
+   wqueue2.Enqueue(wq2_w4);
+
+   wqueue1.Enqueue(wq1_w5);
+   wqueue2.Enqueue(wq2_w5);
+
+   wqueue1.Enqueue(wq1_w6);
+   wqueue2.Enqueue(wq2_w6);
+
+   wqueue1.Enqueue(wq1_w7);
+   wqueue2.Enqueue(wq2_w7);
+
+   wqueue1.Enqueue(wq1_w8);
+   wqueue2.Enqueue(wq2_w8);
+
+   sleep(5);
+
+   wqueue1.Terminate();
+   wqueue2.Terminate();
+   wqueue1.Join();
+   wqueue2.Join();
+
+   lua_close(L1);
+   lua_close(L2);
+}

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/threadtest.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp Thu May 20 10:12:20 2010
@@ -1,0 +1,204 @@
+/**
+ * A simple Work Queue implementation. On construction, starts an internal thread. 
+ * Work can be enqueued via the thread-safe Enqueue method. All work must implement the IWork interface.
+ */
+#include <iostream>
+#include <boost/shared_ptr.hpp>
+#include <boost/bind.hpp>
+
+#include "workqueue.h"
+
+using namespace hydra;
+using namespace boost;
+
+namespace hydra
+{
+class WorkQueuePriv
+{
+public:
+	WorkQueuePriv(std::string id, bool paused) 
+	     : mQid(id),
+		    mInitialized(false), 
+	       mPaused(paused), 
+		    mFinished(false), 
+		    mThread(boost::bind(&WorkQueuePriv::Execute, this))
+  {
+  }
+
+  ~WorkQueuePriv()
+  {
+  }
+
+  IWorkPtr Dequeue();
+  IWorkPtr WaitAndDequeue();
+  void Execute();
+
+  std::string mQid;
+  bool mInitialized;
+  bool mFinished;
+  bool mPaused;
+  std::list<IWorkPtr> mQueue;
+  boost::thread mThread;
+  boost::mutex mQueueMutex;
+  boost::condition mEmptyQueueCondition;
+  boost::mutex mPauseMutex;
+  boost::condition mPauseCondition;
+};
+}
+
+WorkQueue::WorkQueue(std::string qid, bool runByDefault) : mImpl(new WorkQueuePriv(qid, !runByDefault))
+{
+  mImpl->mInitialized = true; 
+}
+
+WorkQueue::~WorkQueue()
+{
+    Terminate();
+
+	// Wait for worker thread to shut down. 
+	mImpl->mThread.join(); // If you don't do this, then the mImpl is trashed and Execute has bad "this" ptr on other thread. 
+}
+
+bool WorkQueue::IsRunning() 
+{ 
+	return (mImpl->mInitialized && !mImpl->mPaused && !mImpl->mFinished);
+}
+
+/**
+ * Pause the WorkQueue's thread.
+ */
+void WorkQueue::Pause()
+{
+   boost::mutex::scoped_lock lock(mImpl->mPauseMutex);
+   mImpl->mPaused = true;
+}
+
+
+/**
+ * Resume from a Paused state. 
+ */
+void WorkQueue::Resume()
+{
+   boost::mutex::scoped_lock lock(mImpl->mPauseMutex);
+   mImpl->mPaused = false;
+   mImpl->mPauseCondition.notify_all();
+}
+
+/** 
+ * Stops this thread from executing. 
+ */
+void WorkQueue::Terminate()
+{
+  mImpl->mFinished = true;
+  mImpl->mPaused = false;
+  mImpl->mPauseCondition.notify_all();      // In case the thread was waiting on the PauseCondition.
+  mImpl->mEmptyQueueCondition.notify_all(); // In case the thread was waiting on an EmptyQueueCondition
+}
+
+/**
+ * A convenience method to determine if there is any pending work on the queue. 
+ */
+bool WorkQueue::WorkPending()
+{
+   return !mImpl->mQueue.empty();
+}
+
+/**
+ * Allows other thread to join to this thread. The caller needs to 
+ * call this object's Terminate method, or the join will block
+ * indefinitely.
+ */
+void WorkQueue::Join()
+{
+   mImpl->mThread.join();
+}
+
+/**
+ * Enqueue an item of work for processing on this queue's thread. 
+ */
+void WorkQueue::Enqueue(IWorkPtr w)
+{
+   boost::mutex::scoped_lock lock(mImpl->mQueueMutex);
+   bool wasEmpty = mImpl->mQueue.empty();
+   mImpl->mQueue.push_back(w);
+   int size = mImpl->mQueue.size();
+   lock.unlock();
+ 
+   if (wasEmpty)
+   { 
+      mImpl->mEmptyQueueCondition.notify_all();
+   }
+}
+
+/**
+ * This is a private no-op implementation of a work item. Returned from WaitAndDequeue 
+ * if the program is Terminated while waiting on the EmptyQueueCondition.  
+ */
+class NO_WORK_CLASS : public IWorkQueue::IWork
+{
+public:
+    NO_WORK_CLASS() {};
+    void DoWork() {}   // Do nothing
+};
+static shared_ptr<IWorkQueue::IWork> NO_WORK_PTR(new NO_WORK_CLASS());
+
+/**
+ * This method returns the next work from the queue. If no work available,
+ * this method waits on the EmptyQueueCondition. 
+ */
+IWorkPtr WorkQueuePriv::WaitAndDequeue()
+{
+    boost::mutex::scoped_lock lock(mQueueMutex);
+
+    int size = mQueue.size(); // debugging
+
+    while (mQueue.empty())
+    {
+		if (mFinished)
+        {
+           return NO_WORK_PTR;
+        }
+
+        mEmptyQueueCondition.wait(lock);
+    }
+    
+    size = mQueue.size(); // debugging
+
+    shared_ptr<IWorkQueue::IWork> work = mQueue.front();
+    mQueue.pop_front();
+
+    return work;
+}
+
+/**
+ * This is the thread's event loop. The thread terminates when this method returns.
+ */
+void WorkQueuePriv::Execute()
+{
+   while (!mInitialized)
+   {
+      // The thread can start before the constructor has finished initializing the object.
+	  // Can lead to strange behavior. 
+      continue;
+   }
+
+   while (!mFinished)
+   {
+      { // scope the lock
+         boost::mutex::scoped_lock lock(mPauseMutex);
+         while(mPaused)
+         {
+            mPauseCondition.wait(lock);
+         }
+
+         if (mFinished) // In case Terminate was called while in PauseCondition
+            break;
+      }
+
+      shared_ptr<IWorkQueue::IWork> work  = WaitAndDequeue();
+
+      work->DoWork();
+   }
+
+}
+

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h
URL: https://origsvn.digium.com/svn-view/hydra/techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h?view=auto&rev=629
==============================================================================
--- techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h (added)
+++ techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h Thu May 20 10:12:20 2010
@@ -1,0 +1,40 @@
+#pragma once
+#ifndef _WORKQUEUE_H
+#define _WORKQUEUE_H
+
+#include <list>
+#include <boost/thread.hpp>
+#include <boost/thread/condition.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "iworkqueue.h"
+
+namespace hydra
+{
+
+class WorkQueuePriv;
+
+class WorkQueue : public IWorkQueue,  boost::noncopyable
+{
+public:
+  WorkQueue(std::string id="<unnamed>", bool runByDefault=false);
+  ~WorkQueue();
+
+  virtual void Enqueue(IWorkPtr w);
+  virtual void Terminate();
+  virtual void Join();
+
+  // This implementation adds the concept of Pausing to the generic IWorkQueue. 
+  bool IsRunning();
+  bool WorkPending();
+  void Pause();
+  void Resume();
+
+private:
+  boost::shared_ptr<WorkQueuePriv> mImpl;
+};
+
+};
+
+#endif
+

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: techdemo/team/ken.hunt/embeddedLuaTest/threadtest/workqueue.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the asterisk-scf-commits mailing list