OperationGroup.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #if SWIFT_PACKAGE
  34. import CgRPC
  35. #endif
  36. /// Singleton class that provides a mutex for synchronizing tag generation
  37. private class OperationGroupTagLock {
  38. var mutex : Mutex
  39. private init() {
  40. mutex = Mutex()
  41. }
  42. static let sharedInstance = OperationGroupTagLock()
  43. }
  44. /// A collection of gRPC operations
  45. class OperationGroup {
  46. /// Used to generate unique tags for OperationGroups
  47. static var nextTag : Int64 = 1
  48. /// Automatically-assigned tag that is used with the completion queue.
  49. var tag : Int64
  50. /// The call associated with the operation group. Retained while the operations are running.
  51. var call : Call
  52. /// An array of operation objects that are passed into the initializer
  53. var operationsArray : [Operation]?
  54. /// Pointer to underlying C representation
  55. var underlyingOperations : UnsafeMutableRawPointer!
  56. /// Completion handler that is called when the group completes
  57. var completion : ((Bool) -> Void)
  58. /// Initializes a Operations representation
  59. ///
  60. /// - Parameter operations: an array of operations
  61. init(call: Call,
  62. operations: [Operation],
  63. completion: @escaping ((Bool) -> Void)) {
  64. self.call = call
  65. self.operationsArray = operations
  66. self.underlyingOperations = cgrpc_operations_create()
  67. cgrpc_operations_reserve_space_for_operations(self.underlyingOperations, Int32(operations.count))
  68. for operation in operations {
  69. cgrpc_operations_add_operation(self.underlyingOperations, operation.underlyingObserver)
  70. }
  71. self.completion = completion
  72. let mutex = OperationGroupTagLock.sharedInstance.mutex
  73. mutex.lock()
  74. self.tag = OperationGroup.nextTag
  75. OperationGroup.nextTag += 1
  76. mutex.unlock()
  77. }
  78. }