Codemod asserts to assertxs in the runtime
[hiphop-php.git] / hphp / runtime / base / file-await.cpp
blobd989b0ae4babbd7bdd8aca1d31c6cbe1fd516670
1 #include "hphp/runtime/base/file-await.h"
2 #include "hphp/runtime/base/file.h"
3 #include "hphp/runtime/ext/asio/ext_static-wait-handle.h"
5 #include "hphp/util/compatibility.h"
7 namespace HPHP {
8 /////////////////////////////////////////////////////////////////////////////
10 void FileTimeoutHandler::timeoutExpired() noexcept {
11 if (m_fileAwait) {
12 m_fileAwait->setFinished(FileAwait::TIMEOUT);
16 void FileEventHandler::handlerReady(uint16_t events) noexcept {
17 if (m_fileAwait) {
18 m_fileAwait->setFinished(events ? FileAwait::READY : FileAwait::CLOSED);
22 /////////////////////////////////////////////////////////////////////////////
24 FileAwait::FileAwait(int fd, uint16_t events, double timeout) {
25 assertx(fd >= 0);
26 assertx(events & FileEventHandler::READ_WRITE);
28 auto asio_event_base = getSingleton<AsioEventBase>();
29 m_file = std::make_shared<FileEventHandler>(asio_event_base.get(), fd, this);
30 m_file->registerHandler(events);
32 int64_t timeout_ms = timeout * 1000.0;
33 if (timeout_ms > 0) {
34 m_timeout = std::make_shared<FileTimeoutHandler>(asio_event_base.get(),
35 this);
36 asio_event_base->runInEventBaseThreadAndWait([this,timeout_ms] {
37 if (m_timeout) {
38 m_timeout->scheduleTimeout(timeout_ms);
40 });
44 FileAwait::~FileAwait() {
45 if (m_file) {
46 // Avoid possible race condition
47 m_file->m_fileAwait = nullptr;
49 m_file->unregisterHandler();
50 m_file.reset();
52 if (m_timeout) {
53 // Avoid race condition, we may (likely) finish destructing
54 // before the timeout cancels
55 m_timeout->m_fileAwait = nullptr;
57 auto to = std::move(m_timeout);
58 getSingleton<AsioEventBase>()->runInEventBaseThreadAndWait([to] {
59 to->cancelTimeout();
60 });
64 void FileAwait::unserialize(Cell& c) {
65 c.m_type = KindOfInt64;
66 c.m_data.num = m_result;
69 void FileAwait::setFinished(int64_t status) {
70 if (status > m_result) {
71 m_result = status;
73 if (!m_finished) {
74 markAsFinished();
75 m_finished = true;
79 /////////////////////////////////////////////////////////////////////////////
81 Object File::await(uint16_t events, double timeout) {
82 if (isClosed()) {
83 Cell closedResult;
84 closedResult.m_type = KindOfInt64;
85 closedResult.m_data.num = FileAwait::CLOSED;
86 return Object{c_StaticWaitHandle::CreateSucceeded(closedResult)};
88 if (fd() < 0) {
89 SystemLib::throwExceptionObject(
90 "Unable to await on stream, invalid file descriptor");
92 events = events & FileEventHandler::READ_WRITE;
93 if (!events) {
94 SystemLib::throwExceptionObject(
95 "Must await for reading, writing, or both.");
98 auto ev = new FileAwait(fd(), events, timeout);
99 try {
100 return Object{ev->getWaitHandle()};
101 } catch (...) {
102 assertx(false);
103 ev->abandon();
104 throw;
108 /////////////////////////////////////////////////////////////////////////////
109 } // namespace HPHP