1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "CallbackHandler.h"
#include "AddonUtils.h"
#include "threads/SingleLock.h"
#include <vector>
#include "commons/Exception.h"
namespace XBMCAddon
{
class AsynchCallbackMessage : public AddonClass
{
public:
AddonClass::Ref<Callback> cb;
AddonClass::Ref<RetardedAsynchCallbackHandler> handler;
AsynchCallbackMessage(Callback* _cb, RetardedAsynchCallbackHandler* _handler) :
cb(_cb), handler(_handler) { XBMC_TRACE; }
};
//********************************************************************
// This holds the callback messages which will be executed. It doesn't
// seem to work correctly with the Ref object so we'll go with Ref*'s
typedef std::vector<AddonClass::Ref<AsynchCallbackMessage> > CallbackQueue;
//********************************************************************
static CCriticalSection critSection;
static CallbackQueue g_callQueue;
void RetardedAsynchCallbackHandler::invokeCallback(Callback* cb)
{
XBMC_TRACE;
CSingleLock lock(critSection);
g_callQueue.push_back(new AsynchCallbackMessage(cb,this));
}
RetardedAsynchCallbackHandler::~RetardedAsynchCallbackHandler()
{
XBMC_TRACE;
CSingleLock lock(critSection);
// find any messages that might be there because of me ... and remove them
CallbackQueue::iterator iter = g_callQueue.begin();
while (iter != g_callQueue.end())
{
AddonClass::Ref<AsynchCallbackMessage> cur(*iter);
{
if (cur->handler.get() == this) // then this message is because of me
{
g_callQueue.erase(iter);
iter = g_callQueue.begin();
}
else
++iter;
}
}
}
void RetardedAsynchCallbackHandler::makePendingCalls()
{
XBMC_TRACE;
CSingleLock lock(critSection);
CallbackQueue::iterator iter = g_callQueue.begin();
while (iter != g_callQueue.end())
{
AddonClass::Ref<AsynchCallbackMessage> p(*iter);
// only call when we are in the right thread state
if(p->handler->isStateOk(p->cb->getObject()))
{
// remove it from the queue. No matter what we're done with
// this. Even if it doesn't execute for some reason.
g_callQueue.erase(iter);
// we need to release the critSection lock prior to grabbing the
// lock on the object. Not doing so results in deadlocks. We no
// longer are accessing the g_callQueue so it's fine to do this now
{
XBMCAddonUtils::InvertSingleLockGuard unlock(lock);
// make sure the object is not deallocating
// we need to grab the object lock to see if the object of the call
// is deallocating. holding this lock should prevent it from
// deallocating durring the execution of this call.
#ifdef ENABLE_XBMC_TRACE_API
CLog::Log(LOGDEBUG,"%sNEWADDON executing callback 0x%lx",_tg.getSpaces(),(long)(p->cb.get()));
#endif
AddonClass* obj = (p->cb->getObject());
AddonClass::Ref<AddonClass> ref(obj);
CSingleLock lock2(*obj);
if (!p->cb->getObject()->isDeallocating())
{
try
{
// need to make the call
p->cb->executeCallback();
}
catch (XbmcCommons::Exception& e) { e.LogThrowMessage(); }
catch (...)
{
CLog::Log(LOGERROR,"Unknown exception while executeing callback 0x%lx", (long)(p->cb.get()));
}
}
}
// since the state of the iterator may have been corrupted by
// the changing state of the list from another thread durring
// the releasing fo the lock in the immediately preceeding
// codeblock, we need to reset it before continuing the loop
iter = g_callQueue.begin();
}
else // if we're not in the right thread for this callback...
++iter;
}
}
void RetardedAsynchCallbackHandler::clearPendingCalls(void* userData)
{
XBMC_TRACE;
CSingleLock lock(critSection);
CallbackQueue::iterator iter = g_callQueue.begin();
while (iter != g_callQueue.end())
{
AddonClass::Ref<AsynchCallbackMessage> p(*iter);
if(p->handler->shouldRemoveCallback(p->cb->getObject(),userData))
{
#ifdef ENABLE_XBMC_TRACE_API
CLog::Log(LOGDEBUG,"%sNEWADDON removing callback 0x%lx for PyThreadState 0x%lx from queue", _tg.getSpaces(),(long)(p->cb.get()) ,(long)userData);
#endif
iter = g_callQueue.erase(iter);
}
else
++iter;
}
}
}
|