annotate osx/include/kj/async-unix.h @ 146:206f0eb279b8

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