Mutex.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /// A general-purpose Mutex used to synchronize gRPC operations
  37. /// but it can be used anywhere
  38. public class Mutex {
  39. /// Pointer to underlying C representation
  40. private var underlyingMutex: UnsafeMutableRawPointer
  41. /// Initializes a Mutex
  42. public init() {
  43. underlyingMutex = cgrpc_mutex_create();
  44. }
  45. deinit {
  46. cgrpc_mutex_destroy(underlyingMutex);
  47. }
  48. /// Locks a Mutex
  49. ///
  50. /// Waits until no thread has a lock on the Mutex,
  51. /// causes the calling thread to own an exclusive lock on the Mutex,
  52. /// then returns.
  53. ///
  54. /// May block indefinitely or crash if the calling thread has a lock on the Mutex.
  55. public func lock() {
  56. cgrpc_mutex_lock(underlyingMutex);
  57. }
  58. /// Unlocks a Mutex
  59. ///
  60. /// Releases an exclusive lock on the Mutex held by the calling thread.
  61. public func unlock() {
  62. cgrpc_mutex_unlock(underlyingMutex);
  63. }
  64. /// Runs a block within a locked mutex
  65. ///
  66. /// Parameter block: the code to run while the mutex is locked
  67. public func synchronize(block:() -> Void) {
  68. lock()
  69. block()
  70. unlock()
  71. }
  72. }