Skip to content

Commit

Permalink
192
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 17, 2022
1 parent 0549f70 commit 8f039da
Show file tree
Hide file tree
Showing 15 changed files with 1,000 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 0190-concurrency-pt1/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## [Point-Free](https://www.pointfree.co)

> #### This directory contains code from Point-Free Episode: [Tour of Concurrency's Past: Threads](https://www.pointfree.co/episodes/ep190-concurrency-s-past-threads)
> #### This directory contains code from Point-Free Episode: [Concurrency's Past: Threads](https://www.pointfree.co/episodes/ep190-concurrency-s-past-threads)
>
> To better understand Swift’s concurrency tools, let’s first look to the past, starting with threads. Threads are a tool most developers don’t reach for these days, but are important to understand, and the way they solve problems reverberate even in today’s tools.
2 changes: 1 addition & 1 deletion 0191-concurrency-pt2/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## [Point-Free](https://www.pointfree.co)

> #### This directory contains code from Point-Free Episode: [Tour of Concurrency's Past: Queues and Combine](https://www.pointfree.co/episodes/ep191-concurrency-s-present-queues-and-combine)
> #### This directory contains code from Point-Free Episode: [Concurrency's Past: Queues and Combine](https://www.pointfree.co/episodes/ep191-concurrency-s-present-queues-and-combine)
>
> Before developing Swift’s modern concurrency tools, Apple improved upon threads with several other abstractions, including operation queues, Grand Central Dispatch, and Combine. Let’s see what these newer tools brought to the table.
9 changes: 9 additions & 0 deletions 0192-concurrency-pt3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrency"
BuildableName = "concurrency"
BlueprintName = "concurrency"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrencyTests"
BuildableName = "concurrencyTests"
BlueprintName = "concurrencyTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrencyTests"
BuildableName = "concurrencyTests"
BlueprintName = "concurrencyTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrency"
BuildableName = "concurrency"
BlueprintName = "concurrency"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrency"
BuildableName = "concurrency"
BlueprintName = "concurrency"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
5 changes: 5 additions & 0 deletions 0192-concurrency-pt3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Point-Free](https://www.pointfree.co)

> #### This directory contains code from Point-Free Episode: [Concurrency's Future: Tasks and Cooperation](https://www.pointfree.co/episodes/ep192-concurrency-s-future-tasks-and-cooperation)
>
> Let’s look at what the future of concurrency looks like in Swift. A recent release of Swift came with a variety of tools with concurrency. Let’s examine its fundamental unit in depth, and explore how they “cooperate” in your applications.
9 changes: 9 additions & 0 deletions 0192-concurrency-pt3/concurrency/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrency"
BuildableName = "concurrency"
BlueprintName = "concurrency"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrencyTests"
BuildableName = "concurrencyTests"
BlueprintName = "concurrencyTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrencyTests"
BuildableName = "concurrencyTests"
BlueprintName = "concurrencyTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrency"
BuildableName = "concurrency"
BlueprintName = "concurrency"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "concurrency"
BuildableName = "concurrency"
BlueprintName = "concurrency"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
19 changes: 19 additions & 0 deletions 0192-concurrency-pt3/concurrency/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "concurrency",
platforms: [.macOS(.v12)],
dependencies: [
],
targets: [
.executableTarget(
name: "concurrency",
dependencies: []),
.testTarget(
name: "concurrencyTests",
dependencies: ["concurrency"]),
]
)
3 changes: 3 additions & 0 deletions 0192-concurrency-pt3/concurrency/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# concurrency

A description of this package.
104 changes: 104 additions & 0 deletions 0192-concurrency-pt3/concurrency/Sources/concurrency/Combine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Combine
import Foundation

let publisher1 = Deferred {
Future<Int, Never> { callback in
print(Thread.current)
callback(.success(42))
}
}
.subscribe(on: DispatchQueue(label: "queue1"))

let publisher2 = Deferred {
Future<String, Never> { callback in
print(Thread.current)
callback(.success("Hello world"))
}
}
.subscribe(on: DispatchQueue(label: "queue2"))

let cancellable = publisher1
.flatMap { integer in
Deferred {
Future<String, Never> { callback in
print(Thread.current)
callback(.success("\(integer)"))
}
}
.subscribe(on: DispatchQueue(label: "queue3"))
}
.zip(publisher2)
.sink {
print("sink", $0, Thread.current)
}

//_ = cancellable


func operationQueueCoordination() {
let queue = OperationQueue()

let operationA = BlockOperation {
print("A")
Thread.sleep(forTimeInterval: 1)
}
let operationB = BlockOperation {
print("B")
}
let operationC = BlockOperation {
print("C")
}
let operationD = BlockOperation {
print("D")
}
operationB.addDependency(operationA)
operationC.addDependency(operationA)
operationD.addDependency(operationB)
operationD.addDependency(operationC)
queue.addOperation(operationA)
queue.addOperation(operationB)
queue.addOperation(operationC)
queue.addOperation(operationD)

operationA.cancel()

/*
A ➡️ B
⬇️ ⬇️
C ➡️ D
*/
}

//a
// .handleEvents(...)
// .compactMap { $0 }
// .flatMap { a in zip(b(a), c(a)) }
// .flatMap { b, c in d(b, c) }
// .handleEvents(receiveCompletion: { _ in print("Finished") })

// defer { print("Finished") }
// guard let a = await f()
// else { return }
// async let b = g(a)
// async let c = h(a)
// let d = await i(b, c)


func dispatchDiamondDependency() {
let queue = DispatchQueue(label: "queue", attributes: .concurrent)
queue.async {
print("A")

let group = DispatchGroup()
queue.async(group: group) {
print("B")
}
queue.async(group: group) {
print("C")
}

group.notify(queue: queue) {
print("D")
}
}
}
Loading

0 comments on commit 8f039da

Please sign in to comment.