Commit Graph
20 Commits
Author SHA1 Message Date
Johannes Weiss 5f94bd34a4 Selectors: make file descriptors CInts everywhere (#1815)
Motivation:

We and the OSes only support `CInt` as file descriptors. For some reason
we had interfaces that took `Int`s as fds which requires frequent
casting around. This is unnecessary.

Modifications:

- make fds `CInt` (internally)
- clean up some `Int32` as `CInt` use

Result:

cleaner code, less casting
2021-04-23 14:20:21 +01:00
Johannes Weiss 52c41b1005 fix compilation on Ubuntu 14.04 and other old Linuxes (#1810)
Motivation:

Glibc (in Sep 2014) changed the first parameter of `eventfd` from `int`
to `unsigned int` (which we assume since recently).

Modifications:

Make the code work with `int` and `unsigned int`.

Result:

Compiles again on 14.04.
2021-04-21 13:17:27 +01:00
George Barnett 5b6b7f02d7 Change redundant 'public' access modifiers (#1803)
Motivation:

I noticed in a recent PR that a bunch of `internal` types have functions
with public access modifiers. This is redundant and a little confusing
in PRs (as you have to know the type is internal).

Modifications:

- Make a bunch of `public` functions on `internal` types `internal`

Result:

Less confusing API.
2021-04-13 17:41:26 +01:00
Joakim Hassila 301e6a652f Fix bug in eventfd(initval: Int32, flags: Int32) (#1797)
Motivation:

There is bug in `eventfd(initval: Int32, flags: Int32)` where it doesn't actually pass on the arguments to `CNIOLinux.eventfd()` disregarding the intent of the caller.

Modification:

Start using the arguments passed in.

Result:

`eventfd(initval: Int32, flags: Int32)` now works as expected.
2021-04-09 17:47:44 +01:00
Peter Adams 43931b7a7d Make all imports of CNIOLinux conditional on OS (#1704)
Motivation:

Some users are including NIO in Xcode workspaces
rather than using SPM.  When this is done, if all
imports of CNIOLinux are conditional they can
remove this project from their workspace.

Modifications:

Make all imports of CNIOLinux conditional on Linux,
Android or FreeBSD

Result:

No user visible change.
2020-12-16 10:41:56 +00:00
buttafaceandCory Benfield 44d67ba4e0 Update and finish Android port. (#1695)
Motivation:

Be able to run and test swift-nio on Android.

Modifications:

- Remove the custom ifaddrs and use the one from the Android NDK instead.
- Enable a bunch of conditionally-compiled code for Android.
- Add a handful of constants and other Android declarations.
- Cast some types because of mismatches specific to Android.

Result:

Most tests pass on Android AArch64 and ARMv7.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-11-27 11:16:11 +00:00
Saleem Abdulrasool cf6f2b1807 Linux: make Epoll available on Android (#1621)
Android is sufficiently Linux-esque that it should be able to use `Epoll`.
2020-08-18 08:05:31 +01:00
Gautier DelormeandJohannes Weiss 0695662d7d fix cores count in containers (#1518)
Motivation:

The current System.coreCount implementation relies on _SC_NPROCESSORS_ONLN so isn't cgroups aware which might have a bad impact for apps runnings in containers (e.g. Docker, Kubernetes, Amazon ECS...).

Modifications:

- Changed System.coreCount on Linux only to make it read from CFS quotas and cpusets when present.
- Removed incorrect precondition in Sources/NIO/LinuxCPUSet.swift

Result:

System.coreCount returns correct values when apps run in containers.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-05-15 10:09:35 +01: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
Johannes Weiss 9f94448bc6 accept: don't do unnecessary dodgy pointer things (#1306)
Motivation:

We never look at the socket address that accept(4) returns so we
shouldn't ask for it. Additionally, it wasn't always the right size (in
IPv6 case) and also the pointer operations were dodgy.

Modifications:

Remove the whole socket address pointer stuff for accept.

Result:

Cleaner code.
2019-12-17 11:57:38 +00:00
Johannes Weiss 08ab8b51cd remove ugly and unnecessary ignore result (_ = ...) (#652)
Motivation:

_ = expression() is ugly and in many cases unimportant. In fact we train
ourselves to overread it which makes special cases where you'd actually
expect a result to be used just look normal.

Modifications:

remove _ = from a lot of places. In many cases we already had a better
(and sometimes cheaper way) to not return a value but in some cases
(mostly `remove` functions) I added `@discardableResult`

Result:

NIO source code looks nicer
2018-11-13 14:49:08 +00:00
Albert Aleksieiev a59293cbcd add Android support (#609)
### Motivation:

make swift-nio compatible with Android, and pass all test in Android.

### Modifications:

* add missed `ifaddrs` implementation
* add missed thread related functions - `CNIOLinux_pthread_getname_np`, `CNIOLinux_pthread_setaffinity_np` and `CNIOLinux_pthread_getaffinity_np`
* fix types incositency between Linux and Android, e.g. `Epoll.swift` class
* make bytes counter explicitely 64bits to avoid overflow on 32bit, in `PendingWritesManager.swift` and `PendingDatagramWritesState.swift`
* fix issue with Unix domain socket, first byte need to be zero in Android
* several incosistency fixes between Linux and Android api.

### Result:

now swift-nio works on Android. All tests passed!
2018-09-27 11:32:43 +01:00
Johannes Weiß b109389c54 eagerly process input EOF/connection resets (fixes #277) (#286)
Motivation:

We had a number of problems:

1. We wanted to lazily process input EOFs and connection resets only
   when the user actually calls `read()`. On Linux however you cannot
   unsubscribe from `EPOLLHUP` so that's not possible.
2. Lazily processing input EOFs/connection resets wastes kernel
   resources and that could potentially lead to a DOS
3. The very low-level `Selector` interpreted the eventing mechanism's
   events quite a lot so the `EventLoop`/`Channel` only ever saw
   `readable` or `writable` without further information what exactly
   happened.
4. We completely ignored `EPOLLHUP` until now which on Unix Domain
   Socket close leads to a 100% CPU spin (issue #277)

Modifications:

- made the `Selector` interface richer, it now sends the following
  events: `readable`, `writable`, `readEOF` (input EOF), `reset`
  (connection reset or some error)
- process input EOFs and connection resets/errors eagerly
- change all tests which relied on using unconnected and unbound sockets
  to user connected/bound ones as `epoll_wait` otherwise would keep
  sending us a stream of `EPOLLHUP`s which would now lead to an eager
  close

Result:

- most importantly: fix issue #277
- waste less kernel resources (by dealing with input EOFs/connection
  resets eagerly)
- bring kqueue/epoll more in line
2018-04-16 19:36:47 +02:00
Norman Maurer b8055f7467 Guard against the case when a Selectable is deregistered during Selector.whenReady(...) processing. (#210)
Motivation:

It's possible someone deregistered a Selectable while Selector.whenReady(...) is processed and an even for the now deregistered Selectable is ready. In this case we crashed (due a force-unwrap) on linux.

Modifications:

- Skip the processing of the Selectable if deregistered (by replacing force-unwrap with an if let).
- Add unit test.

Result:

No more crashes caused by this on Linux
2018-03-21 15:32:15 +01:00
Jason Toffaletti 49e159c51d use accept4 and SOCK_NONBLOCK on Linux (#124)
Motivation:

Reduce system calls on Linux required for configuring sockets as
nonblocking.

Modifications:

- expose accept4 as CNIOLinux_accept4
- add Linux internal enum for exposing Linux specific API
- wrap CNIOLinux_accept4 with Linux.accept4
- expose SOCK_NONBLOCK and SOCK_CLOEXEC on Linux
- add setNonBlocking parameter to BaseSocket.newSocket
- add setNonBlocking parameter to ServerSocket.accept

Result:

Servers and clients on Linux will use fewer system calls creating new sockets.
2018-03-12 20:12:20 +01:00
Norman Maurer 95a6e44e93 Remove return when possible. (#122)
Motivation:

We can cleanup the code by removing return keywords in closures sometimes.

Modifications:

Remove return when possible.

Result:

Cleaner code.
2018-03-11 22:47:29 +09:00
Cory Benfield 67bb71e44b Add a pending datagram writes manager
This change adds support for doing datagram-style writes. It's not currently
hooked up to anything, but it will be sometime soon.
2018-02-14 15:32:31 +00:00
Norman Maurer 17b5fd58e4 Use aliases to reduce duplication and make it easier to maintain.
* Use aliases to reduce duplication and make it easier to maintain.

Motivation:

We used many #if #else blocks which made the code very clumby.

Modifications:

Use aliases and define these in one place.

Result:

Easier to maintain code.

* Comments

* Comments
2018-02-09 16:56:52 +01:00
Norman Maurer 995177fbc6 Support setting Thread affinity and use it with EventLoopGroups 2018-02-06 16:08:57 +01:00
Cory Benfield a2bb047224 Add bindings for {send,recv}mmsg. 2018-01-17 09:18:28 +00:00