Skip to content

Commit

Permalink
added stopping timer so that CanonControlLuaBindings object can be di…
Browse files Browse the repository at this point in the history
…sposed of
  • Loading branch information
vividos committed Mar 5, 2016
1 parent 02b8817 commit 4f7543c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/LuaScripting/CameraScriptProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class CameraScriptProcessor::Impl : public std::enable_shared_from_this<CameraSc
/// cleans up bindings
void CleanupBindings()
{
m_spCanonControlLuaBindings->StopTimer();

m_scriptWorkerThread.GetStrand().post([&]()
{
// set these to nil, to allow garbage collection
Expand Down
30 changes: 27 additions & 3 deletions src/LuaScripting/CanonControlLuaBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Viewfinder.hpp"
#include "BulbReleaseControl.hpp"
#include "Asio.hpp"
#include <atomic>

/// name for Lua value in App object to store handler for asyncWaitForCamera()
LPCTSTR c_pszAsyncWaitForCamera_OnConnectedHandler = _T("__AsyncWaitForCamera_OnConnectedHandler");
Expand All @@ -31,7 +32,8 @@ const unsigned int c_uiEventTimerCycleInMilliseconds = 100;
CanonControlLuaBindings::CanonControlLuaBindings(Lua::State& state, boost::asio::io_service::strand& strand)
:m_state(state),
m_strand(strand),
m_timerEventHandling(m_strand.get_io_service())
m_timerEventHandling(m_strand.get_io_service()),
m_evtTimerStopped(true, false) // manual-reset event
{
}

Expand Down Expand Up @@ -147,6 +149,8 @@ void CanonControlLuaBindings::InitRemoteReleaseControlConstants(Lua::Table& cons

void CanonControlLuaBindings::RestartEventTimer()
{
m_evtTimerStopped.Reset();

m_timerEventHandling.expires_from_now(boost::posix_time::milliseconds(c_uiEventTimerCycleInMilliseconds));
m_timerEventHandling.async_wait(
m_strand.wrap(
Expand All @@ -156,7 +160,11 @@ void CanonControlLuaBindings::RestartEventTimer()
void CanonControlLuaBindings::OnTimerEventHandling(const boost::system::error_code& error)
{
if (error)
return; // timer was canceled
{
// timer was canceled
m_evtTimerStopped.Set();
return;
}

Instance::OnIdle();

Expand All @@ -177,6 +185,9 @@ void CanonControlLuaBindings::CancelHandlers()
// TODO
//m_spRemoteRelaseControl->RemoveDownloadEventHandler();
m_spRemoteRelaseControl.reset();

m_releaseSettings.HandlerOnFinishedTransfer(ShutterReleaseSettings::T_fnOnFinishedTransfer());
m_spRemoteRelaseControl->SetReleaseSettings(m_releaseSettings);
}

if (m_upInstance != nullptr)
Expand All @@ -199,10 +210,23 @@ void CanonControlLuaBindings::CancelHandlers()
GetState().CollectGarbage();
}

void CanonControlLuaBindings::CleanupBindings()
void CanonControlLuaBindings::StopTimer()
{
m_timerEventHandling.cancel();

std::atomic<bool> finished = false;

do
{
m_strand.post([&] {
finished = m_evtTimerStopped.Wait(0);
});
Sleep(50);
} while (!finished);
}

void CanonControlLuaBindings::CleanupBindings()
{
m_upInstance.reset();

GetState().AddValue(_T("Constants"), Lua::Value());
Expand Down
7 changes: 7 additions & 0 deletions src/LuaScripting/CanonControlLuaBindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Lua.hpp"
#include "Instance.hpp"
#include "RecursiveMutex.hpp"
#include "Event.hpp"
#include "Asio.hpp"
#include "ShutterReleaseSettings.hpp"

Expand Down Expand Up @@ -51,6 +52,9 @@ class CanonControlLuaBindings : public std::enable_shared_from_this<CanonControl
/// cancels all handlers of async operations
void CancelHandlers();

/// stops internal timer
void StopTimer();

private:
/// returns Lua state object
Lua::State& GetState() throw() { return m_state; }
Expand Down Expand Up @@ -247,4 +251,7 @@ class CanonControlLuaBindings : public std::enable_shared_from_this<CanonControl

/// timer for event handling
boost::asio::deadline_timer m_timerEventHandling;

/// event that is set when the event handling timer has stopped
Event m_evtTimerStopped;
};

0 comments on commit 4f7543c

Please sign in to comment.