Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: third_party/grpc/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-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
34 #import "GRPCCompletionQueue.h"
35
36 #import <grpc/grpc.h>
37
38 @implementation GRPCCompletionQueue
39
40 + (instancetype)completionQueue {
41 return [[self alloc] init];
42 }
43
44 - (instancetype)init {
45 if ((self = [super init])) {
46 _unmanagedQueue = grpc_completion_queue_create(NULL);
47
48 // This is for the following block to capture the pointer by value (instead
49 // of retaining self and doing self->_unmanagedQueue). This is essential
50 // because the block doesn't end until after grpc_completion_queue_shutdown
51 // is called, and we only want that to happen after nobody's using the queue
52 // anymore (i.e. on self dealloc). So the block would never end if it
53 // retained self.
54 grpc_completion_queue *unmanagedQueue = _unmanagedQueue;
55
56 // Start a loop on a concurrent queue to read events from the completion
57 // queue and dispatch each.
58 static dispatch_once_t initialization;
59 static dispatch_queue_t gDefaultConcurrentQueue;
60 dispatch_once(&initialization, ^{
61 gDefaultConcurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORIT Y_DEFAULT, 0);
62 });
63 dispatch_async(gDefaultConcurrentQueue, ^{
64 while (YES) {
65 // The following call blocks until an event is available.
66 grpc_event event = grpc_completion_queue_next(unmanagedQueue,
67 gpr_inf_future(GPR_CLOCK_R EALTIME),
68 NULL);
69 GRPCQueueCompletionHandler handler;
70 switch (event.type) {
71 case GRPC_OP_COMPLETE:
72 handler = (__bridge_transfer GRPCQueueCompletionHandler)event.tag;
73 handler(event.success);
74 break;
75 case GRPC_QUEUE_SHUTDOWN:
76 grpc_completion_queue_destroy(unmanagedQueue);
77 return;
78 default:
79 [NSException raise:@"Unrecognized completion type" format:@""];
80 }
81 };
82 });
83 }
84 return self;
85 }
86
87 - (void)dealloc {
88 // This makes the completion queue produce a GRPC_QUEUE_SHUTDOWN event *after*
89 // all other pending events are flushed. What this means is all the blocks
90 // passed to the gRPC C library as void* are eventually called, even if some
91 // are called after self is dealloc'd.
92 grpc_completion_queue_shutdown(_unmanagedQueue);
93 }
94 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698