This repository has been archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from belozierov/NewSharedCoroutineDispatcher
New shared coroutine dispatcher
- Loading branch information
Showing
13 changed files
with
313 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// AtomicBitMask.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 06.04.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
private let deBruijn = [00, 01, 48, 02, 57, 49, 28, 03, | ||
61, 58, 50, 42, 38, 29, 17, 04, | ||
62, 55, 59, 36, 53, 51, 43, 22, | ||
45, 39, 33, 30, 24, 18, 12, 05, | ||
63, 47, 56, 27, 60, 41, 37, 16, | ||
54, 35, 52, 21, 44, 32, 23, 11, | ||
46, 26, 40, 15, 34, 20, 31, 10, | ||
25, 14, 19, 09, 13, 08, 07, 06] | ||
|
||
struct AtomicBitMask { | ||
|
||
private var atomic = AtomicInt(value: 0) | ||
var rawValue: Int { atomic.value } | ||
var isEmpty: Bool { atomic.value == 0 } | ||
|
||
mutating func insert(_ index: Int) { | ||
atomic.update { $0 | (1 << index) } | ||
} | ||
|
||
mutating func remove(_ index: Int) -> Bool { | ||
if isEmpty { return false } | ||
let (new, old) = atomic.update { $0 & ~(1 << index) } | ||
return new != old | ||
} | ||
|
||
mutating func pop() -> Int? { | ||
var index: Int! | ||
atomic.update { | ||
if $0 == 0 { index = nil; return $0 } | ||
let uint = UInt(bitPattern: $0) | ||
let value = uint & (0 &- uint) | ||
index = deBruijn[Int((value &* 285870213051386505) >> 58)] | ||
return $0 & ~(1 << index) | ||
} | ||
return index | ||
} | ||
|
||
mutating func pop(offset: Int) -> Int? { | ||
var index: Int! | ||
atomic.update { | ||
if $0 == 0 { index = nil; return $0 } | ||
let uint = UInt(bitPattern: ($0 << offset) + ($0 >> (64 - offset))) | ||
let value = uint & (0 &- uint) | ||
index = deBruijn[Int((value &* 285870213051386505) >> 58)] - offset | ||
if index < 0 { index += 64 } | ||
return $0 & ~(1 << index) | ||
} | ||
return index | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.