Переглянути джерело

Convert generated code to use CountDownLatch instead of NSCondition.

Tim Burks 9 роки тому
батько
коміт
b1845c5add

+ 24 - 56
Examples/Echo/Generated/echo.client.pb.swift

@@ -60,7 +60,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 done = NSCondition()
+    let latch = CountDownLatch(1)
     var callResult : CallResult!
     var response : Echo_EchoResponse?
     let requestData = try request.serializeProtobuf()
@@ -72,13 +72,9 @@ public class Echo_EchoGetCall {
       if let responseData = callResult.resultData {
         response = try? Echo_EchoResponse(protobuf:responseData)
       }
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     if let response = response {
       return response
     } else {
@@ -99,18 +95,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 done = NSCondition()
+    let latch = CountDownLatch(1)
     try call.start(.serverStreaming,
                    metadata:metadata,
                    message:requestData)
     {callResult in
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     return self
   }
 
@@ -118,7 +110,7 @@ public class Echo_EchoExpandCall {
   public func Receive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var response : Echo_EchoResponse!
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData {
@@ -129,13 +121,9 @@ public class Echo_EchoExpandCall {
         } else {
           returnError = Echo_EchoClientError.endOfStream
         }
-        done.lock()
-        done.signal()
-        done.unlock()
+        latch.signal()
       }
-      done.lock()
-      done.wait()
-      done.unlock()
+      latch.wait()
     }
     if let returnError = returnError {
       throw returnError
@@ -155,17 +143,13 @@ public class Echo_EchoCollectCall {
 
   // Call this to start a call.
   fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     try self.call.start(.clientStreaming,
                         metadata:metadata)
     {callResult in
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     return self
   }
 
@@ -179,7 +163,7 @@ public class Echo_EchoCollectCall {
   public func CloseAndReceive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var returnResponse : Echo_EchoResponse!
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData,
@@ -188,14 +172,10 @@ public class Echo_EchoCollectCall {
         } else {
           returnError = Echo_EchoClientError.invalidMessageReceived
         }
-        done.lock()
-        done.signal()
-        done.unlock()
+        latch.signal()
       }
       try call.close(completion:{})
-      done.lock()
-      done.wait()
-      done.unlock()
+      latch.wait()
     } catch (let error) {
       throw error
     }
@@ -216,24 +196,20 @@ public class Echo_EchoUpdateCall {
   }
 
   fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     try self.call.start(.bidiStreaming,
                         metadata:metadata)
     {callResult in
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     return self
   }
 
   public func Receive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var returnMessage : Echo_EchoResponse!
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     do {
       try call.receiveMessage() {(data) in
         if let data = data {
@@ -244,13 +220,9 @@ public class Echo_EchoUpdateCall {
         } else {
           returnError = Echo_EchoClientError.endOfStream
         }
-        done.lock()
-        done.signal()
-        done.unlock()
+        latch.signal()
       }
-      done.lock()
-      done.wait()
-      done.unlock()
+      latch.wait()
     }
     if let returnError = returnError {
       throw returnError
@@ -264,15 +236,11 @@ public class Echo_EchoUpdateCall {
   }
 
   public func CloseSend() throws {
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     try call.close() {
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
   }
 }
 

+ 9 - 21
Examples/Echo/Generated/echo.server.pb.swift

@@ -150,19 +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 done = NSCondition()
+    let latch = CountDownLatch(1)
     var requestMessage : Echo_EchoRequest?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
         requestMessage = try? Echo_EchoRequest(protobuf:requestData)
       }
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     if requestMessage == nil {
       throw Echo_EchoServerError.endOfStream
     }
@@ -203,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 done = NSCondition()
+    let latch = CountDownLatch(1)
     var requestMessage : Echo_EchoRequest?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
@@ -213,13 +209,9 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
           print("error \(error)")
         }
       }
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     if let requestMessage = requestMessage {
       return requestMessage
     } else {
@@ -234,17 +226,13 @@ public class Echo_EchoUpdateSession : Echo_EchoSession {
 
   /// Close a connection. Blocks until the connection is closed.
   public func Close() throws {
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     try self.handler.sendStatus(statusCode:self.statusCode,
                                 statusMessage:self.statusMessage,
                                 trailingMetadata:self.trailingMetadata) {
-                                  done.lock()
-                                  done.signal()
-                                  done.unlock()
+                                  latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
   }
 
   /// Run the session. Internal.

Різницю між файлами не показано, бо вона завелика
+ 0 - 0
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift


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

@@ -8,24 +8,20 @@ public class {{ .|call:protoFile,service,method }} {
   }
 
   fileprivate func run(metadata:Metadata) throws -> {{ .|call:protoFile,service,method }} {
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     try self.call.start(.bidiStreaming,
                         metadata:metadata)
     {callResult in
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     return self
   }
 
   public func Receive() throws -> {{ method|output }} {
     var returnError : {{ .|clienterror:protoFile,service }}?
     var returnMessage : {{ method|output }}!
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     do {
       try call.receiveMessage() {(data) in
         if let data = data {
@@ -36,13 +32,9 @@ public class {{ .|call:protoFile,service,method }} {
         } else {
           returnError = {{ .|clienterror:protoFile,service }}.endOfStream
         }
-        done.lock()
-        done.signal()
-        done.unlock()
+        latch.signal()
       }
-      done.lock()
-      done.wait()
-      done.unlock()
+      latch.wait()
     }
     if let returnError = returnError {
       throw returnError
@@ -56,14 +48,10 @@ public class {{ .|call:protoFile,service,method }} {
   }
 
   public func CloseSend() throws {
-    let done = NSCondition()
+    let latch = CountDownLatch(1)
     try call.close() {
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
   }
 }

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

@@ -9,17 +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 done = NSCondition()
+    let latch = CountDownLatch(1)
     try self.call.start(.clientStreaming,
                         metadata:metadata)
     {callResult in
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     return self
   }
 
@@ -33,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 done = NSCondition()
+    let latch = CountDownLatch(1)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData,
@@ -42,14 +38,10 @@ public class {{ .|call:protoFile,service,method }} {
         } else {
           returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
         }
-        done.lock()
-        done.signal()
-        done.unlock()
+        latch.signal()
       }
       try call.close(completion:{})
-      done.lock()
-      done.wait()
-      done.unlock()
+      latch.wait()
     } catch (let error) {
       throw error
     }

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

@@ -10,18 +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 done = NSCondition()
+    let latch = CountDownLatch(1)
     try call.start(.serverStreaming,
                    metadata:metadata,
                    message:requestData)
     {callResult in
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     return self
   }
 
@@ -29,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 done = NSCondition()
+    let latch = CountDownLatch(1)
     do {
       try call.receiveMessage() {(responseData) in
         if let responseData = responseData {
@@ -40,13 +36,9 @@ public class {{ .|call:protoFile,service,method }} {
         } else {
           returnError = {{ .|clienterror:protoFile,service }}.endOfStream
         }
-        done.lock()
-        done.signal()
-        done.unlock()
+        latch.signal()
       }
-      done.lock()
-      done.wait()
-      done.unlock()
+      latch.wait()
     }
     if let returnError = returnError {
       throw returnError

+ 3 - 7
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 done = NSCondition()
+    let latch = CountDownLatch(1)
     var callResult : CallResult!
     var response : {{ method|output }}?
     let requestData = try request.serializeProtobuf()
@@ -22,13 +22,9 @@ public class {{ .|call:protoFile,service,method }} {
       if let responseData = callResult.resultData {
         response = try? {{ method|output }}(protobuf:responseData)
       }
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     if let response = response {
       return response
     } else {

+ 6 - 14
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 done = NSCondition()
+    let latch = CountDownLatch(1)
     var requestMessage : {{ method|input }}?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
@@ -20,13 +20,9 @@ public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,s
           print("error \(error)")
         }
       }
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     if let requestMessage = requestMessage {
       return requestMessage
     } else {
@@ -41,17 +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 done = NSCondition()
+    let latch = CountDownLatch(1)
     try self.handler.sendStatus(statusCode:self.statusCode,
                                 statusMessage:self.statusMessage,
                                 trailingMetadata:self.trailingMetadata) {
-                                  done.lock()
-                                  done.signal()
-                                  done.unlock()
+                                  latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
   }
 
   /// Run the session. Internal.

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

@@ -10,19 +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 done = NSCondition()
+    let latch = CountDownLatch(1)
     var requestMessage : {{ method|input }}?
     try self.handler.receiveMessage() {(requestData) in
       if let requestData = requestData {
         requestMessage = try? {{ method|input }}(protobuf:requestData)
       }
-      done.lock()
-      done.signal()
-      done.unlock()
+      latch.signal()
     }
-    done.lock()
-    done.wait()
-    done.unlock()
+    latch.wait()
     if requestMessage == nil {
       throw {{ .|servererror:protoFile,service }}.endOfStream
     }

Деякі файли не було показано, через те що забагато файлів було змінено