CompletionQueue.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. import CgRPC
  18. import Dispatch
  19. #endif
  20. import Foundation
  21. /// Execute the given closure and ensure we release all auto pools if needed.
  22. @inlinable
  23. internal func withAutoReleasePool<T>(_ execute: () throws -> T) rethrows -> T {
  24. #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
  25. return try autoreleasepool {
  26. try execute()
  27. }
  28. #else
  29. return try execute()
  30. #endif
  31. }
  32. /// A type indicating the kind of event returned by the completion queue
  33. enum CompletionType {
  34. case queueShutdown
  35. case queueTimeout
  36. case complete
  37. case unknown
  38. fileprivate static func completionType(_ value: grpc_completion_type) -> CompletionType {
  39. switch value {
  40. case GRPC_QUEUE_SHUTDOWN:
  41. return .queueShutdown
  42. case GRPC_QUEUE_TIMEOUT:
  43. return .queueTimeout
  44. case GRPC_OP_COMPLETE:
  45. return .complete
  46. default:
  47. return .unknown
  48. }
  49. }
  50. }
  51. /// An event that is returned by the completion queue
  52. struct CompletionQueueEvent {
  53. let type: CompletionType
  54. let success: Int32
  55. let tag: Int
  56. init(_ event: grpc_event) {
  57. type = CompletionType.completionType(event.type)
  58. success = event.success
  59. tag = Int(bitPattern: cgrpc_event_tag(event))
  60. }
  61. }
  62. /// A gRPC Completion Queue
  63. class CompletionQueue {
  64. /// Optional user-provided name for the queue
  65. let name: String?
  66. /// Pointer to underlying C representation
  67. private let underlyingCompletionQueue: UnsafeMutableRawPointer
  68. /// Operation groups that are awaiting completion, keyed by tag
  69. private var operationGroups: [Int: OperationGroup] = [:]
  70. /// Mutex for synchronizing access to operationGroups
  71. private let operationGroupsMutex: Mutex = Mutex()
  72. private var _hasBeenShutdown = false
  73. public var hasBeenShutdown: Bool { return operationGroupsMutex.synchronize { _hasBeenShutdown } }
  74. /// Initializes a CompletionQueue
  75. ///
  76. /// - Parameter cq: the underlying C representation
  77. init(underlyingCompletionQueue: UnsafeMutableRawPointer, name: String? = nil) {
  78. // The underlying completion queue is NOT OWNED by this class, so we don't dealloc it in a deinit
  79. self.underlyingCompletionQueue = underlyingCompletionQueue
  80. self.name = name
  81. }
  82. deinit {
  83. operationGroupsMutex.synchronize {
  84. _hasBeenShutdown = true
  85. }
  86. cgrpc_completion_queue_shutdown(underlyingCompletionQueue)
  87. cgrpc_completion_queue_drain(underlyingCompletionQueue)
  88. grpc_completion_queue_destroy(underlyingCompletionQueue)
  89. }
  90. /// Waits for an operation group to complete
  91. ///
  92. /// - Parameter timeout: a timeout value in seconds
  93. /// - Returns: a grpc_completion_type code indicating the result of waiting
  94. func wait(timeout: TimeInterval) -> CompletionQueueEvent {
  95. let event = cgrpc_completion_queue_get_next_event(underlyingCompletionQueue, timeout)
  96. return CompletionQueueEvent(event)
  97. }
  98. /// Register an operation group for handling upon completion. Will throw if the queue has been shutdown already.
  99. ///
  100. /// - Parameter operationGroup: the operation group to handle.
  101. func register(_ operationGroup: OperationGroup, onSuccess: () throws -> Void) throws {
  102. try operationGroupsMutex.synchronize {
  103. guard !_hasBeenShutdown
  104. else { throw CallError.completionQueueShutdown }
  105. operationGroups[operationGroup.tag] = operationGroup
  106. try onSuccess()
  107. }
  108. }
  109. /// Runs a completion queue and call a completion handler when finished
  110. ///
  111. /// - Parameter completion: a completion handler that is called when the queue stops running
  112. func runToCompletion(completion: (() -> Void)?) {
  113. // run the completion queue on a new background thread
  114. var threadLabel = "SwiftGRPC.CompletionQueue.runToCompletion.spinloopThread"
  115. if let name = self.name {
  116. threadLabel.append(" (\(name))")
  117. }
  118. let spinloopThreadQueue = DispatchQueue(label: threadLabel)
  119. spinloopThreadQueue.async {
  120. var spinloopActive = true
  121. while spinloopActive {
  122. withAutoReleasePool {
  123. let event = cgrpc_completion_queue_get_next_event(self.underlyingCompletionQueue, 600)
  124. switch event.type {
  125. case GRPC_OP_COMPLETE:
  126. let tag = Int(bitPattern:cgrpc_event_tag(event))
  127. self.operationGroupsMutex.lock()
  128. let operationGroup = self.operationGroups[tag]
  129. self.operationGroupsMutex.unlock()
  130. if let operationGroup = operationGroup {
  131. // call the operation group completion handler
  132. operationGroup.success = (event.success == 1)
  133. operationGroup.completion?(operationGroup)
  134. self.operationGroupsMutex.synchronize {
  135. self.operationGroups[tag] = nil
  136. }
  137. } else {
  138. print("CompletionQueue.runToCompletion error: operation group with tag \(tag) not found")
  139. }
  140. case GRPC_QUEUE_SHUTDOWN:
  141. self.operationGroupsMutex.lock()
  142. let currentOperationGroups = self.operationGroups
  143. self.operationGroups = [:]
  144. self.operationGroupsMutex.unlock()
  145. for operationGroup in currentOperationGroups.values {
  146. operationGroup.success = false
  147. operationGroup.completion?(operationGroup)
  148. }
  149. spinloopActive = false
  150. return
  151. case GRPC_QUEUE_TIMEOUT:
  152. return
  153. default:
  154. spinloopActive = false
  155. return
  156. }
  157. }
  158. }
  159. // when the queue stops running, call the queue completion handler
  160. completion?()
  161. }
  162. }
  163. /// Runs a completion queue
  164. func run() {
  165. runToCompletion(completion: nil)
  166. }
  167. /// Shuts down a completion queue
  168. func shutdown() {
  169. var needsShutdown = false
  170. operationGroupsMutex.synchronize {
  171. if !_hasBeenShutdown {
  172. needsShutdown = true
  173. _hasBeenShutdown = true
  174. }
  175. }
  176. if needsShutdown {
  177. cgrpc_completion_queue_shutdown(underlyingCompletionQueue)
  178. }
  179. }
  180. }