Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
08_pipe.c File Reference
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <serene/rt/fiber/io.h>
#include <serene/runtime.h>
Include dependency graph for 08_pipe.c:

Go to the source code of this file.

Data Structures

struct  writer_args_t
struct  reader_args_t

Functions

static void set_nonblock (int fd)
static srn_fiber_result_t writer (srn_context_t *ctx, void *arg)
static srn_fiber_result_t reader (srn_context_t *ctx, void *arg)
int main (void)

Function Documentation

◆ main()

int main ( void )

Definition at line 83 of file 08_pipe.c.

83 {
84 SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, nullptr);
85 srn_context_t *ctx = srn_context_make(engine);
86
87 int fds[2];
88 if (pipe(fds) != 0) {
89 perror("pipe");
90 return 1;
91 }
92 set_nonblock(fds[0]);
93 set_nonblock(fds[1]);
94
95 char buf[64] = {0};
96 writer_args_t w = {fds[1], "hello over a pipe"};
97 reader_args_t r = {fds[0], buf, sizeof(buf)};
98 (void)srn_fiber_spawn(ctx, writer, &w);
99 (void)srn_fiber_spawn(ctx, reader, &r);
100
101 srn_sched_run(sched, 1);
102
105 return 0;
106}
static void set_nonblock(int fd)
Definition 08_pipe.c:51
static srn_fiber_result_t writer(srn_context_t *ctx, void *arg)
Definition 08_pipe.c:56
static srn_fiber_result_t reader(srn_context_t *ctx, void *arg)
Definition 08_pipe.c:69
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:39
int srn_context_release(srn_context_t *ctx)
Definition context.c:64
srn_fiber_t * srn_fiber_spawn(srn_context_t *ctx, srn_fiber_entry_t entry, void *arg)
Make and schedule a fiber with every default, the engine's scheduler, the configured stack size,...
Definition fiber.c:247
#define SERENE_RUNTIME_SHUTDOWN(engine)
Tear down what SERENE_RUNTIME_INIT brought up, in reverse order, the engine (reactor,...
Definition runtime.h:58
#define SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, config)
SERENE_RUNTIME_INIT plus a sched variable bound to the engine's scheduler, which every run and fiber ...
Definition runtime.h:46
void srn_sched_run(srn_scheduler_t *sched, size_t nworkers)
Run the scheduler with nworkers os threads draining it, returning once the pool goes quiescent (every...
Definition scheduler.c:843
Here is the call graph for this function:

◆ reader()

srn_fiber_result_t reader ( srn_context_t * ctx,
void * arg )
static

Definition at line 69 of file 08_pipe.c.

69 {
70 (void)ctx;
71 reader_args_t *rd = arg;
72 srn_io_size_result_t r = srn_fiber_read(rd->fd, rd->buf, rd->cap - 1, -1);
73 if (HAS_ERROR(r)) {
74 printf("reader: error: %s\n", r.maybe_error->msg);
75 } else {
76 rd->buf[r.count] = '\0';
77 printf("reader: read %zu bytes: \"%s\"\n", r.count, rd->buf);
78 }
79 close(rd->fd);
80 return nullptr;
81}
#define HAS_ERROR(x)
True when x – a value carrying an ERROR_HEADER – has an error attached.
Definition errors.h:63
srn_io_size_result_t srn_fiber_read(int fd, void *buf, size_t len, int64_t offset)
Read up to len bytes from fd into buf, suspending the calling fiber until the operation completes rat...
Definition io.c:284
char * buf
Definition 08_pipe.c:47
size_t cap
Definition 08_pipe.c:48
The result of a byte transfer op (read/write/sendto/recvmsg/sendmsg).
Definition io.h:76
size_t count
Definition io.h:78
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_nonblock()

void set_nonblock ( int fd)
static

Definition at line 51 of file 08_pipe.c.

51 {
52 int fl = fcntl(fd, F_GETFL, 0);
53 (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
54}
Here is the caller graph for this function:

◆ writer()

srn_fiber_result_t writer ( srn_context_t * ctx,
void * arg )
static

Definition at line 56 of file 08_pipe.c.

56 {
57 (void)ctx;
58 writer_args_t *w = arg;
59 srn_io_size_result_t r = srn_fiber_write(w->fd, w->msg, strlen(w->msg), -1);
60 if (HAS_ERROR(r)) {
61 printf("writer: error: %s\n", r.maybe_error->msg);
62 } else {
63 printf("writer: wrote %zu bytes\n", r.count);
64 }
65 close(w->fd); // signals EOF to the reader once the bytes are drained
66 return nullptr;
67}
srn_io_size_result_t srn_fiber_write(int fd, const void *buf, size_t len, int64_t offset)
Write len bytes from buf to fd, suspending the calling fiber until the operation completes rather tha...
Definition io.c:296
const char * msg
Definition 08_pipe.c:42
Here is the call graph for this function:
Here is the caller graph for this function: