@@ -11,18 +11,18 @@ void emitError(renderer_test::CoreToRHIDebugBridge& bridge, const char* message)
1111 bridge.handleMessage (rhi::DebugMessageType::Error, rhi::DebugMessageSource::Layer, message);
1212}
1313
14- renderer_test::CoreToRHIDebugBridge& getStaticBridgeAfterStackCallbackScope ()
14+ renderer_test::CoreToRHIDebugBridge* getRetainedBridgeAfterStackCallbackScope ()
1515{
16- static renderer_test::CoreToRHIDebugBridge bridge ;
16+ auto bridge = renderer_test::createRetainedCoreToRHIDebugBridge () ;
1717
1818 renderer_test::CoreDebugCallback callback;
1919 {
20- renderer_test::ScopedCoreDebugCallback scopedDebugCallback (bridge, &callback);
21- emitError (bridge, " static scope" );
20+ renderer_test::ScopedCoreDebugCallback scopedDebugCallback (* bridge, &callback);
21+ emitError (* bridge, " retained scope" );
2222 }
23- SLANG_CHECK (callback.getString () == " static scope\n " );
23+ SLANG_CHECK (callback.getString () == " retained scope\n " );
2424
25- return bridge;
25+ return bridge. Ptr () ;
2626}
2727} // namespace
2828
@@ -99,61 +99,78 @@ SLANG_UNIT_TEST(scopedCoreDebugCallbackClearsBridgeOnException)
9999 SLANG_CHECK (secondCallback.getString () == " next iteration\n " );
100100}
101101
102- SLANG_UNIT_TEST (scopedCoreDebugCallbackClearsStaticBridgeAfterStackCallback )
102+ SLANG_UNIT_TEST (scopedCoreDebugCallbackSeparatesRetainedBridgeScopes )
103103{
104- renderer_test::CoreToRHIDebugBridge& bridge = getStaticBridgeAfterStackCallbackScope ();
105-
106- emitError (bridge, " after stack callback" );
104+ renderer_test::CoreToRHIDebugBridge* oldBridge = getRetainedBridgeAfterStackCallbackScope ();
105+ auto nextBridge = renderer_test::createRetainedCoreToRHIDebugBridge ();
107106
108107 renderer_test::CoreDebugCallback nextCallback;
109108 {
110- renderer_test::ScopedCoreDebugCallback scopedDebugCallback (bridge, &nextCallback);
111- emitError (bridge, " next invocation" );
109+ renderer_test::ScopedCoreDebugCallback scopedDebugCallback (*nextBridge, &nextCallback);
110+ emitError (*oldBridge, " after stack callback" );
111+ emitError (*nextBridge, " next invocation" );
112112 }
113113 SLANG_CHECK (nextCallback.getString () == " next invocation\n " );
114114}
115115
116116SLANG_UNIT_TEST (coreDebugBridgeHandlesConcurrentMessages)
117117{
118118 static constexpr int kThreadCount = 4 ;
119- static constexpr int kMessageCount = 64 ;
119+ static constexpr int kMessageCount = 1024 ;
120120
121121 renderer_test::CoreToRHIDebugBridge bridge;
122122 renderer_test::CoreDebugCallback callback;
123- {
124- renderer_test::ScopedCoreDebugCallback scopedDebugCallback (bridge, &callback );
123+ std::atomic< bool > startWriting ( false );
124+ std::atomic< bool > keepReading ( true );
125125
126- std::atomic<bool > keepReading (true );
127- std::thread readerThread (
126+ std::thread readerThread (
127+ [&]()
128+ {
129+ while (keepReading.load (std::memory_order_acquire))
130+ {
131+ callback.getString ();
132+ }
133+ });
134+
135+ std::thread writerThreads[kThreadCount ];
136+ for (int threadIndex = 0 ; threadIndex < kThreadCount ; ++threadIndex)
137+ {
138+ writerThreads[threadIndex] = std::thread (
128139 [&]()
129140 {
130- while (keepReading .load (std::memory_order_acquire))
141+ while (!startWriting .load (std::memory_order_acquire))
131142 {
132- callback. getString ();
143+ std::this_thread::yield ();
133144 }
134- });
135145
136- std::thread writerThreads[kThreadCount ];
137- for (int threadIndex = 0 ; threadIndex < kThreadCount ; ++threadIndex)
138- {
139- writerThreads[threadIndex] = std::thread (
140- [&]()
146+ for (int messageIndex = 0 ; messageIndex < kMessageCount ; ++messageIndex)
141147 {
142- for (int messageIndex = 0 ; messageIndex < kMessageCount ; ++messageIndex)
143- {
144- emitError (bridge, " x" );
145- }
146- });
147- }
148+ emitError (bridge, " x" );
149+ }
150+ });
151+ }
148152
149- for (auto & writerThread : writerThreads)
153+ {
154+ renderer_test::ScopedCoreDebugCallback scopedDebugCallback (bridge, &callback);
155+ startWriting.store (true , std::memory_order_release);
156+ for (int spinCount = 0 ; spinCount < 100000 && callback.getString ().getLength () == 0 ;
157+ ++spinCount)
150158 {
151- writerThread. join ();
159+ std::this_thread::yield ();
152160 }
161+ SLANG_CHECK (callback.getString ().getLength () > 0 );
162+ }
153163
154- keepReading.store (false , std::memory_order_release);
155- readerThread.join ();
164+ for (auto & writerThread : writerThreads)
165+ {
166+ writerThread.join ();
156167 }
157168
158- SLANG_CHECK (callback.getString ().getLength () == kThreadCount * kMessageCount * 2 );
169+ keepReading.store (false , std::memory_order_release);
170+ readerThread.join ();
171+
172+ auto capturedLength = callback.getString ().getLength ();
173+ SLANG_CHECK (capturedLength > 0 );
174+ SLANG_CHECK (capturedLength <= kThreadCount * kMessageCount * 2 );
175+ SLANG_CHECK ((capturedLength % 2 ) == 0 );
159176}
0 commit comments