杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
//  SchedulerType+SharedSequence.swift
//  RxCocoa
//
//  Created by Krunoslav Zaher on 8/27/17.
//  Copyright © 2017 Krunoslav Zaher. All rights reserved.
//
 
import RxSwift
 
public enum SharingScheduler {
    /// Default scheduler used in SharedSequence based traits.
    public private(set) static var make: () -> SchedulerType = { MainScheduler() }
 
    /**
     This method can be used in unit tests to ensure that built in shared sequences are using mock schedulers instead
     of main schedulers.
 
     **This shouldn't be used in normal release builds.**
    */
    static public func mock(scheduler: SchedulerType, action: () throws -> Void) rethrows {
        return try mock(makeScheduler: { scheduler }, action: action)
    }
 
    /**
     This method can be used in unit tests to ensure that built in shared sequences are using mock schedulers instead
     of main schedulers.
 
     **This shouldn't be used in normal release builds.**
     */
    static public func mock(makeScheduler: @escaping () -> SchedulerType, action: () throws -> Void) rethrows {
        let originalMake = make
        make = makeScheduler
        defer {
            make = originalMake
        }
 
        try action()
 
        // If you remove this line , compiler buggy optimizations will change behavior of this code
        _forceCompilerToStopDoingInsaneOptimizationsThatBreakCode(makeScheduler)
        // Scary, I know
    }
}
 
#if os(Linux)
    import Glibc
#else
    import Foundation
#endif
 
func _forceCompilerToStopDoingInsaneOptimizationsThatBreakCode(_ scheduler: () -> SchedulerType) {
    let a: Int32 = 1
#if os(Linux)
    let b = 314 + Int32(Glibc.random() & 1)
#else
    let b = 314 + Int32(arc4random() & 1)
#endif
    if a == b {
        print(scheduler())
    }
}