Skip to content

Commit 201c0fc

Browse files
committed
Update DelegateMQ library
1 parent 08b8ed8 commit 201c0fc

File tree

30 files changed

+147
-52
lines changed

30 files changed

+147
-52
lines changed

DelegateMQ/Predef.cmake

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ elseif (DMQ_THREAD STREQUAL "DMQ_THREAD_ZEPHYR")
3232
)
3333
elseif (DMQ_THREAD STREQUAL "DMQ_THREAD_CMSIS_RTOS2")
3434
add_compile_definitions(DMQ_THREAD_CMSIS_RTOS2)
35-
file(GLOB THREAD_SOURCES
36-
"${DMQ_ROOT_DIR}/predef/os/cmsis-rtos2/*.c*"
37-
"${DMQ_ROOT_DIR}/predef/os/cmsis-rtos2/*.h"
35+
file(GLOB THREAD_SOURCES
36+
"${DMQ_ROOT_DIR}/predef/os/cmsis-rtos2/*.c*"
37+
"${DMQ_ROOT_DIR}/predef/os/cmsis-rtos2/*.h"
38+
)
39+
elseif (DMQ_THREAD STREQUAL "DMQ_THREAD_QT")
40+
add_compile_definitions(DMQ_THREAD_QT)
41+
file(GLOB THREAD_SOURCES
42+
"${DMQ_ROOT_DIR}/predef/os/qt/*.c*"
43+
"${DMQ_ROOT_DIR}/predef/os/qt/*.h"
3844
)
3945
elseif (DMQ_THREAD STREQUAL "DMQ_THREAD_NONE")
4046
add_compile_definitions(DMQ_THREAD_NONE)
@@ -100,7 +106,7 @@ elseif (DMQ_TRANSPORT STREQUAL "DMQ_TRANSPORT_ARM_LWIP_NETCONN_UDP")
100106
file(GLOB TRANSPORT_SOURCES "${DMQ_ROOT_DIR}/predef/transport/arm-lwip-netconn-udp/*.h")
101107
elseif (DMQ_TRANSPORT STREQUAL "DMQ_TRANSPORT_THREADX_UDP")
102108
add_compile_definitions(DMQ_TRANSPORT_THREADX_UDP)
103-
file(GLOB TRANSPORT_SOURCES "${DMQ_ROOT_DIR}/predef/transport/threadx-udp/*.h")
109+
file(GLOB TRANSPORT_SOURCES "${DMQ_ROOT_DIR}/predef/transport/netx-udp/*.h")
104110
elseif (DMQ_TRANSPORT STREQUAL "DMQ_TRANSPORT_STM32_UART")
105111
add_compile_definitions(DMQ_TRANSPORT_STM32_UART)
106112
file(GLOB TRANSPORT_SOURCES "${DMQ_ROOT_DIR}/predef/transport/stm32-uart/*.h")

DelegateMQ/delegate/DelegateAsync.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,7 @@ class DelegateFreeAsync<RetType(Args...)> : public DelegateFree<RetType(Args...)
272272
BAD_ALLOC();
273273

274274
// Create a new message instance for sending to the destination thread
275-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
276-
std::shared_ptr<DelegateAsyncMsg<Args...>> msg(new DelegateAsyncMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
275+
auto msg = xmake_shared<DelegateAsyncMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
277276
if (!msg)
278277
BAD_ALLOC();
279278

@@ -586,8 +585,7 @@ class DelegateMemberAsync<TClass, RetType(Args...)> : public DelegateMember<TCla
586585
BAD_ALLOC();
587586

588587
// Create a new message instance for sending to the destination thread
589-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
590-
std::shared_ptr<DelegateAsyncMsg<Args...>> msg(new DelegateAsyncMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
588+
auto msg = xmake_shared<DelegateAsyncMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
591589
if (!msg)
592590
BAD_ALLOC();
593591

@@ -829,8 +827,7 @@ class DelegateMemberAsyncSp<TClass, RetType(Args...)> : public DelegateMemberSp<
829827
BAD_ALLOC();
830828

831829
// Create a new message instance for sending to the destination thread
832-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
833-
std::shared_ptr<DelegateAsyncMsg<Args...>> msg(new DelegateAsyncMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
830+
auto msg = xmake_shared<DelegateAsyncMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
834831
if (!msg)
835832
BAD_ALLOC();
836833

@@ -1084,8 +1081,7 @@ class DelegateFunctionAsync<RetType(Args...)> : public DelegateFunction<RetType(
10841081
BAD_ALLOC();
10851082

10861083
// Create a new message instance for sending to the destination thread
1087-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
1088-
std::shared_ptr<DelegateAsyncMsg<Args...>> msg(new DelegateAsyncMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
1084+
auto msg = xmake_shared<DelegateAsyncMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
10891085
if (!msg)
10901086
BAD_ALLOC();
10911087

DelegateMQ/delegate/DelegateAsyncWait.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ class DelegateFreeAsyncWait<RetType(Args...)> : public DelegateFree<RetType(Args
330330
BAD_ALLOC();
331331

332332
// Create a new message instance for sending to the destination thread.
333-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
334-
std::shared_ptr<DelegateAsyncWaitMsg<Args...>> msg(new DelegateAsyncWaitMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
333+
auto msg = xmake_shared<DelegateAsyncWaitMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
335334
if (!msg)
336335
BAD_ALLOC();
337336
msg->SetInvokerWaiting(true);
@@ -767,8 +766,7 @@ class DelegateMemberAsyncWait<TClass, RetType(Args...)> : public DelegateMember<
767766
BAD_ALLOC();
768767

769768
// Create a new message instance for sending to the destination thread.
770-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
771-
std::shared_ptr<DelegateAsyncWaitMsg<Args...>> msg(new DelegateAsyncWaitMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
769+
auto msg = xmake_shared<DelegateAsyncWaitMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
772770
if (!msg)
773771
BAD_ALLOC();
774772
msg->SetInvokerWaiting(true);
@@ -1121,8 +1119,7 @@ class DelegateMemberAsyncWaitSp<TClass, RetType(Args...)> : public DelegateMembe
11211119
BAD_ALLOC();
11221120

11231121
// Create a new message instance for sending to the destination thread.
1124-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
1125-
std::shared_ptr<DelegateAsyncWaitMsg<Args...>> msg(new DelegateAsyncWaitMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
1122+
auto msg = xmake_shared<DelegateAsyncWaitMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
11261123
if (!msg)
11271124
BAD_ALLOC();
11281125
msg->SetInvokerWaiting(true);
@@ -1477,8 +1474,7 @@ class DelegateFunctionAsyncWait<RetType(Args...)> : public DelegateFunction<RetT
14771474
BAD_ALLOC();
14781475

14791476
// Create a new message instance for sending to the destination thread.
1480-
// If using XALLOCATOR explicit operator new required. See xallocator.h.
1481-
std::shared_ptr<DelegateAsyncWaitMsg<Args...>> msg(new DelegateAsyncWaitMsg<Args...>(delegate, m_priority, std::forward<Args>(args)...));
1477+
auto msg = xmake_shared<DelegateAsyncWaitMsg<Args...>>(delegate, m_priority, std::forward<Args>(args)...);
14821478
if (!msg)
14831479
BAD_ALLOC();
14841480
msg->SetInvokerWaiting(true);

DelegateMQ/delegate/DelegateMsg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DelegateMsg
4242

4343
/// Get the delegate message priority
4444
/// @return Delegate message priority
45-
Priority GetPriority() { return m_priority; }
45+
Priority GetPriority() const { return m_priority; }
4646

4747
private:
4848
/// The IThreadInvoker instance used to invoke the target function

DelegateMQ/delegate/DelegateOpt.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#error "RTTI compiler option is disabled but required by the DelegateMQ library."
1313
#endif
1414

15-
#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_WIN32)
16-
// Windows / Linux / macOS (Standard Library)
17-
#include <condition_variable>
15+
#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_WIN32) || defined(DMQ_THREAD_QT)
16+
// Windows / Linux / macOS / Qt (Standard Library)
17+
#include <condition_variable>
1818
#elif defined(DMQ_THREAD_FREERTOS)
1919
#include "predef/util/FreeRTOSClock.h"
2020
#include "predef/util/FreeRTOSMutex.h"
@@ -38,7 +38,7 @@ namespace dmq
3838
// @TODO: Change aliases to switch clock type globally if necessary
3939

4040
// --- CLOCK SELECTION ---
41-
#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_WIN32)
41+
#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_WIN32) || defined(DMQ_THREAD_QT)
4242
// Windows / Linux / macOS / Qt
4343
using Clock = std::chrono::steady_clock;
4444

@@ -68,7 +68,7 @@ namespace dmq
6868
using TimePoint = typename Clock::time_point;
6969

7070
// --- MUTEX / LOCK SELECTION ---
71-
#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_WIN32)
71+
#if defined(DMQ_THREAD_STDLIB) || defined(DMQ_THREAD_WIN32) || defined(DMQ_THREAD_QT)
7272
// Windows / Linux / macOS / Qt
7373
using Mutex = std::mutex;
7474
using RecursiveMutex = std::recursive_mutex;
@@ -143,6 +143,8 @@ namespace dmq
143143
#include <string>
144144
#include <list>
145145
#include <sstream>
146+
#include <memory>
147+
#include <utility>
146148

147149
// Not using xallocator; define as nothing
148150
#undef XALLOCATOR
@@ -161,6 +163,13 @@ namespace dmq
161163

162164
typedef std::string xstring;
163165
typedef std::wstring xwstring;
166+
167+
// Fallback xmake_shared — uses std::make_shared when fixed-block allocator is disabled
168+
template <typename T, typename... Args>
169+
inline std::shared_ptr<T> xmake_shared(Args&&... args)
170+
{
171+
return std::make_shared<T>(std::forward<Args>(args)...);
172+
}
164173
#endif
165174

166175
// @TODO: Select the desired logging (see Predef.cmake).

DelegateMQ/delegate/IThread.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ class IThread
2525
virtual ~IThread() = default;
2626

2727
/// @brief Enqueues a delegate message for execution on this thread.
28-
///
29-
/// @details
30-
/// This function is called by the *source* thread (the caller). The implementation must
28+
///
29+
/// @details
30+
/// This function is called by the *source* thread (the caller). The implementation must
3131
/// thread-safely transfer ownership of the `msg` into the target thread's processing queue.
32-
///
33-
/// Once the message is received by the target thread's main loop, that loop is responsible
32+
///
33+
/// Once the message is received by the target thread's main loop, that loop is responsible
3434
/// for calling `IInvoker::Invoke(msg)` to actually execute the function.
3535
///
36-
/// @param[in] msg A shared pointer to the delegate message. This pointer must remain valid
36+
/// @param[in] msg A shared pointer to the delegate message. This pointer must remain valid
3737
/// until the target thread finishes execution.
3838
virtual void DispatchDelegate(std::shared_ptr<DelegateMsg> msg) = 0;
39+
40+
/// @brief Returns true if the calling thread is this thread.
41+
/// Used to decide whether to marshal an event or execute inline.
42+
virtual bool IsCurrentThread() = 0;
3943
};
4044

4145
}

DelegateMQ/delegate/Signal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class Signal<RetType(Args...)>
165165
/// or call `Disconnect()` manually.
166166
[[nodiscard]] ScopedConnection Connect(const DelegateType& delegate) {
167167
auto copy = std::shared_ptr<DelegateType>(delegate.Clone());
168+
if (!copy)
169+
BAD_ALLOC();
168170
auto state = m_state;
169171
{
170172
std::lock_guard<RecursiveMutex> lock(state->mtx);
@@ -216,7 +218,7 @@ class Signal<RetType(Args...)>
216218
xlist<std::shared_ptr<DelegateType>> delegates;
217219
XALLOCATOR
218220
};
219-
std::shared_ptr<State> m_state = std::make_shared<State>();
221+
std::shared_ptr<State> m_state = xmake_shared<State>();
220222
};
221223

222224
} // namespace dmq

DelegateMQ/predef/allocator/stl_allocator.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// David Lafreniere
66

77
#include "xallocator.h"
8-
#include <memory> // For std::allocator and std::allocator_traits
8+
#include <memory> // For std::allocator and std::allocator_traits
9+
#include <utility> // For std::forward
910

1011
// Forward declaration for stl_allocator<void>
1112
template <typename T>
@@ -63,9 +64,16 @@ class stl_allocator : public std::allocator<T>
6364

6465
// Comparison operators for compatibility
6566
template <typename T, typename U>
66-
inline bool operator==(const stl_allocator<T>&, const stl_allocator<U>) { return true; }
67+
inline bool operator==(const stl_allocator<T>&, const stl_allocator<U>&) { return true; }
6768

6869
template <typename T, typename U>
69-
inline bool operator!=(const stl_allocator<T>&, const stl_allocator<U>) { return false; }
70+
inline bool operator!=(const stl_allocator<T>&, const stl_allocator<U>&) { return false; }
71+
72+
// Create a shared_ptr with both the object and control block allocated from the fixed-block pool
73+
template <typename T, typename... Args>
74+
inline std::shared_ptr<T> xmake_shared(Args&&... args)
75+
{
76+
return std::allocate_shared<T>(stl_allocator<T>(), std::forward<Args>(args)...);
77+
}
7078

7179
#endif

DelegateMQ/predef/os/cmsis-rtos2/Thread.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ osThreadId_t Thread::GetCurrentThreadId()
142142
return osThreadGetId();
143143
}
144144

145+
//----------------------------------------------------------------------------
146+
// IsCurrentThread
147+
//----------------------------------------------------------------------------
148+
bool Thread::IsCurrentThread()
149+
{
150+
return GetThreadId() == GetCurrentThreadId();
151+
}
152+
145153
//----------------------------------------------------------------------------
146154
// DispatchDelegate
147155
//----------------------------------------------------------------------------

DelegateMQ/predef/os/cmsis-rtos2/Thread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Thread : public dmq::IThread
4848
osThreadId_t GetThreadId();
4949
static osThreadId_t GetCurrentThreadId();
5050

51+
/// Returns true if the calling thread is this thread
52+
virtual bool IsCurrentThread() override;
53+
5154
/// Set the thread priority.
5255
/// Can be called before or after CreateThread().
5356
void SetThreadPriority(osPriority_t priority);

0 commit comments

Comments
 (0)