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