2023-06-07 08:24:10 -07:00
|
|
|
#include "object/channel.h"
|
|
|
|
|
2023-06-17 01:45:53 -07:00
|
|
|
#include "include/ztypes.h"
|
2023-06-15 16:20:29 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
2023-06-07 08:24:10 -07:00
|
|
|
|
2023-06-21 20:47:40 -07:00
|
|
|
glcr::Pair<glcr::RefPtr<Channel>, glcr::RefPtr<Channel>>
|
2023-06-21 15:07:40 -07:00
|
|
|
Channel::CreateChannelPair() {
|
|
|
|
auto c1 = glcr::MakeRefCounted<Channel>();
|
|
|
|
auto c2 = glcr::MakeRefCounted<Channel>();
|
2023-06-07 08:24:10 -07:00
|
|
|
c1->SetPeer(c2);
|
|
|
|
c2->SetPeer(c1);
|
|
|
|
return {c1, c2};
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:36:17 -07:00
|
|
|
z_err_t Channel::Write(uint64_t num_bytes, const void* bytes, uint64_t num_caps,
|
|
|
|
const z_cap_t* caps) {
|
|
|
|
return peer_->WriteInternal(num_bytes, bytes, num_caps, caps);
|
2023-06-07 08:24:10 -07:00
|
|
|
}
|
|
|
|
|
2023-06-20 15:36:17 -07:00
|
|
|
z_err_t Channel::Read(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
|
|
|
|
z_cap_t* caps) {
|
2023-06-15 16:20:29 -07:00
|
|
|
mutex_.Lock();
|
2023-06-20 15:36:17 -07:00
|
|
|
while (message_queue_.empty()) {
|
|
|
|
auto thread = gScheduler->CurrentThread();
|
|
|
|
thread->SetState(Thread::BLOCKED);
|
|
|
|
blocked_threads_.PushBack(thread);
|
2023-06-15 16:20:29 -07:00
|
|
|
mutex_.Unlock();
|
|
|
|
gScheduler->Yield();
|
|
|
|
mutex_.Lock();
|
2023-06-07 08:24:10 -07:00
|
|
|
}
|
2023-06-15 16:20:29 -07:00
|
|
|
mutex_.Unlock();
|
|
|
|
|
|
|
|
MutexHolder lock(mutex_);
|
2023-06-20 15:36:17 -07:00
|
|
|
return message_queue_.PopFront(num_bytes, bytes, num_caps, caps);
|
2023-06-07 08:24:10 -07:00
|
|
|
}
|
|
|
|
|
2023-06-20 15:36:17 -07:00
|
|
|
z_err_t Channel::WriteInternal(uint64_t num_bytes, const void* bytes,
|
|
|
|
uint64_t num_caps, const z_cap_t* caps) {
|
2023-06-15 16:20:29 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-20 15:36:17 -07:00
|
|
|
RET_ERR(message_queue_.PushBack(num_bytes, bytes, num_caps, caps));
|
2023-06-15 16:20:29 -07:00
|
|
|
|
|
|
|
if (blocked_threads_.size() > 0) {
|
2023-06-16 01:29:00 -07:00
|
|
|
auto thread = blocked_threads_.PopFront();
|
|
|
|
thread->SetState(Thread::RUNNABLE);
|
|
|
|
gScheduler->Enqueue(thread);
|
2023-06-15 16:20:29 -07:00
|
|
|
}
|
2023-06-21 18:28:54 -07:00
|
|
|
return glcr::OK;
|
2023-06-07 08:24:10 -07:00
|
|
|
}
|