Files
swift-async-algorithms/Sources/AsyncSequenceValidation/TaskDriver.swift
T
Philippe Hauslerandkperryua 7969001299 Add a detailed proposal-like guide for AsyncSequence validation (#56)
* Add a detailed proposal like guide for MarbleDiagram

* Slight spacing change

Co-authored-by: kperryua <kperry@apple.com>

* Fix some pluralization agreement

Co-authored-by: kperryua <kperry@apple.com>

* Clarify the testing of merge and add a future direction for unordered groups

* rename the diagram system to a more general 'validate' based name

* Spelling fixes

* Grammar fixes for credits

Co-authored-by: kperryua <kperry@apple.com>
2022-02-22 11:03:27 -08:00

62 lines
1.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import _CAsyncSequenceValidationSupport
#if canImport(Darwin)
@_implementationOnly import Darwin
#elseif canImport(Glibc)
@_implementationOnly import Glibc
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#endif
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
final class TaskDriver {
let work: (TaskDriver) -> Void
let queue: WorkQueue
var thread: pthread_t?
init(queue: WorkQueue, _ work: @escaping (TaskDriver) -> Void) {
self.queue = queue
self.work = work
}
func start() {
pthread_create(&thread, nil, start_thread,
Unmanaged.passRetained(self).toOpaque())
}
func run() {
pthread_setname_np("Validation Diagram Clock Driver")
work(self)
}
func join() {
pthread_join(thread!, nil)
}
func enqueue(_ job: JobRef) {
let job = Job(job)
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob) {
let previous = AsyncSequenceValidationDiagram.Context.currentJob
AsyncSequenceValidationDiagram.Context.currentJob = job
job.execute()
AsyncSequenceValidationDiagram.Context.currentJob = previous
}
}
}