Skip to content

Commit 7e79934

Browse files
[libc] Simplify access permissions, change to composition over inheritance
Private member variable minimises scope of access to Process Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D153603
1 parent be1b2ac commit 7e79934

File tree

1 file changed

+43
-25
lines changed
  • libc/src/__support/RPC

1 file changed

+43
-25
lines changed

libc/src/__support/RPC/rpc.h

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,13 @@ template <bool Invert, uint32_t lane_size> struct Process {
8282
LIBC_INLINE Process &operator=(Process &&) = default;
8383
LIBC_INLINE ~Process() = default;
8484

85-
template <bool T, uint32_t S> friend struct Port;
86-
87-
protected:
8885
uint64_t port_count;
8986
cpp::Atomic<uint32_t> *inbox;
9087
cpp::Atomic<uint32_t> *outbox;
9188
Packet<lane_size> *packet;
9289

9390
cpp::Atomic<uint32_t> lock[DEFAULT_PORT_COUNT] = {0};
9491

95-
public:
9692
/// Initialize the communication channels.
9793
LIBC_INLINE void reset(uint64_t port_count, void *buffer) {
9894
this->port_count = port_count;
@@ -328,7 +324,7 @@ template <bool T, uint32_t S> struct Port {
328324
};
329325

330326
/// The RPC client used to make requests to the server.
331-
struct Client : public Process<false, gpu::LANE_SIZE> {
327+
struct Client {
332328
LIBC_INLINE Client() = default;
333329
LIBC_INLINE Client(const Client &) = delete;
334330
LIBC_INLINE Client &operator=(const Client &) = delete;
@@ -337,10 +333,17 @@ struct Client : public Process<false, gpu::LANE_SIZE> {
337333
using Port = rpc::Port<false, gpu::LANE_SIZE>;
338334
template <uint16_t opcode> LIBC_INLINE cpp::optional<Port> try_open();
339335
template <uint16_t opcode> LIBC_INLINE Port open();
336+
337+
LIBC_INLINE void reset(uint64_t port_count, void *buffer) {
338+
process.reset(port_count, buffer);
339+
}
340+
341+
private:
342+
Process<false, gpu::LANE_SIZE> process;
340343
};
341344

342345
/// The RPC server used to respond to the client.
343-
template <uint32_t lane_size> struct Server : public Process<true, lane_size> {
346+
template <uint32_t lane_size> struct Server {
344347
LIBC_INLINE Server() = default;
345348
LIBC_INLINE Server(const Server &) = delete;
346349
LIBC_INLINE Server &operator=(const Server &) = delete;
@@ -349,6 +352,21 @@ template <uint32_t lane_size> struct Server : public Process<true, lane_size> {
349352
using Port = rpc::Port<true, lane_size>;
350353
LIBC_INLINE cpp::optional<Port> try_open();
351354
LIBC_INLINE Port open();
355+
356+
LIBC_INLINE void reset(uint64_t port_count, void *buffer) {
357+
process.reset(port_count, buffer);
358+
}
359+
360+
LIBC_INLINE void *get_buffer_start() const {
361+
return process.get_buffer_start();
362+
}
363+
364+
LIBC_INLINE static uint64_t allocation_size(uint64_t port_count) {
365+
return Process<true, lane_size>::allocation_size(port_count);
366+
}
367+
368+
private:
369+
Process<true, lane_size> process;
352370
};
353371

354372
/// Applies \p fill to the shared buffer and initiates a send operation.
@@ -487,28 +505,28 @@ template <uint16_t opcode>
487505
[[clang::convergent]] LIBC_INLINE cpp::optional<Client::Port>
488506
Client::try_open() {
489507
// Perform a naive linear scan for a port that can be opened to send data.
490-
for (uint64_t index = 0; index < this->port_count; ++index) {
508+
for (uint64_t index = 0; index < process.port_count; ++index) {
491509
// Attempt to acquire the lock on this index.
492510
uint64_t lane_mask = gpu::get_lane_mask();
493-
if (!this->try_lock(lane_mask, index))
511+
if (!process.try_lock(lane_mask, index))
494512
continue;
495513

496-
uint32_t in = this->load_inbox(index);
497-
uint32_t out = this->load_outbox(index);
514+
uint32_t in = process.load_inbox(index);
515+
uint32_t out = process.load_outbox(index);
498516

499517
// Once we acquire the index we need to check if we are in a valid sending
500518
// state.
501-
if (this->buffer_unavailable(in, out)) {
502-
this->unlock(lane_mask, index);
519+
if (process.buffer_unavailable(in, out)) {
520+
process.unlock(lane_mask, index);
503521
continue;
504522
}
505523

506524
if (is_first_lane(lane_mask)) {
507-
this->packet[index].header.opcode = opcode;
508-
this->packet[index].header.mask = lane_mask;
525+
process.packet[index].header.opcode = opcode;
526+
process.packet[index].header.mask = lane_mask;
509527
}
510528
gpu::sync_lane(lane_mask);
511-
return Port(*this, lane_mask, index, out);
529+
return Port(process, lane_mask, index, out);
512530
}
513531
return cpp::nullopt;
514532
}
@@ -528,29 +546,29 @@ template <uint32_t lane_size>
528546
cpp::optional<typename Server<lane_size>::Port>
529547
Server<lane_size>::try_open() {
530548
// Perform a naive linear scan for a port that has a pending request.
531-
for (uint64_t index = 0; index < this->port_count; ++index) {
532-
uint32_t in = this->load_inbox(index);
533-
uint32_t out = this->load_outbox(index);
549+
for (uint64_t index = 0; index < process.port_count; ++index) {
550+
uint32_t in = process.load_inbox(index);
551+
uint32_t out = process.load_outbox(index);
534552

535553
// The server is passive, if there is no work pending don't bother
536554
// opening a port.
537-
if (this->buffer_unavailable(in, out))
555+
if (process.buffer_unavailable(in, out))
538556
continue;
539557

540558
// Attempt to acquire the lock on this index.
541559
uint64_t lane_mask = gpu::get_lane_mask();
542-
if (!this->try_lock(lane_mask, index))
560+
if (!process.try_lock(lane_mask, index))
543561
continue;
544562

545-
in = this->load_inbox(index);
546-
out = this->load_outbox(index);
563+
in = process.load_inbox(index);
564+
out = process.load_outbox(index);
547565

548-
if (this->buffer_unavailable(in, out)) {
549-
this->unlock(lane_mask, index);
566+
if (process.buffer_unavailable(in, out)) {
567+
process.unlock(lane_mask, index);
550568
continue;
551569
}
552570

553-
return Port(*this, lane_mask, index, out);
571+
return Port(process, lane_mask, index, out);
554572
}
555573
return cpp::nullopt;
556574
}

0 commit comments

Comments
 (0)