grpc 0.13.0.pre1.1-universal-darwin

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (95) hide show
  1. checksums.yaml +7 -0
  2. data/etc/roots.pem +5114 -0
  3. data/grpc_c.32.ruby +0 -0
  4. data/grpc_c.64.ruby +0 -0
  5. data/src/ruby/bin/apis/google/protobuf/empty.rb +44 -0
  6. data/src/ruby/bin/apis/pubsub_demo.rb +256 -0
  7. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub.rb +174 -0
  8. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub_services.rb +103 -0
  9. data/src/ruby/bin/grpc_ruby_interop_client +33 -0
  10. data/src/ruby/bin/grpc_ruby_interop_server +33 -0
  11. data/src/ruby/bin/interop/interop_client.rb +51 -0
  12. data/src/ruby/bin/interop/interop_server.rb +50 -0
  13. data/src/ruby/bin/math.rb +32 -0
  14. data/src/ruby/bin/math_client.rb +147 -0
  15. data/src/ruby/bin/math_server.rb +206 -0
  16. data/src/ruby/bin/math_services.rb +27 -0
  17. data/src/ruby/bin/noproto_client.rb +108 -0
  18. data/src/ruby/bin/noproto_server.rb +112 -0
  19. data/src/ruby/ext/grpc/extconf.rb +129 -0
  20. data/src/ruby/ext/grpc/rb_byte_buffer.c +70 -0
  21. data/src/ruby/ext/grpc/rb_byte_buffer.h +47 -0
  22. data/src/ruby/ext/grpc/rb_call.c +908 -0
  23. data/src/ruby/ext/grpc/rb_call.h +66 -0
  24. data/src/ruby/ext/grpc/rb_call_credentials.c +319 -0
  25. data/src/ruby/ext/grpc/rb_call_credentials.h +46 -0
  26. data/src/ruby/ext/grpc/rb_channel.c +432 -0
  27. data/src/ruby/ext/grpc/rb_channel.h +47 -0
  28. data/src/ruby/ext/grpc/rb_channel_args.c +169 -0
  29. data/src/ruby/ext/grpc/rb_channel_args.h +53 -0
  30. data/src/ruby/ext/grpc/rb_channel_credentials.c +268 -0
  31. data/src/ruby/ext/grpc/rb_channel_credentials.h +47 -0
  32. data/src/ruby/ext/grpc/rb_completion_queue.c +183 -0
  33. data/src/ruby/ext/grpc/rb_completion_queue.h +55 -0
  34. data/src/ruby/ext/grpc/rb_event_thread.c +158 -0
  35. data/src/ruby/ext/grpc/rb_event_thread.h +37 -0
  36. data/src/ruby/ext/grpc/rb_grpc.c +336 -0
  37. data/src/ruby/ext/grpc/rb_grpc.h +85 -0
  38. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +560 -0
  39. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +843 -0
  40. data/src/ruby/ext/grpc/rb_loader.c +72 -0
  41. data/src/ruby/ext/grpc/rb_loader.h +40 -0
  42. data/src/ruby/ext/grpc/rb_server.c +400 -0
  43. data/src/ruby/ext/grpc/rb_server.h +47 -0
  44. data/src/ruby/ext/grpc/rb_server_credentials.c +284 -0
  45. data/src/ruby/ext/grpc/rb_server_credentials.h +47 -0
  46. data/src/ruby/lib/grpc.rb +44 -0
  47. data/src/ruby/lib/grpc/2.0/grpc_c.bundle +0 -0
  48. data/src/ruby/lib/grpc/2.1/grpc_c.bundle +0 -0
  49. data/src/ruby/lib/grpc/2.2/grpc_c.bundle +0 -0
  50. data/src/ruby/lib/grpc/2.3/grpc_c.bundle +0 -0
  51. data/src/ruby/lib/grpc/core/time_consts.rb +71 -0
  52. data/src/ruby/lib/grpc/errors.rb +62 -0
  53. data/src/ruby/lib/grpc/generic/active_call.rb +488 -0
  54. data/src/ruby/lib/grpc/generic/bidi_call.rb +218 -0
  55. data/src/ruby/lib/grpc/generic/client_stub.rb +471 -0
  56. data/src/ruby/lib/grpc/generic/rpc_desc.rb +147 -0
  57. data/src/ruby/lib/grpc/generic/rpc_server.rb +504 -0
  58. data/src/ruby/lib/grpc/generic/service.rb +234 -0
  59. data/src/ruby/lib/grpc/grpc.rb +34 -0
  60. data/src/ruby/lib/grpc/logconfig.rb +59 -0
  61. data/src/ruby/lib/grpc/notifier.rb +60 -0
  62. data/src/ruby/lib/grpc/version.rb +33 -0
  63. data/src/ruby/pb/README.md +42 -0
  64. data/src/ruby/pb/generate_proto_ruby.sh +51 -0
  65. data/src/ruby/pb/grpc/health/checker.rb +75 -0
  66. data/src/ruby/pb/grpc/health/v1alpha/health.rb +29 -0
  67. data/src/ruby/pb/grpc/health/v1alpha/health_services.rb +28 -0
  68. data/src/ruby/pb/test/client.rb +469 -0
  69. data/src/ruby/pb/test/proto/empty.rb +15 -0
  70. data/src/ruby/pb/test/proto/messages.rb +80 -0
  71. data/src/ruby/pb/test/proto/test.rb +14 -0
  72. data/src/ruby/pb/test/proto/test_services.rb +64 -0
  73. data/src/ruby/pb/test/server.rb +253 -0
  74. data/src/ruby/spec/call_credentials_spec.rb +57 -0
  75. data/src/ruby/spec/call_spec.rb +163 -0
  76. data/src/ruby/spec/channel_credentials_spec.rb +97 -0
  77. data/src/ruby/spec/channel_spec.rb +177 -0
  78. data/src/ruby/spec/client_server_spec.rb +475 -0
  79. data/src/ruby/spec/completion_queue_spec.rb +42 -0
  80. data/src/ruby/spec/generic/active_call_spec.rb +373 -0
  81. data/src/ruby/spec/generic/client_stub_spec.rb +476 -0
  82. data/src/ruby/spec/generic/rpc_desc_spec.rb +331 -0
  83. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +138 -0
  84. data/src/ruby/spec/generic/rpc_server_spec.rb +576 -0
  85. data/src/ruby/spec/generic/service_spec.rb +345 -0
  86. data/src/ruby/spec/pb/health/checker_spec.rb +232 -0
  87. data/src/ruby/spec/server_credentials_spec.rb +94 -0
  88. data/src/ruby/spec/server_spec.rb +209 -0
  89. data/src/ruby/spec/spec_helper.rb +69 -0
  90. data/src/ruby/spec/testdata/README +1 -0
  91. data/src/ruby/spec/testdata/ca.pem +15 -0
  92. data/src/ruby/spec/testdata/server1.key +16 -0
  93. data/src/ruby/spec/testdata/server1.pem +16 -0
  94. data/src/ruby/spec/time_consts_spec.rb +89 -0
  95. metadata +319 -0
@@ -0,0 +1,47 @@
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
+
34
+ #ifndef GRPC_RB_CREDENTIALS_H_
35
+ #define GRPC_RB_CREDENTIALS_H_
36
+
37
+ #include <ruby/ruby.h>
38
+
39
+ #include <grpc/grpc_security.h>
40
+
41
+ /* Initializes the ruby ChannelCredentials class. */
42
+ void Init_grpc_channel_credentials();
43
+
44
+ /* Gets the wrapped credentials from the ruby wrapper */
45
+ grpc_channel_credentials* grpc_rb_get_wrapped_channel_credentials(VALUE v);
46
+
47
+ #endif /* GRPC_RB_CREDENTIALS_H_ */
@@ -0,0 +1,183 @@
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
+ #include <ruby/ruby.h>
35
+ #include "rb_grpc_imports.generated.h"
36
+ #include "rb_completion_queue.h"
37
+
38
+ #include <ruby/ruby.h>
39
+ #include <ruby/thread.h>
40
+
41
+ #include <grpc/grpc.h>
42
+ #include <grpc/support/time.h>
43
+ #include "rb_grpc.h"
44
+
45
+ /* grpc_rb_cCompletionQueue is the ruby class that proxies
46
+ * grpc_completion_queue. */
47
+ static VALUE grpc_rb_cCompletionQueue = Qnil;
48
+
49
+ /* Used to allow grpc_completion_queue_next call to release the GIL */
50
+ typedef struct next_call_stack {
51
+ grpc_completion_queue *cq;
52
+ grpc_event event;
53
+ gpr_timespec timeout;
54
+ void *tag;
55
+ } next_call_stack;
56
+
57
+ /* Calls grpc_completion_queue_next without holding the ruby GIL */
58
+ static void *grpc_rb_completion_queue_next_no_gil(void *param) {
59
+ next_call_stack *const next_call = (next_call_stack*)param;
60
+ next_call->event =
61
+ grpc_completion_queue_next(next_call->cq, next_call->timeout, NULL);
62
+ return NULL;
63
+ }
64
+
65
+ /* Calls grpc_completion_queue_pluck without holding the ruby GIL */
66
+ static void *grpc_rb_completion_queue_pluck_no_gil(void *param) {
67
+ next_call_stack *const next_call = (next_call_stack*)param;
68
+ next_call->event = grpc_completion_queue_pluck(next_call->cq, next_call->tag,
69
+ next_call->timeout, NULL);
70
+ return NULL;
71
+ }
72
+
73
+ /* Shuts down and drains the completion queue if necessary.
74
+ *
75
+ * This is done when the ruby completion queue object is about to be GCed.
76
+ */
77
+ static void grpc_rb_completion_queue_shutdown_drain(grpc_completion_queue *cq) {
78
+ next_call_stack next_call;
79
+ grpc_completion_type type;
80
+ int drained = 0;
81
+ MEMZERO(&next_call, next_call_stack, 1);
82
+
83
+ grpc_completion_queue_shutdown(cq);
84
+ next_call.cq = cq;
85
+ next_call.event.type = GRPC_QUEUE_TIMEOUT;
86
+ /* TODO: the timeout should be a module level constant that defaults
87
+ * to gpr_inf_future(GPR_CLOCK_REALTIME).
88
+ *
89
+ * - at the moment this does not work, it stalls. Using a small timeout like
90
+ * this one works, and leads to fast test run times; a longer timeout was
91
+ * causing unnecessary delays in the test runs.
92
+ *
93
+ * - investigate further, this is probably another example of C-level cleanup
94
+ * not working consistently in all cases.
95
+ */
96
+ next_call.timeout = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
97
+ gpr_time_from_micros(5e3, GPR_TIMESPAN));
98
+ do {
99
+ rb_thread_call_without_gvl(grpc_rb_completion_queue_next_no_gil,
100
+ (void *)&next_call, NULL, NULL);
101
+ type = next_call.event.type;
102
+ if (type == GRPC_QUEUE_TIMEOUT) break;
103
+ if (type != GRPC_QUEUE_SHUTDOWN) {
104
+ ++drained;
105
+ rb_warning("completion queue shutdown: %d undrained events", drained);
106
+ }
107
+ } while (type != GRPC_QUEUE_SHUTDOWN);
108
+ }
109
+
110
+ /* Helper function to free a completion queue. */
111
+ static void grpc_rb_completion_queue_destroy(void *p) {
112
+ grpc_completion_queue *cq = NULL;
113
+ if (p == NULL) {
114
+ return;
115
+ }
116
+ cq = (grpc_completion_queue *)p;
117
+ grpc_rb_completion_queue_shutdown_drain(cq);
118
+ grpc_completion_queue_destroy(cq);
119
+ }
120
+
121
+ static rb_data_type_t grpc_rb_completion_queue_data_type = {
122
+ "grpc_completion_queue",
123
+ {GRPC_RB_GC_NOT_MARKED, grpc_rb_completion_queue_destroy,
124
+ GRPC_RB_MEMSIZE_UNAVAILABLE, {NULL, NULL}},
125
+ NULL, NULL,
126
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
127
+ /* cannot immediately free because grpc_rb_completion_queue_shutdown_drain
128
+ * calls rb_thread_call_without_gvl. */
129
+ 0,
130
+ #endif
131
+ };
132
+
133
+ /* Allocates a completion queue. */
134
+ static VALUE grpc_rb_completion_queue_alloc(VALUE cls) {
135
+ grpc_completion_queue *cq = grpc_completion_queue_create(NULL);
136
+ if (cq == NULL) {
137
+ rb_raise(rb_eArgError, "could not create a completion queue: not sure why");
138
+ }
139
+ return TypedData_Wrap_Struct(cls, &grpc_rb_completion_queue_data_type, cq);
140
+ }
141
+
142
+ /* Blocks until the next event for given tag is available, and returns the
143
+ * event. */
144
+ grpc_event grpc_rb_completion_queue_pluck_event(VALUE self, VALUE tag,
145
+ VALUE timeout) {
146
+ next_call_stack next_call;
147
+ MEMZERO(&next_call, next_call_stack, 1);
148
+ TypedData_Get_Struct(self, grpc_completion_queue,
149
+ &grpc_rb_completion_queue_data_type, next_call.cq);
150
+ if (TYPE(timeout) == T_NIL) {
151
+ next_call.timeout = gpr_inf_future(GPR_CLOCK_REALTIME);
152
+ } else {
153
+ next_call.timeout = grpc_rb_time_timeval(timeout, /* absolute time*/ 0);
154
+ }
155
+ if (TYPE(tag) == T_NIL) {
156
+ next_call.tag = NULL;
157
+ } else {
158
+ next_call.tag = ROBJECT(tag);
159
+ }
160
+ next_call.event.type = GRPC_QUEUE_TIMEOUT;
161
+ rb_thread_call_without_gvl(grpc_rb_completion_queue_pluck_no_gil,
162
+ (void *)&next_call, NULL, NULL);
163
+ return next_call.event;
164
+ }
165
+
166
+ void Init_grpc_completion_queue() {
167
+ grpc_rb_cCompletionQueue =
168
+ rb_define_class_under(grpc_rb_mGrpcCore, "CompletionQueue", rb_cObject);
169
+
170
+ /* constructor: uses an alloc func without an initializer. Using a simple
171
+ alloc func works here as the grpc header does not specify any args for
172
+ this func, so no separate initialization step is necessary. */
173
+ rb_define_alloc_func(grpc_rb_cCompletionQueue,
174
+ grpc_rb_completion_queue_alloc);
175
+ }
176
+
177
+ /* Gets the wrapped completion queue from the ruby wrapper */
178
+ grpc_completion_queue *grpc_rb_get_wrapped_completion_queue(VALUE v) {
179
+ grpc_completion_queue *cq = NULL;
180
+ TypedData_Get_Struct(v, grpc_completion_queue,
181
+ &grpc_rb_completion_queue_data_type, cq);
182
+ return cq;
183
+ }
@@ -0,0 +1,55 @@
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
+
34
+ #ifndef GRPC_RB_COMPLETION_QUEUE_H_
35
+ #define GRPC_RB_COMPLETION_QUEUE_H_
36
+
37
+ #include <ruby/ruby.h>
38
+
39
+ #include <grpc/grpc.h>
40
+
41
+ /* Gets the wrapped completion queue from the ruby wrapper */
42
+ grpc_completion_queue *grpc_rb_get_wrapped_completion_queue(VALUE v);
43
+
44
+ /**
45
+ * Makes the implementation of CompletionQueue#pluck available in other files
46
+ *
47
+ * This avoids having code that holds the GIL repeated at multiple sites.
48
+ */
49
+ grpc_event grpc_rb_completion_queue_pluck_event(VALUE cqueue, VALUE tag,
50
+ VALUE timeout);
51
+
52
+ /* Initializes the CompletionQueue class. */
53
+ void Init_grpc_completion_queue();
54
+
55
+ #endif /* GRPC_RB_COMPLETION_QUEUE_H_ */
@@ -0,0 +1,158 @@
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
+
34
+ #include <ruby/ruby.h>
35
+ #include "rb_grpc_imports.generated.h"
36
+ #include "rb_event_thread.h"
37
+
38
+ #include <stdbool.h>
39
+
40
+ #include <ruby/ruby.h>
41
+ #include <ruby/thread.h>
42
+ #include <grpc/support/alloc.h>
43
+ #include <grpc/support/sync.h>
44
+ #include <grpc/support/time.h>
45
+ #include <grpc/support/log.h>
46
+
47
+ typedef struct grpc_rb_event {
48
+ // callback will be called with argument while holding the GVL
49
+ void (*callback)(void*);
50
+ void *argument;
51
+
52
+ struct grpc_rb_event *next;
53
+ } grpc_rb_event;
54
+
55
+ typedef struct grpc_rb_event_queue {
56
+ grpc_rb_event *head;
57
+ grpc_rb_event *tail;
58
+
59
+ gpr_mu mu;
60
+ gpr_cv cv;
61
+
62
+ // Indicates that the thread should stop waiting
63
+ bool abort;
64
+ } grpc_rb_event_queue;
65
+
66
+ static grpc_rb_event_queue event_queue;
67
+
68
+ void grpc_rb_event_queue_enqueue(void (*callback)(void*),
69
+ void *argument) {
70
+ grpc_rb_event *event = gpr_malloc(sizeof(grpc_rb_event));
71
+ event->callback = callback;
72
+ event->argument = argument;
73
+ event->next = NULL;
74
+ gpr_mu_lock(&event_queue.mu);
75
+ if (event_queue.tail == NULL) {
76
+ event_queue.head = event_queue.tail = event;
77
+ } else {
78
+ event_queue.tail->next = event;
79
+ event_queue.tail = event;
80
+ }
81
+ gpr_cv_signal(&event_queue.cv);
82
+ gpr_mu_unlock(&event_queue.mu);
83
+ }
84
+
85
+ static grpc_rb_event *grpc_rb_event_queue_dequeue() {
86
+ grpc_rb_event *event;
87
+ if (event_queue.head == NULL) {
88
+ event = NULL;
89
+ } else {
90
+ event = event_queue.head;
91
+ if (event_queue.head->next == NULL) {
92
+ event_queue.head = event_queue.tail = NULL;
93
+ } else {
94
+ event_queue.head = event_queue.head->next;
95
+ }
96
+ }
97
+ return event;
98
+ }
99
+
100
+ static void grpc_rb_event_queue_destroy() {
101
+ gpr_mu_destroy(&event_queue.mu);
102
+ gpr_cv_destroy(&event_queue.cv);
103
+ }
104
+
105
+ static void *grpc_rb_wait_for_event_no_gil(void *param) {
106
+ grpc_rb_event *event = NULL;
107
+ (void)param;
108
+ gpr_mu_lock(&event_queue.mu);
109
+ while ((event = grpc_rb_event_queue_dequeue()) == NULL) {
110
+ gpr_cv_wait(&event_queue.cv,
111
+ &event_queue.mu,
112
+ gpr_inf_future(GPR_CLOCK_REALTIME));
113
+ if (event_queue.abort) {
114
+ gpr_mu_unlock(&event_queue.mu);
115
+ return NULL;
116
+ }
117
+ }
118
+ gpr_mu_unlock(&event_queue.mu);
119
+ return event;
120
+ }
121
+
122
+ static void grpc_rb_event_unblocking_func(void *arg) {
123
+ (void)arg;
124
+ gpr_mu_lock(&event_queue.mu);
125
+ event_queue.abort = true;
126
+ gpr_cv_signal(&event_queue.cv);
127
+ gpr_mu_unlock(&event_queue.mu);
128
+ }
129
+
130
+ /* This is the implementation of the thread that handles auth metadata plugin
131
+ * events */
132
+ static VALUE grpc_rb_event_thread(VALUE arg) {
133
+ grpc_rb_event *event;
134
+ (void)arg;
135
+ while(true) {
136
+ event = (grpc_rb_event*)rb_thread_call_without_gvl(
137
+ grpc_rb_wait_for_event_no_gil, NULL,
138
+ grpc_rb_event_unblocking_func, NULL);
139
+ if (event == NULL) {
140
+ // Indicates that the thread needs to shut down
141
+ break;
142
+ } else {
143
+ event->callback(event->argument);
144
+ gpr_free(event);
145
+ }
146
+ }
147
+ grpc_rb_event_queue_destroy();
148
+ return Qnil;
149
+ }
150
+
151
+ void grpc_rb_event_queue_thread_start() {
152
+ event_queue.head = event_queue.tail = NULL;
153
+ event_queue.abort = false;
154
+ gpr_mu_init(&event_queue.mu);
155
+ gpr_cv_init(&event_queue.cv);
156
+
157
+ rb_thread_create(grpc_rb_event_thread, NULL);
158
+ }
@@ -0,0 +1,37 @@
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
+
34
+ void grpc_rb_event_queue_thread_start();
35
+
36
+ void grpc_rb_event_queue_enqueue(void (*callback)(void*),
37
+ void *argument);