annotate win64-msvc/include/kj/async-unix.h @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents b4bfdf10c4b3
children
rev   line source
cannam@148 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@148 2 // Licensed under the MIT License:
cannam@148 3 //
cannam@148 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@148 5 // of this software and associated documentation files (the "Software"), to deal
cannam@148 6 // in the Software without restriction, including without limitation the rights
cannam@148 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@148 8 // copies of the Software, and to permit persons to whom the Software is
cannam@148 9 // furnished to do so, subject to the following conditions:
cannam@148 10 //
cannam@148 11 // The above copyright notice and this permission notice shall be included in
cannam@148 12 // all copies or substantial portions of the Software.
cannam@148 13 //
cannam@148 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@148 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@148 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@148 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@148 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@148 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@148 20 // THE SOFTWARE.
cannam@148 21
cannam@148 22 #ifndef KJ_ASYNC_UNIX_H_
cannam@148 23 #define KJ_ASYNC_UNIX_H_
cannam@148 24
cannam@148 25 #if _WIN32
cannam@148 26 #error "This file is Unix-specific. On Windows, include async-win32.h instead."
cannam@148 27 #endif
cannam@148 28
cannam@148 29 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@148 30 #pragma GCC system_header
cannam@148 31 #endif
cannam@148 32
cannam@148 33 #include "async.h"
cannam@148 34 #include "time.h"
cannam@148 35 #include "vector.h"
cannam@148 36 #include "io.h"
cannam@148 37 #include <signal.h>
cannam@148 38
cannam@148 39 #if __linux__ && !__BIONIC__ && !defined(KJ_USE_EPOLL)
cannam@148 40 // Default to epoll on Linux, except on Bionic (Android) which doesn't have signalfd.h.
cannam@148 41 #define KJ_USE_EPOLL 1
cannam@148 42 #endif
cannam@148 43
cannam@148 44 namespace kj {
cannam@148 45
cannam@148 46 class UnixEventPort: public EventPort {
cannam@148 47 // An EventPort implementation which can wait for events on file descriptors as well as signals.
cannam@148 48 // This API only makes sense on Unix.
cannam@148 49 //
cannam@148 50 // The implementation uses `poll()` or possibly a platform-specific API (e.g. epoll, kqueue).
cannam@148 51 // To also wait on signals without race conditions, the implementation may block signals until
cannam@148 52 // just before `poll()` while using a signal handler which `siglongjmp()`s back to just before
cannam@148 53 // the signal was unblocked, or it may use a nicer platform-specific API like signalfd.
cannam@148 54 //
cannam@148 55 // The implementation reserves a signal for internal use. By default, it uses SIGUSR1. If you
cannam@148 56 // need to use SIGUSR1 for something else, you must offer a different signal by calling
cannam@148 57 // setReservedSignal() at startup.
cannam@148 58 //
cannam@148 59 // WARNING: A UnixEventPort can only be used in the thread and process that created it. In
cannam@148 60 // particular, note that after a fork(), a UnixEventPort created in the parent process will
cannam@148 61 // not work correctly in the child, even if the parent ceases to use its copy. In particular
cannam@148 62 // note that this means that server processes which daemonize themselves at startup must wait
cannam@148 63 // until after daemonization to create a UnixEventPort.
cannam@148 64
cannam@148 65 public:
cannam@148 66 UnixEventPort();
cannam@148 67 ~UnixEventPort() noexcept(false);
cannam@148 68
cannam@148 69 class FdObserver;
cannam@148 70 // Class that watches an fd for readability or writability. See definition below.
cannam@148 71
cannam@148 72 Promise<siginfo_t> onSignal(int signum);
cannam@148 73 // When the given signal is delivered to this thread, return the corresponding siginfo_t.
cannam@148 74 // The signal must have been captured using `captureSignal()`.
cannam@148 75 //
cannam@148 76 // If `onSignal()` has not been called, the signal will remain blocked in this thread.
cannam@148 77 // Therefore, a signal which arrives before `onSignal()` was called will not be "missed" -- the
cannam@148 78 // next call to 'onSignal()' will receive it. Also, you can control which thread receives a
cannam@148 79 // process-wide signal by only calling `onSignal()` on that thread's event loop.
cannam@148 80 //
cannam@148 81 // The result of waiting on the same signal twice at once is undefined.
cannam@148 82
cannam@148 83 static void captureSignal(int signum);
cannam@148 84 // Arranges for the given signal to be captured and handled via UnixEventPort, so that you may
cannam@148 85 // then pass it to `onSignal()`. This method is static because it registers a signal handler
cannam@148 86 // which applies process-wide. If any other threads exist in the process when `captureSignal()`
cannam@148 87 // is called, you *must* set the signal mask in those threads to block this signal, otherwise
cannam@148 88 // terrible things will happen if the signal happens to be delivered to those threads. If at
cannam@148 89 // all possible, call `captureSignal()` *before* creating threads, so that threads you create in
cannam@148 90 // the future will inherit the proper signal mask.
cannam@148 91 //
cannam@148 92 // To un-capture a signal, simply install a different signal handler and then un-block it from
cannam@148 93 // the signal mask.
cannam@148 94
cannam@148 95 static void setReservedSignal(int signum);
cannam@148 96 // Sets the signal number which `UnixEventPort` reserves for internal use. If your application
cannam@148 97 // needs to use SIGUSR1, call this at startup (before any calls to `captureSignal()` and before
cannam@148 98 // constructing an `UnixEventPort`) to offer a different signal.
cannam@148 99
cannam@148 100 Timer& getTimer() { return timerImpl; }
cannam@148 101
cannam@148 102 // implements EventPort ------------------------------------------------------
cannam@148 103 bool wait() override;
cannam@148 104 bool poll() override;
cannam@148 105 void wake() const override;
cannam@148 106
cannam@148 107 private:
cannam@148 108 struct TimerSet; // Defined in source file to avoid STL include.
cannam@148 109 class TimerPromiseAdapter;
cannam@148 110 class SignalPromiseAdapter;
cannam@148 111
cannam@148 112 TimerImpl timerImpl;
cannam@148 113
cannam@148 114 SignalPromiseAdapter* signalHead = nullptr;
cannam@148 115 SignalPromiseAdapter** signalTail = &signalHead;
cannam@148 116
cannam@148 117 TimePoint readClock();
cannam@148 118 void gotSignal(const siginfo_t& siginfo);
cannam@148 119
cannam@148 120 friend class TimerPromiseAdapter;
cannam@148 121
cannam@148 122 #if KJ_USE_EPOLL
cannam@148 123 AutoCloseFd epollFd;
cannam@148 124 AutoCloseFd signalFd;
cannam@148 125 AutoCloseFd eventFd; // Used for cross-thread wakeups.
cannam@148 126
cannam@148 127 sigset_t signalFdSigset;
cannam@148 128 // Signal mask as currently set on the signalFd. Tracked so we can detect whether or not it
cannam@148 129 // needs updating.
cannam@148 130
cannam@148 131 bool doEpollWait(int timeout);
cannam@148 132
cannam@148 133 #else
cannam@148 134 class PollContext;
cannam@148 135
cannam@148 136 FdObserver* observersHead = nullptr;
cannam@148 137 FdObserver** observersTail = &observersHead;
cannam@148 138
cannam@148 139 unsigned long long threadId; // actually pthread_t
cannam@148 140 #endif
cannam@148 141 };
cannam@148 142
cannam@148 143 class UnixEventPort::FdObserver {
cannam@148 144 // Object which watches a file descriptor to determine when it is readable or writable.
cannam@148 145 //
cannam@148 146 // For listen sockets, "readable" means that there is a connection to accept(). For everything
cannam@148 147 // else, it means that read() (or recv()) will return data.
cannam@148 148 //
cannam@148 149 // The presence of out-of-band data should NOT fire this event. However, the event may
cannam@148 150 // occasionally fire spuriously (when there is actually no data to read), and one thing that can
cannam@148 151 // cause such spurious events is the arrival of OOB data on certain platforms whose event
cannam@148 152 // interfaces fail to distinguish between regular and OOB data (e.g. Mac OSX).
cannam@148 153 //
cannam@148 154 // WARNING: The exact behavior of this class differs across systems, since event interfaces
cannam@148 155 // vary wildly. Be sure to read the documentation carefully and avoid depending on unspecified
cannam@148 156 // behavior. If at all possible, use the higher-level AsyncInputStream interface instead.
cannam@148 157
cannam@148 158 public:
cannam@148 159 enum Flags {
cannam@148 160 OBSERVE_READ = 1,
cannam@148 161 OBSERVE_WRITE = 2,
cannam@148 162 OBSERVE_URGENT = 4,
cannam@148 163 OBSERVE_READ_WRITE = OBSERVE_READ | OBSERVE_WRITE
cannam@148 164 };
cannam@148 165
cannam@148 166 FdObserver(UnixEventPort& eventPort, int fd, uint flags);
cannam@148 167 // Begin watching the given file descriptor for readability. Only one ReadObserver may exist
cannam@148 168 // for a given file descriptor at a time.
cannam@148 169
cannam@148 170 ~FdObserver() noexcept(false);
cannam@148 171
cannam@148 172 KJ_DISALLOW_COPY(FdObserver);
cannam@148 173
cannam@148 174 Promise<void> whenBecomesReadable();
cannam@148 175 // Resolves the next time the file descriptor transitions from having no data to read to having
cannam@148 176 // some data to read.
cannam@148 177 //
cannam@148 178 // KJ uses "edge-triggered" event notification whenever possible. As a result, it is an error
cannam@148 179 // to call this method when there is already data in the read buffer which has been there since
cannam@148 180 // prior to the last turn of the event loop or prior to creation FdWatcher. In this case, it is
cannam@148 181 // unspecified whether the promise will ever resolve -- it depends on the underlying event
cannam@148 182 // mechanism being used.
cannam@148 183 //
cannam@148 184 // In order to avoid this problem, make sure that you only call `whenBecomesReadable()`
cannam@148 185 // only at times when you know the buffer is empty. You know this for sure when one of the
cannam@148 186 // following happens:
cannam@148 187 // * read() or recv() fails with EAGAIN or EWOULDBLOCK. (You MUST have non-blocking mode
cannam@148 188 // enabled on the fd!)
cannam@148 189 // * The file descriptor is a regular byte-oriented object (like a socket or pipe),
cannam@148 190 // read() or recv() returns fewer than the number of bytes requested, and `atEndHint()`
cannam@148 191 // returns false. This can only happen if the buffer is empty but EOF is not reached. (Note,
cannam@148 192 // though, that for record-oriented file descriptors like Linux's inotify interface, this
cannam@148 193 // rule does not hold, because it could simply be that the next record did not fit into the
cannam@148 194 // space available.)
cannam@148 195 //
cannam@148 196 // It is an error to call `whenBecomesReadable()` again when the promise returned previously
cannam@148 197 // has not yet resolved. If you do this, the previous promise may throw an exception.
cannam@148 198
cannam@148 199 inline Maybe<bool> atEndHint() { return atEnd; }
cannam@148 200 // Returns true if the event system has indicated that EOF has been received. There may still
cannam@148 201 // be data in the read buffer, but once that is gone, there's nothing left.
cannam@148 202 //
cannam@148 203 // Returns false if the event system has indicated that EOF had NOT been received as of the
cannam@148 204 // last turn of the event loop.
cannam@148 205 //
cannam@148 206 // Returns nullptr if the event system does not know whether EOF has been reached. In this
cannam@148 207 // case, the only way to know for sure is to call read() or recv() and check if it returns
cannam@148 208 // zero.
cannam@148 209 //
cannam@148 210 // This hint may be useful as an optimization to avoid an unnecessary system call.
cannam@148 211
cannam@148 212 Promise<void> whenBecomesWritable();
cannam@148 213 // Resolves the next time the file descriptor transitions from having no space available in the
cannam@148 214 // write buffer to having some space available.
cannam@148 215 //
cannam@148 216 // KJ uses "edge-triggered" event notification whenever possible. As a result, it is an error
cannam@148 217 // to call this method when there is already space in the write buffer which has been there
cannam@148 218 // since prior to the last turn of the event loop or prior to creation FdWatcher. In this case,
cannam@148 219 // it is unspecified whether the promise will ever resolve -- it depends on the underlying
cannam@148 220 // event mechanism being used.
cannam@148 221 //
cannam@148 222 // In order to avoid this problem, make sure that you only call `whenBecomesWritable()`
cannam@148 223 // only at times when you know the buffer is full. You know this for sure when one of the
cannam@148 224 // following happens:
cannam@148 225 // * write() or send() fails with EAGAIN or EWOULDBLOCK. (You MUST have non-blocking mode
cannam@148 226 // enabled on the fd!)
cannam@148 227 // * write() or send() succeeds but accepts fewer than the number of bytes provided. This can
cannam@148 228 // only happen if the buffer is full.
cannam@148 229 //
cannam@148 230 // It is an error to call `whenBecomesWritable()` again when the promise returned previously
cannam@148 231 // has not yet resolved. If you do this, the previous promise may throw an exception.
cannam@148 232
cannam@148 233 Promise<void> whenUrgentDataAvailable();
cannam@148 234 // Resolves the next time the file descriptor's read buffer contains "urgent" data.
cannam@148 235 //
cannam@148 236 // The conditions for availability of urgent data are specific to the file descriptor's
cannam@148 237 // underlying implementation.
cannam@148 238 //
cannam@148 239 // It is an error to call `whenUrgentDataAvailable()` again when the promise returned previously
cannam@148 240 // has not yet resolved. If you do this, the previous promise may throw an exception.
cannam@148 241 //
cannam@148 242 // WARNING: This has some known weird behavior on macOS. See
cannam@148 243 // https://github.com/sandstorm-io/capnproto/issues/374.
cannam@148 244
cannam@148 245 private:
cannam@148 246 UnixEventPort& eventPort;
cannam@148 247 int fd;
cannam@148 248 uint flags;
cannam@148 249
cannam@148 250 kj::Maybe<Own<PromiseFulfiller<void>>> readFulfiller;
cannam@148 251 kj::Maybe<Own<PromiseFulfiller<void>>> writeFulfiller;
cannam@148 252 kj::Maybe<Own<PromiseFulfiller<void>>> urgentFulfiller;
cannam@148 253 // Replaced each time `whenBecomesReadable()` or `whenBecomesWritable()` is called. Reverted to
cannam@148 254 // null every time an event is fired.
cannam@148 255
cannam@148 256 Maybe<bool> atEnd;
cannam@148 257
cannam@148 258 void fire(short events);
cannam@148 259
cannam@148 260 #if !KJ_USE_EPOLL
cannam@148 261 FdObserver* next;
cannam@148 262 FdObserver** prev;
cannam@148 263 // Linked list of observers which currently have a non-null readFulfiller or writeFulfiller.
cannam@148 264 // If `prev` is null then the observer is not currently in the list.
cannam@148 265
cannam@148 266 short getEventMask();
cannam@148 267 #endif
cannam@148 268
cannam@148 269 friend class UnixEventPort;
cannam@148 270 };
cannam@148 271
cannam@148 272 } // namespace kj
cannam@148 273
cannam@148 274 #endif // KJ_ASYNC_UNIX_H_