//===----------------------------------------------------------------------===// // // This source file is part of the SwiftNIO open source project // // Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information // See CONTRIBUTORS.txt for the list of SwiftNIO project authors // // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// #if !os(WASI) #if os(Linux) || os(Android) import CNIOLinux /// A set that contains CPU ids to use. struct LinuxCPUSet { /// The ids of all the cpus. let cpuIds: Set /// Create a new instance /// /// - arguments: /// - cpuIds: The `Set` of CPU ids. It must be non-empty and can not contain invalid ids. init(cpuIds: Set) { precondition(!cpuIds.isEmpty) self.cpuIds = cpuIds } /// Create a new instance /// /// - arguments: /// - cpuId: The CPU id. init(_ cpuId: Int) { let ids: Set = [cpuId] self.init(cpuIds: ids) } } extension LinuxCPUSet: Equatable {} /// Linux specific extension to `NIOThread`. extension NIOThread { /// Specify the thread-affinity of the current thread. static var currentAffinity: LinuxCPUSet { get { var cpuset = cpu_set_t() // Ensure the cpuset is empty (and so nothing is selected yet). CNIOLinux_CPU_ZERO(&cpuset) let res = CNIOLinux_pthread_getaffinity_np(pthread_self(), MemoryLayout.size(ofValue: cpuset), &cpuset) precondition(res == 0, "pthread_getaffinity_np failed: \(res)") let set = Set( (CInt(0)..