GRPCCompletionQueue.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. *
  3. * Copyright 2015, 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. #import "GRPCCompletionQueue.h"
  34. #import <grpc/grpc.h>
  35. @implementation GRPCCompletionQueue
  36. + (instancetype)completionQueue {
  37. return [[self alloc] init];
  38. }
  39. - (instancetype)init {
  40. if ((self = [super init])) {
  41. _unmanagedQueue = grpc_completion_queue_create();
  42. // This is for the following block to capture the pointer by value (instead
  43. // of retaining self and doing self->_unmanagedQueue). This is essential
  44. // because the block doesn't end until after grpc_completion_queue_shutdown
  45. // is called, and we only want that to happen after nobody's using the queue
  46. // anymore (i.e. on self dealloc). So the block would never end if it
  47. // retained self.
  48. grpc_completion_queue *unmanagedQueue = _unmanagedQueue;
  49. // Start a loop on a concurrent queue to read events from the completion
  50. // queue and dispatch each.
  51. static dispatch_once_t initialization;
  52. static dispatch_queue_t gDefaultConcurrentQueue;
  53. dispatch_once(&initialization, ^{
  54. gDefaultConcurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  55. });
  56. dispatch_async(gDefaultConcurrentQueue, ^{
  57. while (YES) {
  58. // The following call blocks until an event is available.
  59. grpc_event event = grpc_completion_queue_next(unmanagedQueue,
  60. gpr_inf_future(GPR_CLOCK_REALTIME));
  61. GRPCQueueCompletionHandler handler;
  62. switch (event.type) {
  63. case GRPC_OP_COMPLETE:
  64. handler = (__bridge_transfer GRPCQueueCompletionHandler)event.tag;
  65. handler(event.success);
  66. break;
  67. case GRPC_QUEUE_SHUTDOWN:
  68. grpc_completion_queue_destroy(unmanagedQueue);
  69. return;
  70. default:
  71. [NSException raise:@"Unrecognized completion type" format:@""];
  72. }
  73. };
  74. });
  75. }
  76. return self;
  77. }
  78. - (void)dealloc {
  79. // This makes the completion queue produce a GRPC_QUEUE_SHUTDOWN event *after*
  80. // all other pending events are flushed. What this means is all the blocks
  81. // passed to the gRPC C library as void* are eventually called, even if some
  82. // are called after self is dealloc'd.
  83. grpc_completion_queue_shutdown(_unmanagedQueue);
  84. }
  85. @end