#pragma once #include "capability/capability.h" #include "include/ztypes.h" #include "lib/linked_list.h" #include "lib/mutex.h" #include "lib/pair.h" #include "lib/ref_ptr.h" #include "lib/shared_ptr.h" #include "object/kernel_object.h" #include "usr/zcall_internal.h" class Channel; template <> struct KernelObjectTag { static const uint64_t type = KernelObject::CHANNEL; }; class Channel : public KernelObject { public: uint64_t TypeTag() override { return KernelObject::CHANNEL; } static Pair, RefPtr> CreateChannelPair(); RefPtr peer() { return peer_; } z_err_t Write(const ZMessage& msg); z_err_t Read(ZMessage& msg); private: // FIXME: We will likely never close the channel based on this // circular dependency. RefPtr peer_{nullptr}; Mutex mutex_{"channel"}; struct Message { uint64_t num_bytes; uint8_t* bytes; LinkedList> caps; }; // FIXME: This is probably dangerous because of an // implicit shallow copy. LinkedList> pending_messages_; LinkedList> blocked_threads_; friend class MakeRefCountedFriend; Channel() {} void SetPeer(const RefPtr& peer) { peer_ = peer; } z_err_t EnqueueMessage(const ZMessage& msg); };