mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 08:24:32 -08:00
[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:
@@ -5,6 +5,10 @@
|
||||
#include "j6/errors.h"
|
||||
#include "j6/signals.h"
|
||||
|
||||
const char message[] = "Hello! This is a message being sent over a channel!";
|
||||
char inbuf[1024];
|
||||
j6_handle_t chan = j6_handle_invalid;
|
||||
|
||||
extern "C" {
|
||||
j6_status_t system_log(const char *msg);
|
||||
|
||||
@@ -17,6 +21,11 @@ extern "C" {
|
||||
j6_status_t thread_sleep(uint64_t til);
|
||||
j6_status_t thread_exit(int64_t status);
|
||||
|
||||
j6_status_t channel_create(j6_handle_t *handle);
|
||||
j6_status_t channel_close(j6_handle_t handle);
|
||||
j6_status_t channel_send(j6_handle_t handle, size_t len, void *data);
|
||||
j6_status_t channel_receive(j6_handle_t handle, size_t *len, void *data);
|
||||
|
||||
int main(int, const char **);
|
||||
}
|
||||
|
||||
@@ -24,6 +33,13 @@ void
|
||||
thread_proc()
|
||||
{
|
||||
system_log("sub thread starting");
|
||||
|
||||
j6_status_t result = channel_send(chan, sizeof(message), (void*)message);
|
||||
if (result != j6_status_ok)
|
||||
thread_exit(result);
|
||||
|
||||
system_log("sub thread sent on channel");
|
||||
|
||||
for (int i = 1; i < 5; ++i)
|
||||
thread_sleep(i*10);
|
||||
|
||||
@@ -34,21 +50,49 @@ thread_proc()
|
||||
int
|
||||
main(int argc, const char **argv)
|
||||
{
|
||||
j6_handle_t child = 0;
|
||||
j6_handle_t child = j6_handle_invalid;
|
||||
j6_signal_t out = 0;
|
||||
|
||||
system_log("main thread starting");
|
||||
|
||||
j6_status_t result = thread_create(&thread_proc, &child);
|
||||
j6_status_t result = channel_create(&chan);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
system_log("main thread created channel");
|
||||
|
||||
result = thread_create(&thread_proc, &child);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
result = object_wait(chan, j6_signal_channel_can_recv, &out);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
size_t len = sizeof(inbuf);
|
||||
result = channel_receive(chan, &len, (void*)inbuf);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
for (int i = 0; i < sizeof(message); ++i) {
|
||||
if (inbuf[i] != message[i])
|
||||
return 127;
|
||||
}
|
||||
|
||||
system_log("main thread received on channel");
|
||||
|
||||
system_log("main thread waiting on child");
|
||||
|
||||
result = object_wait(child, -1ull, &out);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
result = channel_close(chan);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
system_log("main thread closed channel");
|
||||
|
||||
system_log("main thread done, exiting");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ SYSCALL thread_create, 0x19
|
||||
SYSCALL thread_exit, 0x1a
|
||||
SYSCALL thread_pause, 0x1b
|
||||
SYSCALL thread_sleep, 0x1c
|
||||
SYSCALL channel_create, 0x20
|
||||
SYSCALL channel_close, 0x21
|
||||
SYSCALL channel_send, 0x22
|
||||
SYSCALL channel_receive, 0x23
|
||||
|
||||
global _start
|
||||
_start:
|
||||
|
||||
Reference in New Issue
Block a user