curl-fuzzer C++ reference
Loading...
Searching...
No Matches
resolver_cancel_gate.h
Go to the documentation of this file.
1/*
2 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3 *
4 * SPDX-License-Identifier: curl
5 */
6
9
10#ifndef PROTO_FUZZER_RESOLVER_CANCEL_GATE_H_
11#define PROTO_FUZZER_RESOLVER_CANCEL_GATE_H_
12
13#include <chrono>
14#include <condition_variable>
15#include <mutex>
16
17namespace proto_fuzzer {
18
19namespace resolver_cancel_detail {
20
22struct GateState {
24 std::mutex mutex;
26 std::condition_variable released;
28 bool armed = false;
31};
32
35inline GateState& State() {
36 static GateState state;
37 return state;
38}
39
40} // namespace resolver_cancel_detail
41
47class ScopedResolverCancelGate {
48 public:
49 ScopedResolverCancelGate() {
50 auto& state = resolver_cancel_detail::State();
51 std::lock_guard<std::mutex> lock(state.mutex);
52 state.first_lookup_waiting = false;
53 state.armed = true;
54 }
55
56 ~ScopedResolverCancelGate() {
57 auto& state = resolver_cancel_detail::State();
58 {
59 std::lock_guard<std::mutex> lock(state.mutex);
60 state.armed = false;
61 }
62 state.released.notify_all();
63 }
64
65 ScopedResolverCancelGate(const ScopedResolverCancelGate&) = delete;
66 ScopedResolverCancelGate& operator=(const ScopedResolverCancelGate&) = delete;
67};
68
73inline bool WaitForResolverCancelLookup(std::chrono::milliseconds timeout) {
74 auto& state = resolver_cancel_detail::State();
75 std::unique_lock<std::mutex> lock(state.mutex);
76 return state.released.wait_for(lock, timeout, [&state] { return state.first_lookup_waiting; });
77}
78
84 auto& state = resolver_cancel_detail::State();
85 std::unique_lock<std::mutex> lock(state.mutex);
86 if (!state.armed || state.first_lookup_waiting) {
87 return;
88 }
89 state.first_lookup_waiting = true;
90 state.released.notify_all();
91 state.released.wait(lock, [&state] { return !state.armed; });
92}
93
94} // namespace proto_fuzzer
95
96#endif // PROTO_FUZZER_RESOLVER_CANCEL_GATE_H_
void WaitAtResolverCancelGate()
Block only the first lookup issued while ScopedResolverCancelGate is live.
GateState & State()
Access the process-wide resolver cancellation rendezvous.
bool WaitForResolverCancelLookup(std::chrono::milliseconds timeout)
Give curl's single resolver worker a bounded opportunity to take the first family before the main thr...
Shared state for the single opt-in resolver cancellation rendezvous.
bool first_lookup_waiting
Whether the resolver worker has reached the gate.
bool armed
Whether the current scope should hold the first lookup.
std::mutex mutex
Serializes every state transition and condition wait.
std::condition_variable released
Signals both lookup arrival and scope-owned release.