Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class DispatchSerialQueue not available under Linux #848

Open
mman opened this issue Oct 9, 2024 · 1 comment
Open

class DispatchSerialQueue not available under Linux #848

mman opened this issue Oct 9, 2024 · 1 comment

Comments

@mman
Copy link

mman commented Oct 9, 2024

Probably another inconsistency, but the following code creating an actor backed by serial DispatchQueue works on macOS and does not compile under Linux:

public actor MyActor {
    let queue = DispatchSerialQueue(label: "actorQueue")

    nonisolated public var unownedExecutor: UnownedSerialExecutor {
        queue.asUnownedSerialExecutor()
    }
}

Under Linux it produces the following error:

/src/Sources/Test/test.swift:63:17: error: cannot find 'DispatchSerialQueue' in scope
 61 | public actor MyActor {
 62 |     let logger: Logger
 63 |     let queue = DispatchSerialQueue(label: "relayQueue")
    |                 `- error: cannot find 'DispatchSerialQueue' in scope
 64 | 
 65 |     nonisolated public var unownedExecutor: UnownedSerialExecutor {
@mman
Copy link
Author

mman commented Oct 14, 2024

For anybody reaching this bug. For the moment the proper solution under Linux seems to be:

actor X {
    let queue = DispatchQueue(label: "queue")
    private let executor: DispatchQueueExecutor

    final class DispatchQueueExecutor: SerialExecutor {
         private let queue: DispatchQueue

         init(queue: DispatchQueue) {
             self.queue = queue
         }

         func enqueue(_ job: UnownedJob) {
             self.queue.async {
                 job.runSynchronously(on: self.asUnownedSerialExecutor())
             }
         }

         func asUnownedSerialExecutor() -> UnownedSerialExecutor {
             UnownedSerialExecutor(ordinary: self)
         }

        func checkIsolated() {
            dispatchPrecondition(condition: .onQueue(self.queue))
        }
    }

    nonisolated public var unownedExecutor: UnownedSerialExecutor {
        executor.asUnownedSerialExecutor()
    }

    init() {
        self.executor = DispatchQueueExecutor(queue: queue)
    }

    /// remaining logic
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant