Simple IPC with pipes
This commit is contained in:
47
kernel/ipc/pipe/pipe.c
Normal file
47
kernel/ipc/pipe/pipe.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "rbuf/rbuf.h"
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "hal/hal.h"
|
||||
#include "dlmalloc/malloc.h"
|
||||
#include "errors.h"
|
||||
#include "pipe.h"
|
||||
|
||||
int32_t ipc_pipeinit(IpcPipe *pipe) {
|
||||
hal_memset(pipe, 0, sizeof(*pipe));
|
||||
spinlock_init(&pipe->spinlock);
|
||||
pipe->rbuf.buffer = dlmalloc(IPC_PIPE_MAX);
|
||||
if (pipe->rbuf.buffer == NULL) {
|
||||
return E_NOMEMORY;
|
||||
}
|
||||
pipe->rbuf.cap = IPC_PIPE_MAX;
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
void ipc_pipefree(IpcPipe *pipe) {
|
||||
dlfree(pipe->rbuf.buffer);
|
||||
}
|
||||
|
||||
int32_t ipc_pipewrite(IpcPipe *pipe, const uint8_t *const buffer, size_t n) {
|
||||
size_t i = 0;
|
||||
spinlock_acquire(&pipe->spinlock);
|
||||
for (; i < n; i++) {
|
||||
if (rbuf_push(&pipe->rbuf, buffer[i]) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
spinlock_release(&pipe->spinlock);
|
||||
return i;
|
||||
}
|
||||
|
||||
int32_t ipc_piperead(IpcPipe *pipe, uint8_t *const buffer, size_t n) {
|
||||
size_t i = 0;
|
||||
spinlock_acquire(&pipe->spinlock);
|
||||
for (; i < n; i++) {
|
||||
if (rbuf_pop(&pipe->rbuf, &buffer[i]) < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
spinlock_release(&pipe->spinlock);
|
||||
return i;
|
||||
}
|
21
kernel/ipc/pipe/pipe.h
Normal file
21
kernel/ipc/pipe/pipe.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef IPC_PIPE_PIPE_H_
|
||||
#define IPC_PIPE_PIPE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "rbuf/rbuf.h"
|
||||
#include "spinlock/spinlock.h"
|
||||
|
||||
#define IPC_PIPE_MAX 0x1000
|
||||
|
||||
typedef struct {
|
||||
RBuf rbuf;
|
||||
SpinLock spinlock;
|
||||
} IpcPipe;
|
||||
|
||||
int32_t ipc_pipeinit(IpcPipe *pipe);
|
||||
void ipc_pipefree(IpcPipe *pipe);
|
||||
int32_t ipc_pipewrite(IpcPipe *pipe, const uint8_t *const buffer, size_t n);
|
||||
int32_t ipc_piperead(IpcPipe *pipe, uint8_t *const buffer, size_t n);
|
||||
|
||||
#endif // IPC_PIPE_PIPE_H_
|
Reference in New Issue
Block a user