Debian Codegraph @

examples/cat-aio.c

raw symbols SCIP
1 /*
2 * cat-aio.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include <unistd.h>
23
24 #include "cat-util.h"
25
26 /* NOTE:
27 *
28 * `cat` from coreutils is certainly faster than this, especially if you're
29 * doing things like `./examples/cat foo > bar` as it will use
30 * copy_file_range() to avoid reading into userspace.
31 *
32 * `gio cat` is likely faster than this doing synchronous IO on the calling
33 * thread because it doesn't have to coordinate across thread pools.
34 */
35
36 static DexFuture *
37 cat_read_fiber (gpointer user_data)
38 {
39 Cat *cat = user_data;
40 Buffer *buffer = NULL;
41
42 for (;;)
43 {
44 Buffer *next;
45
46 /* Suspend while sending the buffer to the channel, which may
47 * help throttle reads if they get too far ahead of writes.
48 */
49 if (buffer != NULL)
50 dex_await (dex_channel_send (cat->channel,
51 dex_future_new_for_pointer (g_steal_pointer (&buffer))),
52 NULL);
53
54 /* Get next buffer from the pool */
55 next = cat_pop_buffer (cat);
56
57 /* Suspend while reading into the buffer */
58 next->length = dex_await_int64 (dex_aio_read (NULL,
59 cat->read_fd,
60 next->data,
61 next->capacity,
62 -1),
63 NULL);
64
65 /* If we got length <= 0, we failed or finished */
66 if (next->length <= 0)
67 {
68 dex_channel_close_send (cat->channel);
69 cat_push_buffer (cat, next);
70 break;
71 }
72
73 buffer = next;
74 }
75
76 return dex_future_new_for_boolean (TRUE);
77 }
78
79 static DexFuture *
80 cat_write_fiber (gpointer user_data)
81 {
82 Cat *cat = user_data;
83
84 for (;;)
85 {
86 Buffer *buffer;
87 gssize len;
88
89 /* Suspend while we wait for another buffer (or error on channel closed) */
90 if (!(buffer = dex_await_pointer (dex_channel_receive (cat->channel), NULL)))
91 break;
92
93 /* Suspend while we write the buffer contents to output stream */
94 len = dex_await_int64 (dex_aio_write (NULL,
95 cat->write_fd,
96 buffer->data,
97 buffer->length,
98 -1),
99 NULL);
100
101 /* Give the buffer back to the pool */
102 cat_push_buffer (cat, buffer);
103
104 /* Bail if we got a failure or empty buffer */
105 if (len <= 0)
106 break;
107 }
108
109 return dex_future_new_for_boolean (TRUE);
110 }
111
112 int
113 main (int argc,
114 char *argv[])
115 {
116 GError *error = NULL;
117 Cat cat;
118 int ret = EXIT_SUCCESS;
119
120 dex_init ();
121
122 if (!cat_init (&cat, &argc, &argv, &error) ||
123 !cat_run (&cat,
124 dex_scheduler_spawn (NULL, 0, cat_read_fiber, &cat, NULL),
125 dex_scheduler_spawn (NULL, 0, cat_write_fiber, &cat, NULL),
126 &error))
127 {
128 g_printerr ("cat: %s\n", error->message);
129 g_clear_error (&error);
130 ret = EXIT_FAILURE;
131 }
132
133 cat_clear (&cat);
134
135 return ret;
136 }