@@ -28,22 +28,14 @@ THE SOFTWARE.
28
28
#define PARALLEL_BUILD
29
29
30
30
// Macro for allocating 16-byte aligned stack memory
31
- #define STACK_ALLOC (COUNT, TYPE ) static_cast <TYPE *>(Align(16 , (COUNT) * sizeof (TYPE), alloca(RoundUp( 16 , ( COUNT) * sizeof (TYPE)) )))
31
+ #define STACK_ALLOC (COUNT, TYPE ) static_cast <TYPE *>(Align(16u , (COUNT) * sizeof (TYPE), (COUNT) * sizeof (TYPE) + 15u , alloca(( COUNT) * sizeof (TYPE) + 15u )))
32
32
33
33
namespace RadeonRays
34
34
{
35
35
inline
36
- std::size_t RoundUp ( std::size_t alignment , std::size_t size )
36
+ void * Align ( std::size_t alignment, std::size_t size , std::size_t space, void *ptr )
37
37
{
38
- return (size + alignment - 1 ) & ~(alignment - 1 );
39
- }
40
-
41
- inline
42
- void *Align (std::size_t alignment, std::size_t size, void *ptr)
43
- {
44
- std::size_t space;
45
- void *aligned_ptr = std::align (alignment, RoundUp (alignment, size), ptr, space);
46
- return aligned_ptr;
38
+ return std::align (alignment, size, ptr, space);
47
39
}
48
40
49
41
#ifdef __GNUC__
@@ -225,16 +217,21 @@ namespace RadeonRays
225
217
}
226
218
}
227
219
#else
228
- std::mutex mutex;
220
+ // Parallel build variables
221
+ // Global requests stack
222
+ std::stack<SplitRequest> requests;
223
+ // Condition to wait on the global stack
229
224
std::condition_variable cv;
230
- std::atomic_bool shutdown;
225
+ // Mutex to guard cv
226
+ std::mutex mutex;
227
+ // Indicates if we need to shutdown all the threads
228
+ std::atomic<bool > shutdown;
229
+ // Number of primitives processed so far
231
230
std::atomic<std::uint32_t > num_refs_processed;
232
231
233
232
num_refs_processed.store (0 );
234
233
shutdown.store (false );
235
234
236
- std::stack<SplitRequest> requests;
237
-
238
235
requests.push (SplitRequest{
239
236
scene_min,
240
237
scene_max,
@@ -311,16 +308,23 @@ namespace RadeonRays
311
308
312
309
for (auto i = 0u ; i < num_threads; ++i)
313
310
{
314
- threads[i] = std::move (std::thread (worker_thread));
315
- threads[i].detach ();
311
+ threads[i] = std::thread (worker_thread);
316
312
}
317
313
318
314
while (num_refs_processed != num_aabbs)
319
315
{
320
316
std::this_thread::sleep_for (std::chrono::milliseconds (20 ));
321
317
}
322
318
323
- shutdown = true ;
319
+ // Signal shutdown and wake up all the threads
320
+ shutdown.store (true );
321
+ cv.notify_all ();
322
+
323
+ // Wait for all the threads to finish
324
+ for (auto i = 0u ; i < num_threads; ++i)
325
+ {
326
+ threads[i].join ();
327
+ }
324
328
#endif
325
329
}
326
330
0 commit comments