OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-cogutil commits / c46517c

Get rid of the latch; use an atomic.
Better. This calls notify too often. But OK for now.
c46517c983cf1bb0bc3a2fa6242f9a4e23cbedea
by Linas Vepstas <linasvepstas@gmail.com> · 2025-12-07T19:22:57.000Z

Changed files (1) +24 -11

opencog/util/async_buffer.h +24 -11
……@@ -27,7 +27,6 @@
2727
2828#include <atomic>
2929#include <chrono>
30#include <latch>
3130#include <mutex>
3231#include <thread>
3332#include <vector>
……@@ -144,7 +143,7 @@ class async_buffer
144143
145144 // Barrier synchronization
146145 const Element* _current_barrier;
147 std::latch* _barrier_latch;
146 std::atomic<unsigned> _barrier_count;
148147
149148 void start_writer_thread();
150149 void stop_writer_threads();
……@@ -219,7 +218,7 @@ async_buffer<Writer, Element>::async_buffer(Writer* wr,
219218 _stall_writers = false;
220219 _in_drain = false;
221220 _current_barrier = nullptr;
222 _barrier_latch = nullptr;
221 _barrier_count = 0;
223222
224223 _high_watermark = DEFAULT_HIGH_WATER_MARK;
225224 _low_watermark = DEFAULT_LOW_WATER_MARK;
……@@ -360,7 +359,7 @@ void async_buffer<Writer, Element>::stop_writer_threads()
360359 }
361360
362361 _current_barrier = nullptr;
363 _barrier_latch = nullptr;
362 _barrier_count = 0;
364363
365364 // Its now OK to start new threads, if desired ...(!)
366365 _stopping_writers = false;
……@@ -454,17 +453,23 @@ void async_buffer<Writer, Element>::barrier(const Element& elt)
454453
455454 drain();
456455
457 std::latch completion(_thread_count);
458 _barrier_latch = &completion;
456 _barrier_count = _thread_count + 1;
459457 _current_barrier = &elt;
460458
461459 _store_set.cancel();
462460
463 completion.wait();
461 // Wait for all workers to finish processing
462 unsigned cnt = _barrier_count.load();
463 while (1 < cnt)
464 {
465 _barrier_count.wait(cnt);
466 cnt = _barrier_count.load();
467 }
464468
465 _store_set.cancel_reset();
466469 _current_barrier = nullptr;
467 _barrier_latch = nullptr;
470 _store_set.cancel_reset();
471 _barrier_count--;
472 _barrier_count.notify_all();
468473}
469474
470475/// A single write thread. Reads elements from set, and invokes the
……@@ -494,8 +499,16 @@ void async_buffer<Writer, Element>::write_loop()
494499 if (_current_barrier != nullptr)
495500 {
496501 (_writer->*_do_write)(*_current_barrier);
497 _barrier_latch->count_down();
498 _barrier_latch->wait();
502 _barrier_count--;
503 _barrier_count.notify_all();
504
505 // Wait for barrier to complete
506 unsigned cnt = _barrier_count.load();
507 while (0 < cnt)
508 {
509 _barrier_count.wait(cnt);
510 cnt = _barrier_count.load();
511 }
499512 }
500513
501514 // We are so out of here. Nothing to do, just exit this thread.