Skip to content

Instantly share code, notes, and snippets.

import Foundation
@propertyWrapper
public struct ThreadLocal<Value> {
private let defaultValue: () -> Value
private let name: String
public init(wrappedValue: @autoclosure @escaping () -> Value) {
self.defaultValue = wrappedValue
self.name = UUID().uuidString
@sidepelican
sidepelican / main.swift
Created February 22, 2023 02:18
Task.detachedはTask.initとは別のスレッドプールを持つが、無限に伸びるわけではない
var greeting = "Hello, playground"
print(greeting, ProcessInfo.processInfo.processorCount)
defer {
print(greeting)
}
for i in 0..<30 {
Task.detached {
print("detached", i)
sleep(1)
@sidepelican
sidepelican / main.swift
Created September 18, 2023 09:08
TaskLocalの引き継ぎはasync letやTask {}では行われる
import Foundation
enum MyValue {
@TaskLocal static var foo: Int = 0
}
func myFunc(@_inheritActorContext _ op: @Sendable @escaping () async -> ()) async {
await withCheckedContinuation { c in
DispatchQueue.global().async {
Task {
@sidepelican
sidepelican / main.swift
Created September 18, 2023 09:33
Taskはキャンセルされても明示的にtry Task.checkCancellation()などで抜けない限り最後まで走り抜ける
import Foundation
func mySleep(seconds: Int) async {
await withCheckedContinuation { c in
DispatchQueue.global().asyncAfter(deadline: .now() + TimeInterval(seconds)) {
c.resume()
}
}
}
@sidepelican
sidepelican / test.swift
Last active May 23, 2024 00:44
子タスクがキャンセルハンドルしなくても元スコープを抜け出せるtimeout
import Foundation
func brokenTask() async {
await withCheckedContinuation { (c) in
// キャンセルシグナルが握りつぶされていて正しく中断しない処理
Task {
try await Task.sleep(for: .seconds(5))
c.resume()
}
}
public struct RandomUInt8Sequence: Sequence {
@usableFromInline var count: Int
public init(count: Int) {
self.count = count
}
public func makeIterator() -> Iterator {
Iterator(self)
}
public struct Iterator: IteratorProtocol {
@sidepelican
sidepelican / concat.swift
Last active June 5, 2024 08:27
resultBuilderと雪だるまジェネリクスで型の組み立て
import Foundation
@dynamicMemberLookup
struct Concat<L: Decodable, R: Decodable>: Decodable {
init(left: L, right: R) {
self.left = left
self.right = right
}
private var left: L
private var right: R