Skip to content

Commit f19f749

Browse files
[libc][rpc] Factor try_lock, unlock into functions on Process
Reduces line noise and localises changes needed for volta Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D149845
1 parent 525d60b commit f19f749

File tree

1 file changed

+19
-8
lines changed
  • libc/src/__support/RPC

1 file changed

+19
-8
lines changed

libc/src/__support/RPC/rpc.h

+19-8
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,23 @@ template <bool InvertInbox> struct Process {
101101
return InvertInbox ? !i : i;
102102
}
103103

104-
/// Determines if this process needs to wait for ownership of the buffer
104+
/// Determines if this process needs to wait for ownership of the buffer.
105105
LIBC_INLINE static bool buffer_unavailable(uint32_t in, uint32_t out) {
106106
return in != out;
107107
}
108+
109+
/// Attempt to claim the lock at index. Return true on lock taken.
110+
/// The lock is held when the zeroth bit of the uint32_t at lock[index]
111+
/// is set, and available when that bit is clear. Bits [1, 32) are zero.
112+
/// Or with one is a no-op when the lock is already held.
113+
LIBC_INLINE bool try_lock(uint64_t index) {
114+
return lock[index].fetch_or(1, cpp::MemoryOrder::RELAXED) == 0;
115+
}
116+
117+
// Unlock the lock at index.
118+
LIBC_INLINE void unlock(uint64_t index) {
119+
lock[index].store(0, cpp::MemoryOrder::RELAXED);
120+
}
108121
};
109122

110123
/// The port provides the interface to communicate between the multiple
@@ -130,9 +143,7 @@ template <bool T> struct Port {
130143
return process.buffer[index].opcode;
131144
}
132145

133-
LIBC_INLINE void close() {
134-
process.lock[index].store(0, cpp::MemoryOrder::RELAXED);
135-
}
146+
LIBC_INLINE void close() { process.unlock(index); }
136147

137148
private:
138149
Process<T> &process;
@@ -258,7 +269,7 @@ LIBC_INLINE void Port<T>::recv_n(A alloc) {
258269
LIBC_INLINE cpp::optional<Client::Port> Client::try_open(uint16_t opcode) {
259270
constexpr uint64_t index = 0;
260271
// Attempt to acquire the lock on this index.
261-
if (lock[index].fetch_or(1, cpp::MemoryOrder::RELAXED))
272+
if (!try_lock(index))
262273
return cpp::nullopt;
263274

264275
// The mailbox state must be read with the lock held.
@@ -271,7 +282,7 @@ LIBC_INLINE cpp::optional<Client::Port> Client::try_open(uint16_t opcode) {
271282
// state.
272283

273284
if (buffer_unavailable(in, out)) {
274-
lock[index].store(0, cpp::MemoryOrder::RELAXED);
285+
unlock(index);
275286
return cpp::nullopt;
276287
}
277288

@@ -300,7 +311,7 @@ LIBC_INLINE cpp::optional<Server::Port> Server::try_open() {
300311
return cpp::nullopt;
301312

302313
// Attempt to acquire the lock on this index.
303-
if (lock[index].fetch_or(1, cpp::MemoryOrder::RELAXED))
314+
if (!try_lock(index))
304315
return cpp::nullopt;
305316

306317
// The mailbox state must be read with the lock held.
@@ -310,7 +321,7 @@ LIBC_INLINE cpp::optional<Server::Port> Server::try_open() {
310321
out = outbox[index].load(cpp::MemoryOrder::RELAXED);
311322

312323
if (buffer_unavailable(in, out)) {
313-
lock[index].store(0, cpp::MemoryOrder::RELAXED);
324+
unlock(index);
314325
return cpp::nullopt;
315326
}
316327

0 commit comments

Comments
 (0)