Browse Source

Replace CountDownLatch with DispatchSemaphore in generated code.

Tim Burks 8 years ago
parent
commit
8170253611

+ 26 - 26
Examples/Echo/Generated/echo.client.pb.swift

@@ -40,9 +40,9 @@
  */
 
 import Foundation
+import Dispatch
 import gRPC
 
-
 /// Type for errors thrown from generated client code.
 public enum Echo_EchoClientError : Error {
   case endOfStream
@@ -61,7 +61,7 @@ public class Echo_EchoGetCall {
   /// Run the call. Blocks until the reply is received.
   fileprivate func run(request: Echo_EchoRequest,
                        metadata: Metadata) throws -> Echo_EchoResponse {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     var callResult : CallResult!
     var response : Echo_EchoResponse?
     let requestData = try request.serializeProtobuf()
@@ -73,9 +73,9 @@ public class Echo_EchoGetCall {
       if let responseData = callResult.resultData {
         response = try? Echo_EchoResponse(protobuf:responseData)
       }
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     if let response = response {
       return response
     } else {
@@ -96,14 +96,14 @@ public class Echo_EchoExpandCall {
   // Call this once with the message to send.
   fileprivate func run(request: Echo_EchoRequest, metadata: Metadata) throws -> Echo_EchoExpandCall {
     let requestData = try request.serializeProtobuf()
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try call.start(.serverStreaming,
                    metadata:metadata,
                    message:requestData)
     {callResult in
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     return self
   }
 
@@ -111,7 +111,7 @@ public class Echo_EchoExpandCall {
   public func receive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var response : Echo_EchoResponse!
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData {
@@ -122,9 +122,9 @@ public class Echo_EchoExpandCall {
         } else {
           returnError = Echo_EchoClientError.endOfStream
         }
-        latch.signal()
+        sem.signal()
       }
-      latch.wait()
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
     }
     if let returnError = returnError {
       throw returnError
@@ -144,13 +144,13 @@ public class Echo_EchoCollectCall {
 
   // Call this to start a call.
   fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try self.call.start(.clientStreaming,
                         metadata:metadata)
     {callResult in
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     return self
   }
 
@@ -164,7 +164,7 @@ public class Echo_EchoCollectCall {
   public func closeAndReceive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var returnResponse : Echo_EchoResponse!
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData,
@@ -173,10 +173,10 @@ public class Echo_EchoCollectCall {
         } else {
           returnError = Echo_EchoClientError.invalidMessageReceived
         }
-        latch.signal()
+        sem.signal()
       }
       try call.close(completion:{})
-      latch.wait()
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
     } catch (let error) {
       throw error
     }
@@ -197,20 +197,20 @@ public class Echo_EchoUpdateCall {
   }
 
   fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try self.call.start(.bidiStreaming,
                         metadata:metadata)
     {callResult in
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     return self
   }
 
   public func receive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var returnMessage : Echo_EchoResponse!
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     do {
       try call.receiveMessage() {(data) in
         if let data = data {
@@ -221,9 +221,9 @@ public class Echo_EchoUpdateCall {
         } else {
           returnError = Echo_EchoClientError.endOfStream
         }
-        latch.signal()
+        sem.signal()
       }
-      latch.wait()
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
     }
     if let returnError = returnError {
       throw returnError
@@ -237,11 +237,11 @@ public class Echo_EchoUpdateCall {
   }
 
   public func closeSend() throws {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try call.close() {
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 }
 
@@ -301,4 +301,4 @@ public class Echo_EchoService {
   public func update() throws -> Echo_EchoUpdateCall {
     return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
   }
-}
+}

+ 10 - 10
Examples/Echo/Generated/echo.server.pb.swift

@@ -40,8 +40,8 @@
  */
 
 import Foundation
-import gRPC
 import Dispatch
+import gRPC
 
 /// Type for errors thrown from generated server code.
 public enum Echo_EchoServerError : Error {
@@ -150,15 +150,15 @@ public class Echo_EchoCollectSession : Echo_EchoSession {
 
   /// Receive a message. Blocks until a message is received or the client closes the connection.
   public func receive() throws -> Echo_EchoRequest {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     var requestMessage : Echo_EchoRequest?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
         requestMessage = try? Echo_EchoRequest(protobuf:requestData)
       }
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     if requestMessage == nil {
       throw Echo_EchoServerError.endOfStream
     }
@@ -199,7 +199,7 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
 
   /// Receive a message. Blocks until a message is received or the client closes the connection.
   public func receive() throws -> Echo_EchoRequest {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     var requestMessage : Echo_EchoRequest?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
@@ -209,9 +209,9 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
           print("error \(error)")
         }
       }
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     if let requestMessage = requestMessage {
       return requestMessage
     } else {
@@ -226,13 +226,13 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
 
   /// Close a connection. Blocks until the connection is closed.
   public func close() throws {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try self.handler.sendStatus(statusCode:self.statusCode,
                                 statusMessage:self.statusMessage,
                                 trailingMetadata:self.trailingMetadata) {
-                                  latch.signal()
+                                  sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 
   /// Run the session. Internal.

File diff suppressed because it is too large
+ 0 - 0
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift


+ 9 - 9
Plugin/Templates/client-call-bidistreaming.swift

@@ -8,20 +8,20 @@ public class {{ .|call:protoFile,service,method }} {
   }
 
   fileprivate func run(metadata:Metadata) throws -> {{ .|call:protoFile,service,method }} {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try self.call.start(.bidiStreaming,
                         metadata:metadata)
     {callResult in
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     return self
   }
 
   public func receive() throws -> {{ method|output }} {
     var returnError : {{ .|clienterror:protoFile,service }}?
     var returnMessage : {{ method|output }}!
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     do {
       try call.receiveMessage() {(data) in
         if let data = data {
@@ -32,9 +32,9 @@ public class {{ .|call:protoFile,service,method }} {
         } else {
           returnError = {{ .|clienterror:protoFile,service }}.endOfStream
         }
-        latch.signal()
+        sem.signal()
       }
-      latch.wait()
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
     }
     if let returnError = returnError {
       throw returnError
@@ -48,10 +48,10 @@ public class {{ .|call:protoFile,service,method }} {
   }
 
   public func closeSend() throws {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try call.close() {
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 }

+ 6 - 6
Plugin/Templates/client-call-clientstreaming.swift

@@ -9,13 +9,13 @@ public class {{ .|call:protoFile,service,method }} {
 
   // Call this to start a call.
   fileprivate func run(metadata:Metadata) throws -> {{ .|call:protoFile,service,method }} {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try self.call.start(.clientStreaming,
                         metadata:metadata)
     {callResult in
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     return self
   }
 
@@ -29,7 +29,7 @@ public class {{ .|call:protoFile,service,method }} {
   public func closeAndReceive() throws -> {{ method|output }} {
     var returnError : {{ .|clienterror:protoFile,service }}?
     var returnResponse : {{ method|output }}!
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData,
@@ -38,10 +38,10 @@ public class {{ .|call:protoFile,service,method }} {
         } else {
           returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
         }
-        latch.signal()
+        sem.signal()
       }
       try call.close(completion:{})
-      latch.wait()
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
     } catch (let error) {
       throw error
     }

+ 6 - 6
Plugin/Templates/client-call-serverstreaming.swift

@@ -10,14 +10,14 @@ public class {{ .|call:protoFile,service,method }} {
   // Call this once with the message to send.
   fileprivate func run(request: {{ method|input }}, metadata: Metadata) throws -> {{ .|call:protoFile,service,method }} {
     let requestData = try request.serializeProtobuf()
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try call.start(.serverStreaming,
                    metadata:metadata,
                    message:requestData)
     {callResult in
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     return self
   }
 
@@ -25,7 +25,7 @@ public class {{ .|call:protoFile,service,method }} {
   public func receive() throws -> {{ method|output }} {
     var returnError : {{ .|clienterror:protoFile,service }}?
     var response : {{ method|output }}!
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData {
@@ -36,9 +36,9 @@ public class {{ .|call:protoFile,service,method }} {
         } else {
           returnError = {{ .|clienterror:protoFile,service }}.endOfStream
         }
-        latch.signal()
+        sem.signal()
       }
-      latch.wait()
+      _ = sem.wait(timeout: DispatchTime.distantFuture)
     }
     if let returnError = returnError {
       throw returnError

+ 3 - 3
Plugin/Templates/client-call-unary.swift

@@ -10,7 +10,7 @@ public class {{ .|call:protoFile,service,method }} {
   /// Run the call. Blocks until the reply is received.
   fileprivate func run(request: {{ method|input }},
                        metadata: Metadata) throws -> {{ method|output }} {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     var callResult : CallResult!
     var response : {{ method|output }}?
     let requestData = try request.serializeProtobuf()
@@ -22,9 +22,9 @@ public class {{ .|call:protoFile,service,method }} {
       if let responseData = callResult.resultData {
         response = try? {{ method|output }}(protobuf:responseData)
       }
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     if let response = response {
       return response
     } else {

+ 2 - 2
Plugin/Templates/client.pb.swift

@@ -40,8 +40,8 @@
  */
 
 import Foundation
+import Dispatch
 import gRPC
-
 //-{% for service in protoFile.service %}
 
 /// Type for errors thrown from generated client code.
@@ -131,4 +131,4 @@ public class {{ .|serviceclass:protoFile,service }} {
   //-{% endif %}
   //-{% endfor %}
 }
-//-{% endfor %}
+//-{% endfor %}

+ 6 - 6
Plugin/Templates/server-session-bidistreaming.swift

@@ -10,7 +10,7 @@ public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,s
 
   /// Receive a message. Blocks until a message is received or the client closes the connection.
   public func receive() throws -> {{ method|input }} {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     var requestMessage : {{ method|input }}?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
@@ -20,9 +20,9 @@ public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,s
           print("error \(error)")
         }
       }
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     if let requestMessage = requestMessage {
       return requestMessage
     } else {
@@ -37,13 +37,13 @@ public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,s
 
   /// Close a connection. Blocks until the connection is closed.
   public func close() throws {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     try self.handler.sendStatus(statusCode:self.statusCode,
                                 statusMessage:self.statusMessage,
                                 trailingMetadata:self.trailingMetadata) {
-                                  latch.signal()
+                                  sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 
   /// Run the session. Internal.

+ 3 - 3
Plugin/Templates/server-session-clientstreaming.swift

@@ -10,15 +10,15 @@ public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,s
 
   /// Receive a message. Blocks until a message is received or the client closes the connection.
   public func receive() throws -> {{ method|input }} {
-    let latch = CountDownLatch(1)
+    let sem = DispatchSemaphore(value: 0)
     var requestMessage : {{ method|input }}?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
         requestMessage = try? {{ method|input }}(protobuf:requestData)
       }
-      latch.signal()
+      sem.signal()
     }
-    latch.wait()
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
     if requestMessage == nil {
       throw {{ .|servererror:protoFile,service }}.endOfStream
     }

+ 1 - 1
Plugin/Templates/server.pb.swift

@@ -40,8 +40,8 @@
  */
 
 import Foundation
-import gRPC
 import Dispatch
+import gRPC
 //-{% for service in protoFile.service %}
 
 /// Type for errors thrown from generated server code.

Some files were not shown because too many files changed in this diff