<h1><center> Modernizing Grand Central Dispatch Usage(5) </center></h1>
###### tags: `๐ป WWDC ์คํฐ๋`, `๐ป TIL`, `GCD`
###### date: `2024-01-07T15:12:33.284Z`
> [color=#724cd1][name=๋ฐ๋ฆญ]
> [Modernizing Grand Central Dispatch Usage - wwdc17](https://developer.apple.com/videos/play/wwdc2017/706/)
> WWDC 2017 Session ์ค ํ๋์ธ `Modernizing Grand Central Dispatch Usage`์ ๋ํด ์์๋ณด์
# ๊ฐ์
> 19:37๋ถํฐ ๋ค์ ์์์ด๋ค. (4)์์๋ Using GCD for Concurrency์ ๋ํด ํ์ตํ๋ค.
## Dispatch Source
```swift
let source = DispatchSource.makeReadSource(fileDescriptor: fd, queue: queue)
source.setEventHandler { read(fd) }
source.setCancelHandler { close(fd) }
source.activate()
```
> GCD์ Event monitoring primitive
`makeReadSource` ์์ฑ์๋ฅผ ํตํด `fileDescriptor`๋ฅผ ๋ชจ๋ํฐ๋งํ๋๋ก ์ค์ ํ๋ ๊ฒ์ด๋ค.
- Event handler executes on target queue
- Invalidation pattern with explicit cancellation
- initial setup followed by activate
source์ target ํ์ ํ๋ฅผ ์ ๋ฌํ๋ค. ์ฌ๊ธฐ์์ source์ event handler๋ฅผ ์คํํ๋ค. ๊ทธ๋ฆฌ๊ณ fileDescriptor๋ก ์ฝ๋๋ฅผ ์ฝ๋๋ค.
๋ํ, ์ด target queue๋ ์ฝ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ณ ์ง๋ ฌํํ๋ ๋ค๋ฅธ ์์
์ ๋ฃ์ ์๋ ์๋ค.
๊ทธ๋ฐ ๋ค์ source๊ฐ cancel hander๋ฅผ ์ค์ ํ์ฌ invalidation pattern์ ๊ตฌํํ๋ค.
๋ง์ง๋ง์ผ๋ก ๋ชจ๋ ๊ฒ์ด ์ค์ ๋๋ฉด source๋ฅผ ํธ์ถํ๊ณ , monitoring์ ํ์ฑํํ๋ค.
Source ์ค์ ๋ก OS ์ ์ฒด์์ ์ฌ์ฉ์๊ฐ ์ง์ ํ target queue์ ์ด๋ฒคํธ๋ฅผ ์ ๋ฌํ๋ ๊ฐ์ฒด๋ผ๊ธฐ ๋ณด๋ค ์ผ๋ฐ์ ์ธ ํจํด์ ์ธ์คํด์ค์ผ ๋ฟ์ด๋ผ๋ ๊ฒ์ด๋ค.
- ์: XPC ์ฐ๊ฒฐ
```
So, it's worth nothing that sources arer really just an instance of a more general pattern throughout the OS, where you have objects that deliver events to you on a target queue that you specify.
```
### Target Queue Hierarchy
- Serial queues and sources can form a tree
- Shared single mutual exclusion context
- Independent individual queue order

์์๋ S1, S2์ Q1, Q2๋ ๋ ๊ฐ์ ์์ค์ ํ์
๋๋ค.

```swift
let Q1 = DispatchQueue(label: "Q1", target: EQ)
let Q2 = DispatchQueue(label: "Q2", target: EQ)
```
์ด ๋ ๊ฐ์ ๋ค๋ฅธ ์ง๋ ฌ ํ๋ฅผ ์ถ๊ฐํ๊ณ ํ๋จ์ exclusion queue์ธ EQ๋ฅผ ์ถ๊ฐํจ์ผ๋ก์จ ์์ ํธ๋ฆฌ๋ฅผ ํ์ฑํฉ๋๋ค.
```
So, here we have two sources with their associated target queue, S1, S2 and the queue is Q1 and Q2. And we can form a little tree out of this situation by adding yet another serial queue to the mix, by adding a mutual exclusion queue, EQ, at the bottom.
```
์ด๋ฅผ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋จ์ํ optional target์ argument๋ฅผ dispatchQueue ์์ฑ์์ ์ ๋ฌํ๋ ๊ฒ์ด๋ค.
- ํ ๋ฒ์ ํ๋์ soruce or queue๋ฅผ ์คํํ ์ ์๋ค.
๊ทธ๋ฌ๋, queue 1, queue2๋ ๋
๋ฆฝ์ ์ธ ๊ฐ๋ณ queue order๊ฐ ์ ์ง๋๋ค.
๋ฌด์จ ๋ง์ธ์ง ์๋์ ์ค๋ช
ํ๊ฒ ๋ค.

์ด๋ฏธ์ง์๋ queue์ ํน์ ์์๋ก item์ ๊ฐ์ง๋ queue1๊ณผ queue2๊ฐ ์๋ค.

์ฐ๋ฆฌ๊ฐ ๊ฐ์ง๊ณ ์๋ ์ถ๊ฐ์ ์ธ ์ง๋ ฌ ํ๋ฅผ ํตํด ๋จ์ผ ์์
์ค๋ ๋๊ฐ q1, q2์ ์์
์ ์ํํ๋ค. ์ด ํ๋ EQ์์ ์๋ํ๊ฒ ๋๋๋ฐ ์ด ๊ณผ์ ์์ exclusion์ ํน์ฑ์ด ์ ์ฉ๋์ด ํ ๋ฒ์ ์ค์ง ํ๋์ ์์
๋ง ์ํ๋๊ฒ ๋๋ค.
- ๋ ํ์ item๋ค์ ํ์์ ๊ฐ์ง๊ณ ์๋ ๊ฐ๋ณ ์์๋ฅผ ์ ์งํ๋ฉด์ ๊ต์ฐจ ์คํ๋ ์ ์๋ค.
```
And because we have this extra serial queue at the bottom, and this executes, they will execute in EQ and there will be a single worker thread executing these items giving you that mutual exclusion property, only one item executing at one time.
```
## Quality of Service
- Abstract notion of priority
- Provides explicit classification of your work
- Affects various execution properties
์ฐ์ ์์์ ๋ํ ์ถ์์ ์ธ ๊ฐ๋
..?
QOS์ priority๋ผ๋ ์ฉ์ด๋ฅผ ๋น์ทํ ๊ฐ๋
์ผ๋ก ์ฌ์ฉํ๋ค.

์์คํ
์๋ ์ด 4๊ฐ์ service class๊ฐ ์๋ค.
> ์์์๋ถํฐ ์๋๋ก ๊ฐ์๋ก ๋ฎ์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋ค.
### QoS and Target Queue Hierarchy

S2๋ ์ฌ์ฉ์ ์ธํฐํ์ด์ค์ ๊ด๋ จ์ด ์๋ค๊ณ ๊ฐ์ ํด๋ณด์.
์ด๋ฒคํธ๊ฐ ํธ๋ฆฌ๊ฑฐ๋์๋ง์ UI๋ฅผ ์
๋ฐ์ดํธํด์ผ ํ๋ ์ด๋ฒคํธ๊ฐ ์๋์ง ๋ชจ๋ํฐ๋งํ ์ ์๋ค.

๋ ๋ค๋ฅธ ์ฌ๋ก๋, ์คํ ํ๋ฆ์ ์ ๊ณตํ๊ธฐ ์ํด EQ์ ๋ ์ด๋ธ์ ๋ฐฐ์นํ์ฌ ์ด ํธ๋ฆฌ์ ์ด๋ค item๋ ์ด ์์ค ์๋์์ ์คํํ ์ ์๋๋ก ํ๋ ๊ฒ์ด๋ค.

S1์ด ์คํ๋๋ฉด์ ์์ฒด์ ์ผ๋ก quality of service(QoS)์ ๊ดํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ ๊ฒฝ์ฐ, ํด๋น ํธ๋ฆฌ(flow)๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ค.
```
And now if anything else in this tree fires, for instance source 1, we will be using this flow for the tree if it doesn't have its own quality of service associated.
```

- Source์ ์คํ์ ์ปค๋์์ ์คํ๋๋ ๋น๋๊ธฐ์ด๋ค.
- source handler์ ์คํ์ ์ํด EQ์ ๋ฃ๋๋ค.
user space์์ ๋น๋๊ธฐ์ quality of service๋ ์ผ๋ฐ์ ์ผ๋ก queue.async๋ฅผ ํธ์ถํ ์ค๋ ๋์์ ๊ฒฐ์ ๋๋ค.

IN์ item์ ํ์ ๋ฃ๊ณ EQ๋ก ์คํํ๋ User initiated thread๊ฐ ์๋ค.

์ด ์ํฉ์์ ๋งค์ฐ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ๋ UI์ ๊ด๋ จ๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๋ S2๋ฅผ ์คํํ๋ Event Handler๊ฐ ์๋ค๊ณ ๊ฐ์ ํ์.
์ด ๋, ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ์คํํ๊ณ ๊ทธ ํธ๋ค๋ฌ๋ฅผ ์ด๋ฒคํธ ํ (EQ)์ ์ถ๊ฐํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น?


priority inversion ์ํฉ์ด ๋ฐ์ํ๋ค.

ํ์ง๋ง, system์ด ํ์ฌ queue์ ์ถ๊ฐ๋ ๊ฒ๋ค ์ค ๊ฐ์ฅ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋ worker thread๋ฅผ ๋ถ๋ฌ์์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
```
And now, maybe we have the source 2 that fires with this very high priority UI relevant event that executes its event handler, and enqueues its event handler into EQ.
```
24:30