mirror of
https://github.com/matrix-construct/construct
synced 2025-03-17 06:50:23 +01:00
ircd::fs: Add a wait() interface.
This commit is contained in:
parent
3bed48011d
commit
6ff3c88fa4
4 changed files with 138 additions and 0 deletions
|
@ -55,6 +55,7 @@ namespace ircd::fs
|
|||
#include "dev.h"
|
||||
#include "fd.h"
|
||||
#include "aio.h"
|
||||
#include "wait.h"
|
||||
#include "read.h"
|
||||
#include "write.h"
|
||||
#include "sync.h"
|
||||
|
|
|
@ -33,4 +33,5 @@ enum class ircd::fs::op
|
|||
READ = 1,
|
||||
WRITE = 2,
|
||||
SYNC = 3,
|
||||
WAIT = 4,
|
||||
};
|
||||
|
|
46
include/ircd/fs/wait.h
Normal file
46
include/ircd/fs/wait.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_FS_WAIT_H
|
||||
|
||||
namespace ircd::fs
|
||||
{
|
||||
enum class ready :uint8_t;
|
||||
struct wait_opts extern const wait_opts_default;
|
||||
|
||||
string_view reflect(const ready &);
|
||||
|
||||
void wait(const fd &, const wait_opts &);
|
||||
}
|
||||
|
||||
enum class ircd::fs::ready
|
||||
:uint8_t
|
||||
{
|
||||
ANY, ///< Wait for anything.
|
||||
READ, ///< Ready for read().
|
||||
WRITE, ///< Ready for write().
|
||||
ERROR, ///< Has error.
|
||||
};
|
||||
|
||||
/// Options for a write operation
|
||||
struct ircd::fs::wait_opts
|
||||
:opts
|
||||
{
|
||||
enum ready ready;
|
||||
|
||||
wait_opts(const enum ready &ready = ready::ANY);
|
||||
};
|
||||
|
||||
inline
|
||||
ircd::fs::wait_opts::wait_opts(const enum ready &ready)
|
||||
:opts{0, op::WAIT}
|
||||
,ready{ready}
|
||||
{}
|
90
ircd/fs.cc
90
ircd/fs.cc
|
@ -1034,6 +1034,95 @@ ircd::fs::flags(const write_opts &opts)
|
|||
return ret;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// fs/wait.h
|
||||
//
|
||||
|
||||
namespace ircd::fs
|
||||
{
|
||||
static asio::posix::stream_descriptor::wait_type translate(const ready &);
|
||||
}
|
||||
|
||||
decltype(ircd::fs::wait_opts_default)
|
||||
ircd::fs::wait_opts_default;
|
||||
|
||||
void
|
||||
ircd::fs::wait(const fd &fd,
|
||||
const wait_opts &opts)
|
||||
{
|
||||
assert(opts.op == op::WAIT);
|
||||
|
||||
const auto &wait_type
|
||||
{
|
||||
translate(opts.ready)
|
||||
};
|
||||
|
||||
boost::asio::posix::stream_descriptor sd
|
||||
{
|
||||
ios::get(), int(fd)
|
||||
};
|
||||
|
||||
const unwind release{[&sd]
|
||||
{
|
||||
sd.release();
|
||||
}};
|
||||
|
||||
const auto interruption{[&sd]
|
||||
(ctx::ctx *const &interruptor)
|
||||
{
|
||||
sd.cancel();
|
||||
}};
|
||||
|
||||
boost::system::error_code ec; continuation
|
||||
{
|
||||
continuation::asio_predicate, interruption, [&wait_type, &sd, &ec]
|
||||
(auto &yield)
|
||||
{
|
||||
sd.async_wait(wait_type, yield[ec]);
|
||||
}
|
||||
};
|
||||
|
||||
if(unlikely(ec))
|
||||
throw_system_error(ec);
|
||||
}
|
||||
|
||||
boost::asio::posix::stream_descriptor::wait_type
|
||||
ircd::fs::translate(const ready &ready)
|
||||
{
|
||||
using wait_type = boost::asio::posix::stream_descriptor::wait_type;
|
||||
|
||||
switch(ready)
|
||||
{
|
||||
case ready::ANY:
|
||||
return wait_type::wait_read | wait_type::wait_write | wait_type::wait_error;
|
||||
|
||||
case ready::READ:
|
||||
return wait_type::wait_read;
|
||||
|
||||
case ready::WRITE:
|
||||
return wait_type::wait_write;
|
||||
|
||||
case ready::ERROR:
|
||||
default:
|
||||
return wait_type::wait_error;
|
||||
}
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::reflect(const ready &ready)
|
||||
{
|
||||
switch(ready)
|
||||
{
|
||||
case ready::ANY: return "ANY";
|
||||
case ready::READ: return "READ";
|
||||
case ready::WRITE: return "WRITE";
|
||||
case ready::ERROR: return "ERROR";
|
||||
}
|
||||
|
||||
return "?????";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// fs/aio.h
|
||||
|
@ -1425,6 +1514,7 @@ ircd::fs::reflect(const op &op)
|
|||
case op::READ: return "READ";
|
||||
case op::WRITE: return "WRITE";
|
||||
case op::SYNC: return "SYNC";
|
||||
case op::WAIT: return "WAIT";
|
||||
}
|
||||
|
||||
return "????";
|
||||
|
|
Loading…
Add table
Reference in a new issue