2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <boost/noncopyable.hpp>
25 #include <folly/File.h>
30 * Set of sockets that allows immediate, take-no-prisoners abort.
32 class ShutdownSocketSet : private boost::noncopyable {
35 * Create a socket set that can handle file descriptors < maxFd.
36 * The default value (256Ki) is high enough for just about all
37 * applications, even if you increased the number of file descriptors
40 explicit ShutdownSocketSet(size_t maxFd = 1 << 18);
43 * Add an already open socket to the list of sockets managed by
44 * ShutdownSocketSet. You MUST close the socket by calling
45 * ShutdownSocketSet::close (which will, as a side effect, also handle EINTR
46 * properly) and not by calling close() on the file descriptor.
51 * Remove a socket from the list of sockets managed by ShutdownSocketSet.
52 * Note that remove() might block! (which we lamely implement by
53 * sleep()-ing) in the extremely rare case that the fd is currently
59 * Close a socket managed by ShutdownSocketSet. Returns the same return code
60 * as ::close() (and sets errno accordingly).
65 * Shut down a socket. If abortive is true, we perform an abortive
66 * shutdown (send RST rather than FIN). Note that we might still end up
67 * sending FIN due to the rather interesting implementation.
69 * This is async-signal-safe and ignores errors. Obviously, subsequent
70 * read() and write() operations to the socket will fail. During normal
71 * operation, just call ::shutdown() on the socket.
73 void shutdown(int fd, bool abortive=false);
76 * Shut down all sockets managed by ShutdownSocketSet. This is
77 * async-signal-safe and ignores errors.
79 void shutdownAll(bool abortive=false);
82 void doShutdown(int fd, bool abortive);
89 // IN_USE -> (::close()) -> FREE
90 // SHUT_DOWN -> (::close()) -> FREE
91 // IN_SHUTDOWN -> MUST_CLOSE
92 // (If the socket is currently being shut down, we must defer the
93 // ::close() until the shutdown completes)
96 // IN_USE -> IN_SHUTDOWN
98 // IN_SHUTDOWN -> SHUT_DOWN
99 // MUST_CLOSE -> (::close()) -> FREE
100 enum State : uint8_t {
110 void operator()(T* ptr) const {
116 std::unique_ptr<std::atomic<uint8_t>[], Free> data_;
117 folly::File nullFile_;