2023-06-12 19:20:51 -07:00
|
|
|
#include "object/port.h"
|
|
|
|
|
2023-06-12 20:56:25 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
|
|
|
|
2023-06-12 19:20:51 -07:00
|
|
|
Port::Port() {}
|
|
|
|
|
2023-06-20 15:29:32 -07:00
|
|
|
z_err_t Port::Write(uint64_t num_bytes, const void* bytes, uint64_t num_caps,
|
|
|
|
const z_cap_t* caps) {
|
|
|
|
MutexHolder h(mutex_);
|
|
|
|
RET_ERR(message_queue_.PushBack(num_bytes, bytes, num_caps, caps));
|
2023-06-12 20:56:25 -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-12 20:56:25 -07:00
|
|
|
}
|
2023-06-21 18:28:54 -07:00
|
|
|
return glcr::OK;
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
|
|
|
|
2023-06-20 15:29:32 -07:00
|
|
|
z_err_t Port::Read(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
|
|
|
|
z_cap_t* caps) {
|
2023-06-12 20:56:25 -07:00
|
|
|
mutex_.Lock();
|
2023-06-20 15:29:32 -07:00
|
|
|
while (message_queue_.empty()) {
|
2023-06-20 15:36:17 -07:00
|
|
|
auto thread = gScheduler->CurrentThread();
|
|
|
|
thread->SetState(Thread::BLOCKED);
|
|
|
|
blocked_threads_.PushBack(thread);
|
2023-06-12 20:56:25 -07:00
|
|
|
mutex_.Unlock();
|
|
|
|
gScheduler->Yield();
|
|
|
|
mutex_.Lock();
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
2023-06-12 20:56:25 -07:00
|
|
|
mutex_.Unlock();
|
2023-06-12 19:20:51 -07:00
|
|
|
|
2023-06-12 20:56:25 -07:00
|
|
|
MutexHolder lock(mutex_);
|
2023-06-20 15:29:32 -07:00
|
|
|
return message_queue_.PopFront(num_bytes, bytes, num_caps, caps);
|
2023-06-12 19:20:51 -07:00
|
|
|
}
|
2023-06-16 23:15:28 -07:00
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
void Port::WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap) {
|
2023-06-17 00:07:58 -07:00
|
|
|
MutexHolder h(mutex_);
|
2023-06-20 15:29:32 -07:00
|
|
|
message_queue_.WriteKernel(init, cap);
|
2023-06-17 00:07:58 -07:00
|
|
|
}
|
|
|
|
|
2023-06-16 23:15:28 -07:00
|
|
|
bool Port::HasMessages() {
|
|
|
|
MutexHolder h(mutex_);
|
2023-06-20 15:29:32 -07:00
|
|
|
return !message_queue_.empty();
|
2023-06-16 23:15:28 -07:00
|
|
|
}
|