bneradt commented on code in PR #13566:
URL: https://github.com/apache/trafficserver/pull/13566#discussion_r3857506303


##########
src/tsutil/unit_tests/test_AtomicSharedPtr.cc:
##########
@@ -0,0 +1,162 @@
+/** @file
+
+  Unit tests for AtomicSharedPtr
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include <catch2/catch_test_macros.hpp>
+
+#include "tsutil/AtomicSharedPtr.h"
+
+#include <algorithm>
+#include <atomic>
+#include <chrono>
+#include <memory>
+#include <thread>
+#include <vector>
+
+namespace
+{
+struct Payload {
+  static constexpr size_t VALUE_COUNT = 8;
+
+  explicit Payload(int generation) : generation_(generation) { 
std::fill(std::begin(values_), std::end(values_), generation); }
+
+  bool
+  is_valid() const
+  {
+    return std::all_of(std::begin(values_), std::end(values_), [this](int 
value) { return value == generation_; });
+  }
+
+  int generation_ = 0;
+  int values_[VALUE_COUNT];
+};
+
+struct ReaderState {
+  std::atomic<bool> should_start{false};
+  std::atomic<bool> should_stop{false};

Review Comment:
   On the two flags: switched both to `std::atomic_flag`. `should_start` now 
uses `wait(false, acquire)` against the writer's `test_and_set(release)` + 
`notify_all()`, which drops the spin-with-`yield()` loop, and `should_stop` 
uses `test(acquire)`.
   
   While doing that I also removed `wait_for_reader()` and its 5s timeout in 
favor of `read_count.wait(0, acquire)`, with the first reader issuing a single 
`notify_all()`. The timeout wasn't buying anything: its failure path called 
`stop_readers()`, which joins the readers, so a reader that never saw 
`should_start` would have hung in the join anyway. Net effect is 27 lines out, 
19 in.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to