source: example/io/tty-echo.cfa @ c8238c0

ADTast-experimental
Last change on this file since c8238c0 was 376946e, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added io example for using non-blocking ttys.
(Won't work without io_uring)

  • Property mode set to 100644
File size: 1.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// io.cfa --
8//
9// Author           : Thierry Delisle
10// Created On       : Thu Feb 18 20:31:00 2021
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#include <fstream.hfa>
17#include <iofwd.hfa>
18#include <time.hfa>
19#include <thread.hfa>
20
21thread EchoThread {};
22void main(EchoThread & this) {
23        char buffer[100];
24        for() {
25                int ret = cfa_read(0, buffer + 1, 97, 0);
26                if(ret < 0) {
27                        serr | "Failed to read from fd 0";
28                        exit(1);
29                }
30                if(ret == 0) return;
31                buffer[0]     = '\'';
32                buffer[ret]   = '\'';
33                buffer[ret+1] = '\n';
34                ret = cfa_write(1, buffer, ret + 2, 0);
35                if(ret < 0) {
36                        serr | "Failed to write from fd 1 (ET)";
37                        exit(1);
38                }
39        }
40}
41
42thread HeartBeatThread {};
43void main(HeartBeatThread & this) {
44        for() {
45                waitfor( ^?{} : this) {
46                        return;
47                }
48                or else;
49                int ret = cfa_write(1, ".", 1, 0);
50                if(ret < 0) {
51                        serr | "Failed to write from fd 1 (ET)";
52                        exit(1);
53                }
54                sleep(5`s);
55        }
56}
57
58int main() {
59        HeartBeatThread hb;
60        {
61                EchoThread et;
62        }
63}
Note: See TracBrowser for help on using the repository browser.