Krotos Modules 3
Loading...
Searching...
No Matches
ResourceLock.cpp
Go to the documentation of this file.
1namespace krotos
2{
4
5 bool ResourceLock::requestAccess(const StringRef id)
6 {
7 const ScopedLock myScopedLock(objectLock);
9 {
10#ifdef USE_DEBUG_LOGGING
11 auto added = m_clientList.addIfNotAlreadyThere(id);
12 if (added)
13 {
14 Logger::getCurrentLogger()->writeToLog("Granted " + id +
15 " - Users: " + String(m_clientList.size()));
16 }
17 else
18 {
19 Logger::getCurrentLogger()->writeToLog("Granted Again " + id +
20 " - Users: " + String(m_clientList.size()));
21 }
22 }
23 else
24 {
25 Logger::getCurrentLogger()->writeToLog("Not Granted " + id + " - Users: " + String(m_clientList.size()));
26#else
27 m_clientList.addIfNotAlreadyThere(id);
28#endif
29 }
30 return m_isAccessible;
31 }
32
33 bool ResourceLock::hasAccess(const StringRef id) const { return m_clientList.contains(id); }
34
35 void ResourceLock::finishedAccessing(const StringRef id)
36 {
37 const ScopedLock myScopedLock(objectLock);
38
39 auto index = m_clientList.indexOf(id);
40 jassert(index >= 0); // A client which had not been granted access attempted to finishedAccessing()
41
42 m_clientList.removeString(id);
43
44#ifdef USE_DEBUG_LOGGING
45 Logger::getCurrentLogger()->writeToLog("Finished " + id + " - Users: " + String(m_clientList.size()));
46#endif
47
48 if (m_clientList.size() == 0) // If no client processblocks are processing, then ...
49 {
50 m_waitForZeroUsers.signal(); // ... release the requesting thread, in case it is waiting for us to complete
51 }
52 }
53
54 void ResourceLock::setAccessible(bool isAccessible)
55 {
56 // No scoped lock on this method - because we need to be able to wait
57 // here for other threads to fininsh - so we can't lock them out
58
59#ifdef USE_DEBUG_LOGGING
60 if (isAccessible)
61 {
62 Logger::getCurrentLogger()->writeToLog("Set Accesible True - Users: " + String(m_clientList.size()));
63 }
64 else
65 {
66 Logger::getCurrentLogger()->writeToLog("Set Accesible False - Users: " + String(m_clientList.size()));
67 }
68#endif
69
70 if (!m_isAccessible)
71 {
72 jassert(m_clientList.size() == 0); // m_isAccessible should never be false if there are users
73 if (!isAccessible) // If we are trying to set false and already false, then nothing to do
74 return;
75 }
76
77 m_isAccessible = isAccessible; // grant or deny access to our client process blocks
78 if (!isAccessible)
79 {
80 if (m_clientList.size() > 0)
81 {
82 // Wait, with large timeout
83 bool timedOut = !m_waitForZeroUsers.wait(DSP_THREAD_WAIT_MAX_MS);
84 jassert(timedOut == false); // Timed out waiting for resource to be released
85 }
86 }
87 }
88
89} // namespace krotos
bool requestAccess(const StringRef id)
Called by a client to request access to the resource.
Definition ResourceLock.cpp:5
volatile std::atomic< bool > m_isAccessible
Definition ResourceLock.h:58
void setAccessible(bool isAccessible)
Called by the resource to grant or deny access to clients.
Definition ResourceLock.cpp:54
static const int DSP_THREAD_WAIT_MAX_MS
Definition ResourceLock.h:55
StringArray m_clientList
Definition ResourceLock.h:57
CriticalSection objectLock
Definition ResourceLock.h:54
WaitableEvent m_waitForZeroUsers
Definition ResourceLock.h:59
void finishedAccessing(const StringRef id)
Called by a client to inform the resource that it no longer needs access.
Definition ResourceLock.cpp:35
bool hasAccess(const StringRef id) const
Called by a client to enquire whether it has access to the resource.
Definition ResourceLock.cpp:33
Definition AirAbsorptionFilter.cpp:2