Action Engine
Loading...
Searching...
No Matches
waiter_state.h
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef THREAD_FIBER_CHANNEL_WAITER_STATE_H_
16#define THREAD_FIBER_CHANNEL_WAITER_STATE_H_
17
18#include "thread/cases.h"
19
20namespace thread::internal {
21// A struct that maintains readers' and writers' queues and provides methods
22// to find matching readers and writers in a coordinated way, i.e. locking
23// the selector mutexes of both case states if a pair is found.
24struct ChannelWaiterState {
25 bool GetMatchingReader(const CaseInSelectClause* writer,
26 CaseInSelectClause** reader) const
27 ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true, (*reader)->selector->mu,
28 writer->selector->mu);
29 bool GetMatchingWriter(const CaseInSelectClause* reader,
30 CaseInSelectClause** writer) const
31 ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true, reader->selector->mu,
32 (*writer)->selector->mu);
33
34 // Attempt to find an eligible queued writer. There is no matching reader in
35 // this case, it is used for when space becomes available in the queue due to
36 // a read completing, allowing a writer to complete without partner.
37 //
38 // Returns true, and updates *writer, if a suitable waiter exists. *writer is
39 // returned with selector mutex held and guaranteed pickable.
40 // Returns false with no side effects otherwise.
41 bool GetWaitingWriter(CaseInSelectClause** writer) const
42 ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true, (*writer)->selector->mu);
43
44 // Unlock (and mark selected) the passed reader/writer respectively.
45 // REQUIRES: selector mutex is held, picked == kNonePicked
46 void UnlockAndReleaseReader(CaseInSelectClause* reader)
47 ABSL_UNLOCK_FUNCTION(reader->selector->mu);
48 void UnlockAndReleaseWriter(CaseInSelectClause* writer)
49 ABSL_UNLOCK_FUNCTION(writer->selector->mu);
50
51 // Releases all waiting readers. Unselected readers are picked and marked to
52 // return that this channel was closed.
53 void CloseAndReleaseReaders();
54
55 internal::CaseInSelectClause* readers = nullptr;
56 internal::CaseInSelectClause* writers = nullptr;
57};
58} // namespace thread::internal
59
60#endif // THREAD_FIBER_CHANNEL_WAITER_STATE_H_