Path: blob/main/system/lib/wasmfs/pipe_backend.h
6174 views
// Copyright 2022 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#pragma once67#include <queue>89#include "file.h"10#include "support.h"1112namespace wasmfs {1314// The data shared between the two sides of a pipe.15// TODO: Consider switching to a ring buffer for performance and code size if we16// don't need unbounded pipes.17using PipeData = std::queue<uint8_t>;1819// A PipeFile is a simple file that has a reference to a PipeData that it20// either reads from or writes to. A pair of PipeFiles comprise the two ends of21// a pipe.22class PipeFile : public DataFile {23std::shared_ptr<PipeData> data;2425int open(oflags_t) override { return 0; }26int close() override { return 0; }2728ssize_t write(const uint8_t* buf, size_t len, off_t offset) override {29for (size_t i = 0; i < len; i++) {30data->push(buf[i]);31}32return len;33}3435ssize_t read(uint8_t* buf, size_t len, off_t offset) override {36for (size_t i = 0; i < len; i++) {37if (data->empty()) {38return i;39}40buf[i] = data->front();41data->pop();42}43return len;44}4546int flush() override { return 0; }4748off_t getSize() override { return data->size(); }4950// TODO: Should this return an error?51int setSize(off_t size) override { return 0; }5253public:54// PipeFiles do not have or need a backend. Pass NullBackend to the parent for55// that.56PipeFile(mode_t mode, std::shared_ptr<PipeData> data)57: DataFile(mode, NullBackend), data(data) {58// Reads are always from the front; writes always to the end.59seekable = false;60}61};62} // namespace wasmfs636465