[kernel] Add channel objects

Add the channel object for sending messages between threads. Currently
no good of passing channels to other threads, but global variables in a
single process work. Currently channels are slow and do double copies,
need to refine more.

Tags: ipc
This commit is contained in:
2020-07-26 17:29:11 -07:00
parent ae3290c53d
commit d3e9d92466
10 changed files with 264 additions and 3 deletions

View File

@@ -0,0 +1,70 @@
#include "kutil/assert.h"
#include "objects/channel.h"
channel::channel() :
m_len(0),
m_capacity(0),
m_data(nullptr),
kobject(kobject::type::channel, j6_signal_channel_can_send)
{
}
channel::~channel()
{
kutil::kfree(m_data);
}
j6_status_t
channel::enqueue(size_t len, void *data)
{
// TODO: Make this thread safe!
if (closed())
return j6_status_closed;
if (!can_send())
return j6_err_not_ready;
if (m_capacity < len) {
kutil::kfree(m_data);
m_data = kutil::kalloc(len);
m_capacity = len;
kassert(m_data, "Failed to allocate memory to copy channel message");
}
m_len = len;
kutil::memcpy(m_data, data, len);
assert_signal(j6_signal_channel_can_recv);
return j6_status_ok;
}
j6_status_t
channel::dequeue(size_t *len, void *data)
{
// TODO: Make this thread safe!
if (closed())
return j6_status_closed;
if (!can_receive())
return j6_err_not_ready;
if (!len)
return j6_err_invalid_arg;
if (*len < m_len)
return j6_err_insufficient;
kutil::memcpy(data, m_data, m_len);
*len = m_len;
assert_signal(j6_signal_channel_can_send);
return j6_status_ok;
}
void
channel::on_no_handles()
{
kobject::on_no_handles();
delete this;
}

View File

@@ -0,0 +1,49 @@
#pragma once
/// \file channel.h
/// Definition of channel objects and related functions
#include "j6/signals.h"
#include "objects/kobject.h"
/// Channels are bi-directional means of sending messages
class channel :
public kobject
{
public:
channel();
virtual ~channel();
/// Check if the channel has space for a message to be sent
inline bool can_send() const { return check_signal(j6_signal_channel_can_send); }
/// Check if the channel has a message wiating already
inline bool can_receive() const { return check_signal(j6_signal_channel_can_recv); }
/// Put a message into the channel
/// \arg len Length of data, in bytes
/// \arg data Pointer to the message data
/// \returns j6_status_ok on success
j6_status_t enqueue(size_t len, void *data);
/// Get a message from the channel, copied into a provided buffer
/// \arg len On input, the size of the provided buffer. On output,
/// the size of the message copied into the buffer.
/// \arg data Pointer to the buffer
/// \returns j6_status_ok on success
j6_status_t dequeue(size_t *len, void *data);
/// Mark this channel as closed, all future calls to enqueue or
/// dequeue messages will fail with j6_status_closed.
inline void close() { assert_signal(j6_signal_channel_closed); }
/// Check if this channel has been closed
inline bool closed() { return check_signal(j6_signal_channel_closed); }
protected:
virtual void on_no_handles() override;
private:
size_t m_len;
size_t m_capacity;
void *m_data;
};

View File

@@ -19,6 +19,7 @@ public:
event,
eventpair,
channel,
vms,
vmo,
@@ -55,6 +56,10 @@ public:
/// \arg s The set of signals to deassert
void deassert_signal(j6_signal_t s);
/// Check if the given signals are set on this object
/// \arg s The set of signals to check
inline bool check_signal(j6_signal_t s) const { return (m_signals & s) == s; }
/// Increment the handle refcount
inline void handle_retain() { ++m_handle_count; }

View File

@@ -12,3 +12,7 @@ SYSCALL(0x1a, thread_exit, int64_t)
SYSCALL(0x1b, thread_pause, void)
SYSCALL(0x1c, thread_sleep, uint64_t)
SYSCALL(0x20, channel_create, j6_handle_t *)
SYSCALL(0x21, channel_close, j6_handle_t)
SYSCALL(0x22, channel_send, j6_handle_t, size_t, void *)
SYSCALL(0x23, channel_receive, j6_handle_t, size_t *, void *)

View File

@@ -0,0 +1,76 @@
#include "j6/errors.h"
#include "j6/types.h"
#include "objects/channel.h"
#include "objects/thread.h"
#include "objects/process.h"
#include "scheduler.h"
namespace syscalls {
j6_status_t
channel_create(j6_handle_t *handle)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
channel *c = new channel;
*handle = p.add_handle(c);
return j6_status_ok;
}
j6_status_t
channel_close(j6_handle_t handle)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel)
return j6_err_invalid_arg;
p.remove_handle(handle);
channel *c = static_cast<channel*>(o);
c->close();
return j6_status_ok;
}
j6_status_t
channel_send(j6_handle_t handle, size_t len, void *data)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel)
return j6_err_invalid_arg;
channel *c = static_cast<channel*>(o);
return c->enqueue(len, data);
}
j6_status_t
channel_receive(j6_handle_t handle, size_t *len, void *data)
{
scheduler &s = scheduler::get();
TCB *tcb = s.current();
thread *parent = thread::from_tcb(tcb);
process &p = parent->parent();
kobject *o = p.lookup_handle(handle);
if (!o || o->get_type() != kobject::type::channel)
return j6_err_invalid_arg;
channel *c = static_cast<channel*>(o);
return c->dequeue(len, data);
}
} // namespace syscalls