Commit Graph
2140 Commits
Author SHA1 Message Date
Johannes WeissandCory Benfield 5821c53ee8 allow completely single-threaded NIO programs (#1499)
Motivation:

Usually, you spawn a new MultiThreadedEventLoopGroup to run NIO
programs. But if you're a very simple command line utility, that may not
be necessary, so why not just re-use the main thread?

Modifications:

Allow taking over threads and making them EventLoops.

Result:

Fewer threads if you want.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-11 15:33:02 +01:00
Cory Benfield 30265d69a8 Adjust names of BSDSocket option values. (#1510)
Motivation:

The names of most of the BSD socket option values were brought over into
their NIO helper properties, with their namespace removed. This vastly
increases the risk of collision, particularly for things like TCP_INFO,
which just became .info.

Modifications:

- Added the prefixes back, e.g. `.info` is now `.tcp_info`.
- Renamed socket type `.dgram` to `.datagram`, as this is the nice clean
  API and we should use nice clean names.

Result:

Better APIs
2020-05-11 15:13:58 +01:00
Gwynne Raskind add3e402b5 Add inverse of preconditionInEventLoop() and ability to add messages to the assertion (#1508)
Motivation:

It is valuable to be able to provide additional context for any kind of assertion failure, such as obvious points of API misuse or when the purpose of the assertion itself is unclear.

Modifications:

Added an overload of preconditionInEventLoop() that accepts a message autoclosure parameter in the same fashion as `Swift.precondition(_:file:line:)`. Default the implementation of the original version to calling the new one with an empty message. Add defaulted implementation of the new version which forwards the message closure to precondition(). Add the new message parameter directly to assertInEventLoop() without a forwarder since it is not a customization point.

Result:

It is now possible to optionally provide explanatory messages to be included when the "in event loop" precondition fails, as with any other precondition.
2020-05-11 14:07:56 +01:00
eb14015938 Create new write repeating byte method (#1500)
* Enable fast writing of repeated bytes

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: George Barnett <gbrntt@gmail.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-07 13:14:35 +01:00
Shekhar Rajak 71f7a0d172 code reuse for HTTPReq/Res isKnownUniquelyReferenced checks (#1507)
Motivation:

Fixes: #1504

Modifications:

Refactor: private mutate func copyStorageIfNotUniquelyReferenced is defined.

Result:

Refactored.
2020-05-07 12:51:03 +01:00
Peter AdamsandCory Benfield c8e253206b Add Optional ECN MetaData to Addressed Envelope (#1502)
Add Optional ECN MetaData to Addressed Envelope

Motivation:

This is a very small step along the path to #1472

Modifications:

Add an optional metadata field to addressed envelope. Currently the only possible data is ECN information. This is not currently populated anywhere.

Result:

Metadata in place for future work.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-05 10:30:17 +01:00
Peter Adams c30cf9c50f close channel after data is received. (#1501) 2020-05-04 11:13:00 +01:00
Cory Benfield d536e6619c Make some vars lets. (#1493)
Motivation:

Newer Swift 5.3 snapshots have spotted more vars that could be lets.

Modifications:

Made two vars lets.

Result:

The word `let` appears more frequently; the word `var`, less so.
2020-04-30 15:24:47 +01:00
Johannes Weiss 397343b6a0 add forgotten universal bootstrap docs (#1482) 2020-04-17 18:13:49 +01:00
Peter Adams a91c87f1c2 Correct doc comment on writeEOF (#1474)
Motivation:

Make the documentation correct.

Modifications:

Changed the word read to write in comments.

Result:

Doc comment only change to be correct.
2020-04-08 07:42:38 +01:00
Saleem Abdulrasool c7ab09013d Add BSDSocket.AddressFamily (#1469)
This finishes up the encapsulation that is required to ensure that we
are not leaking libc interfaces to clients but instead vend an
enumeration that they can use.  The enumeration is open so although we
may not have the full interface enumerated, users will not be prevented
from using alternate values.
2020-04-07 22:17:30 +01:00
Saleem Abdulrasool 2e2dcc25ca Add BSDSocket.ProtocolFamily (#1468)
This encapsulates the enumeration of protocol families rather than
relying on the underlying C library to provide the value.
2020-04-07 11:45:20 +01:00
Johannes Weiss 438d6436fd fix accidental deprecation (#1470)
Motivation:

In one of the previous patches, we accidentally merged the deprecation
of `ChannelOptions.Types.SocketOption.init` which will break a lot of
NIO programs.

Modifications:

Undo the deprecation.

Result:

NIOTS builds without warnings.
2020-04-04 17:01:08 +01:00
Saleem Abdulrasool 37b48094ba Add BSDSocket.SocketType to represent the socket type (#1467)
This adds a new enumeration which encapsulates the socket type values.
This enables ports to provide the same surface with a different name as
necessary.
2020-04-03 17:16:48 +01:00
Saleem AbdulrasoolandCory Benfield bcc180dab6 Add new BSDSocket namespace (#1461)
* NIO: Add new `BSDSocket` namespace

This starts the split of the POSIX/Linux/Darwin/BSD interfaces and the
BSD Socket interfaces in order to support Windows.  The constants on
Windows are not part of the C standard library and need to be explicitly
prefixed.  Use the import by name to avoid the `#if` conditions on the
wrapped versions.  This will allow us to cleanly cleave the dependency
on the underlying C library.

This change adds a `BSDSocket.OptionLevel` and `BSDSocket.Option` types
which allow us to get proper enumerations for these values and pipe them
throughout the NIO codebase.

* Apply suggestions from code review

Co-Authored-By: Cory Benfield <lukasa@apple.com>
2020-04-02 19:06:48 +01:00
Johannes Weiss e876fb3741 bootstraps: offer ELG validation (#1464)
Motivation:

Today, we just expect the ELGs passed to the bootstraps to be the
correct ones, if not, we crash.

Modifications:

Offer an alternative `validatingGroup:` `init` that just returns `nil`
if the ELGs are of the wrong types.

Result:

Easier to work with multi-stack systems for example when the user might
pass an ELG for either NIO on Sockets or NIO on Network.framework.
2020-04-01 13:57:48 +01:00
Johannes Weiss b2d937b22f shorten NIOThreadPool's thread names (#1466)
Motivation:

Previously, NIOThreadPool would use super long thread names. Linux
doesn't allow more than 15 bytes (+ \0 char) so in the end they would
never get set at all.

Modifications:

- Shorten NIOThreadPool's thread names
- Only take the first 15 characters of thread names in general on Linux

Result:

We don't lose thread names on Linux.
2020-03-31 20:01:56 +01:00
David Evans 6b0ed24408 Convenient initialisers from ByteBuffer (#1457) 2020-03-31 09:23:25 +01:00
Saleem AbdulrasoolandJohannes Weiss 6cac95501b Selector: further default to Linux behaviour (#1447)
This furthers the behaviour uniformity of special casing behaviour for
other platforms (e.g. Darwin, Windows) and defaulting to the Linux
behaviour as the rest of the code base assumes that the Linux behaviour
is a reasonable fallback.  Ensuring that the paths taken are always
uniformly taken helps identify the changes necessary for a port and make
it easier to reason about the changes.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-26 08:16:28 +00:00
Saleem Abdulrasool 5270fa81df IO: extend IOError for Windows (#1460)
There are three different error domains that we deal with in NIO, which
need to be differentiated.  Store the error with the domain to ensure
that we can identify the error domain.
2020-03-23 16:29:14 +00:00
Saleem Abdulrasool 91bde186b2 CNIOWindows: add paranoid preprocessing (NFC) (#1459)
Ensure that this file is only compiled for Windows targets.
2020-03-21 18:20:14 +00:00
Saleem AbdulrasoolandCory Benfield ad419b56d4 NIO: add module constructor for Windows (#1450)
This adds the necessary code to add a module constructor for NIO.  On
Windows, the socketing subsystem (WinSock) must be explicitly
initialized unlike Unices.  Add a module constructor that will be
handled by the C runtime to initialize the subsystem before the module
code is executed so that the user does not need to worry about this.

Since there is no way to inject this in Swift, write a tiny helper in C
and link that into the final NIO binary.

Since swift-package-manager does not support mixed-language directories,
add this to `CNIOWindows` which will need to be statically linked and
must always be imported as `@_implementationOnly` as this cannot be
linked into any user libraries.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-21 17:51:16 +00:00
Johannes Weiss a27a07719c testSafeToExecute*: fix threading issues (#1455)
Motivation:

testSafeToExecuteTrue/False accessed an internal property of the event
loop without synchronisation.

Modifications:

Fix the threading issue.

Result:

Tests with TSan happy.
2020-03-18 22:36:08 +00:00
Johannes Weiss bd4df32a26 downgrade 'schedule after shutdown' to printing error (#1454)
Motivation:

Since #1395, we crash when something is scheduled to an already shutdown
EventLoop. That's a good intent (#1383) but we feel it's a little too
risky right now.

Modifications:

Print an ERROR instead of just crashing.

Result:

Fewer crashes.
2020-03-18 18:20:35 +00:00
Johannes WeissandCory Benfield 2ad77f9a9b WIP: universal bootstrap (#1323)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-18 16:20:19 +00:00
Saleem Abdulrasool 834a5f2185 NIO: split out FileDescriptor and Selectable (#1444)
A `FileDescriptor` is not necessarily selectable (e.g. Windows), and a
selectable is not necessarily a `FileDescriptor` (e.g. Windows).

This will allow a basic `Selector` on Windows which will use `WSASelect`
to select on sockets for some basic functionality even though
IOCompletionPorts are preferred.
2020-03-18 15:48:40 +00:00
Saleem Abdulrasool 2673be6ae3 NIO: move comment to right location (NFC) (#1452)
Move the block comment to the right location before the function it
describes.
2020-03-17 09:31:00 +00:00
d3d7589a29 NIO: implement core counting on Windows (#1448)
* NIO: implement core counting on Windows

Implement the core counting logic for Windows, which does not support
`sysconf`.  It uses the Windows SDK to get the processor topology and
computes the core count from the topology.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-16 18:41:58 +00:00
Ludovic DewaillyandLudovic Dewailly fefd0c854f Provides a means to control the underlying header storage capacity. (#1451)
Motivation:

It's not always possible to initialize `HTTPHeaders` with all the headers one needs to set. In such case, the underlying array will be expanded as and when needed upon headers being appended. `Array` provides the means to reserve capacity to optimize such use case. This PR exposes this functionality in `HTTPHeaders`.

Modifications:

Expose method `reserveCapacity(_ minimumCapacity: Int)` and computed variable `capacity: Int` in `HTTPHeaders` and added some tests.

Result:

Developers will be able to control the capacity of the underlying headers array as such:

```
var headers = HTTPHeaders()
headers.reserveCapacity(5)
```

Co-authored-by: Ludovic Dewailly <ldewailly@apple.com>
2020-03-16 17:47:34 +00:00
Saleem Abdulrasool e5298263ec SelectableEventLoop: make Darwin the special case (#1449)
Treat the `autoreleasepool` case for Darwin as the special case rather
than Linux as the special case.  This is following the case throughout
the rest of the code base.  This is also needed for the Windows port.
2020-03-16 09:14:21 +00:00
Saleem AbdulrasoolandCory Benfield 648a6712ff NIO: unify the syscall wrapper (#1423)
The path was duplicated because the compiler was not previously able to
optimize with the constant value and would generate poor code.  This has
now been addressed, so unify the blocking and non-blocking paths.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-12 15:11:12 +00:00
Saleem Abdulrasool 790698cd54 NIO: swap selector implementations (NFC) (#1443)
In most of the code, Linux is treated as the fallback case.  This
adjusts the selector implementation to follow the same pattern, checking
if on Darwin and using kqueue (which would also be available on F/BSD)
and otherwise falling back to `epoll` (assuming Linux).  This
homogeneous behaviour makes it easier to reason about when trying to add
support for additional platforms.
2020-03-12 07:43:33 +00:00
Saleem Abdulrasool f44d524d56 NIO: use Posix "namespace" more aggressively (#1439)
Convert many more cases of the raw address family constant to the
definition in the System version.
2020-03-11 17:31:53 +00:00
Kaitlin Mahar a8e08d9757 BlockingIOThreadPool -> NIOThreadPool (#1438)
Update NonBlockingFileIO.init documentation to match the new type name.
2020-03-11 08:35:39 +00:00
Saleem Abdulrasool 7103febd74 NIO: undo exclusion of UDS on Windows (#1437)
This is in-scope for Windows now.
2020-03-09 17:38:10 +00:00
35078406c7 CNIOSHA1: make the code more portable (#1399)
Replace the non-portable `u_*_t` types with the C standard `u*_t` types
instead.  Remove the Unix headers as the only headers really needed are
the C standard headers.  Provide shims for deprecated `strings.h`
functions in terms of "modern" (C89+) functions from `string.h`.  Move
an android only include into the implementation from the interface.

This enables building this library on Windows.

Co-Authored-By: George Barnett <gbrntt@gmail.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-09 16:15:38 +00:00
Johannes Weiss ffb196f544 Thread: repair build on Windows after #1433 (#1434)
This repairs the build on Windows after the thread implementation was
split out.
2020-03-05 08:32:51 +00:00
Doug FriedmanandJohannes Weiss 51238f4df8 Prevent execution of tasks on an already shutdown EL (#1395)
* prevent execution of tasks on an already shutdown EL

* fixup! prevent execution of tasks on an already shutdown EL

* fixup! prevent execution of tasks on an already shutdown EL

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-04 17:20:13 +00:00
Shekhar RajakandJohannes Weiss 95f014c9ba use Posix.read instead of Glibc.read for timerfd (#1432)
Motivation:

To read the bytes from timerfd, we use Glibc.read directly but we should use try! Posix.read

Modifications:

Using Posix instead of Glibc for read operation with try! to check for any error.

Result:

Fixes #1341

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-04 16:51:30 +00:00
Shekhar Rajak 2900f03352 ByteToMessageDecoder: Default implementation for decodeLast should be provided (#1426)
* removed decodeLast function from NIOChatServer

* updated decodeLast function from WebSocketFrameDecoder

* default implementation of decodeLast function

* removed implementation of decodeLast in WebSocketFrameDecoder
2020-03-04 16:22:06 +00:00
Johannes Weiss a5e35655e6 fix macOS build & cleanup Thread (#1433)
Motivation:

The macOS build was broken by #1424, also the Thread.swift file became
a bit of a mess with `#if os(...)/#else` everywhere.

Modifications:

- fix macOS build
- cleanup Thread by splitting it into ThreadPosix & ThreadWindows

Result:

- build working again
- cleaner code
2020-03-04 15:12:10 +00:00
Saleem AbdulrasoolandJohannes Weiss 546a84c4e1 Thread: port to Windows (#1424)
This restructures `Thread` to have a more generic representation of a
thread handle.  Using that it is possible to have an implementation of
the threading APIs for Windows.

Similarly, introduce an abstraction over the Thread Specific Key.  This
allows us to abstract away the key type and the usage.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-03 20:21:45 +00:00
Saleem AbdulrasoolandJohannes Weiss 5e728b5786 NIOConcurrencyHelpers: port to Windows (#1403)
Add support for Windows to the `NIOConcurrencyHelpers`, replacing the
`pthreads` usage for Windows threading primitives.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-03 19:18:59 +00:00
Marcus LiottaandJohannes Weiss 5755c21a61 Add offset writes and file size support to NonBlockingFileIO (#1408)
* Add offset writes and file size support to NonBlockingFileIO

Motivation:

NonBlockingFileIO currently only supports writes from 0 or appending,
if opened with O_APPEND. To facilitate random read/write file access
the API needs to evolve.

Modifications:

- Add toOffset function parameter to write for offset file write access
- Add changeFileSize function for growing or shrinking the file
- Add readFileSize function for asynchronously reading the file size

Result:

- Improve API for random access scenarios
- Provide method for reading and writing file size

* Conform to code review

* Formatting

* Update Sources/NIO/NonBlockingFileIO.swift

Co-Authored-By: Johannes Weiss <johannesweiss@apple.com>

* Update Sources/NIO/NonBlockingFileIO.swift

Co-Authored-By: Johannes Weiss <johannesweiss@apple.com>

* Modify offset handling to support partial writes.
Clean up stray comment.

* Fix precondition check within offset pwrite handling

* Improve guard for readability

* White space cleanup

* Single spacing issue

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-02 11:02:05 +00:00
Kyle Browning 06cdd7f619 Allow discardable results on NIOAtomic for mutating functions (#1417)
* Allow discardable results on NIOAtomic for mutating functions

* Address nits.

* add a tests.

* Removes discards.
2020-02-26 21:43:35 +00:00
Saleem AbdulrasoolandJohannes Weiss 1595c42b9a CNIOAtomics: define types for uintptr_t and intptr_t (#1401)
On LLP64 hosts, `Int` is mapped to `intptr_t` and `UInt` to `uintptr_t`.
As a result, we need to ensure that we map the atomic extensions to
those types for type conversions.

Add the types for `uintptr_t` and `intptr_t` so that they can be used to
implement `CNIOConcurrencyHelpers` on Windows.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-02-26 17:07:39 +00:00
trungducc 8ad2549a49 Stop copying Strings into Arrays for SHA1 (#1413)
Motivation:

Resolve #1409

Modifications:

Using withContiguousStorageIfAvailable if string.utf8 supports an internal representation in a form. Otherwise, fall back to use withUnsafeBufferPointer.

Result:

Save an allocation for each hash function update
2020-02-25 08:52:36 +00:00
Shekhar RajakandJohannes Weiss dfd56c8b56 undo @convention(c) hacks in System.swift (#1412)
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-02-25 07:51:07 +00:00
Shekhar Rajak 58a80c300e Use UInt instead of ObjectIdentifier in promiseCreationStore (#1411)
* Use UInt instead of ObjectIdentifier in promiseCreationStore

* defined obfuscatePointerValue method
2020-02-24 17:01:16 +00:00
2c555d144a NIOConcurrencyHelpers: make LLP64 friendly (#1402)
On LLP64 platforms, `Int` is mapped to `intptr_t` and `UInt` is mapped
to `uintptr_t`.  Use the newly minted `CNIOAtomics` operations to
support these semantics.

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-02-19 19:05:58 +00:00