rb_event_thread.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <ruby/ruby.h>
20 
21 #include "rb_event_thread.h"
22 
23 #include <ruby/thread.h>
24 #include <stdbool.h>
25 
26 #include "rb_grpc.h"
28 
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/sync.h>
32 #include <grpc/support/time.h>
33 
34 typedef struct grpc_rb_event {
35  // callback will be called with argument while holding the GVL
36  void (*callback)(void*);
37  void* argument;
38 
41 
42 typedef struct grpc_rb_event_queue {
45 
48 
49  // Indicates that the thread should stop waiting
50  bool abort;
52 
54 
55 void grpc_rb_event_queue_enqueue(void (*callback)(void*), void* argument) {
56  grpc_rb_event* event = gpr_malloc(sizeof(grpc_rb_event));
57  event->callback = callback;
58  event->argument = argument;
59  event->next = NULL;
61  if (event_queue.tail == NULL) {
62  event_queue.head = event_queue.tail = event;
63  } else {
64  event_queue.tail->next = event;
65  event_queue.tail = event;
66  }
69 }
70 
72  grpc_rb_event* event;
73  if (event_queue.head == NULL) {
74  event = NULL;
75  } else {
76  event = event_queue.head;
77  if (event_queue.head->next == NULL) {
79  } else {
81  }
82  }
83  return event;
84 }
85 
89 }
90 
91 static void* grpc_rb_wait_for_event_no_gil(void* param) {
92  grpc_rb_event* event = NULL;
93  (void)param;
95  while (!event_queue.abort) {
96  if ((event = grpc_rb_event_queue_dequeue()) != NULL) {
98  return event;
99  }
102  }
104  return NULL;
105 }
106 
107 static void grpc_rb_event_unblocking_func(void* arg) {
108  (void)arg;
110  event_queue.abort = true;
113 }
114 
115 /* This is the implementation of the thread that handles auth metadata plugin
116  * events */
117 static VALUE grpc_rb_event_thread(VALUE arg) {
118  grpc_rb_event* event;
119  (void)arg;
120  grpc_ruby_init();
121  while (true) {
122  event = (grpc_rb_event*)rb_thread_call_without_gvl(
124  NULL);
125  if (event == NULL) {
126  // Indicates that the thread needs to shut down
127  break;
128  } else {
129  event->callback(event->argument);
130  gpr_free(event);
131  }
132  }
135  return Qnil;
136 }
137 
139  event_queue.head = event_queue.tail = NULL;
140  event_queue.abort = false;
143 
144  rb_thread_create(grpc_rb_event_thread, NULL);
145 }
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
grpc_rb_event_queue
Definition: rb_event_thread.c:42
grpc_rb_event_queue_dequeue
static grpc_rb_event * grpc_rb_event_queue_dequeue()
Definition: rb_event_thread.c:71
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
log.h
grpc_rb_event_queue::cv
gpr_cv cv
Definition: rb_event_thread.c:47
rb_grpc_imports.generated.h
grpc_rb_event::argument
void * argument
Definition: rb_event_thread.c:37
grpc_ruby_shutdown
void grpc_ruby_shutdown()
Definition: rb_grpc.c:296
grpc_rb_event::next
struct grpc_rb_event * next
Definition: rb_event_thread.c:39
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
time.h
grpc_rb_event
struct grpc_rb_event grpc_rb_event
grpc_rb_event_queue_thread_start
void grpc_rb_event_queue_thread_start()
Definition: rb_event_thread.c:138
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
grpc_rb_wait_for_event_no_gil
static void * grpc_rb_wait_for_event_no_gil(void *param)
Definition: rb_event_thread.c:91
grpc_rb_event_queue::head
grpc_rb_event * head
Definition: rb_event_thread.c:43
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc_rb_event_queue::tail
grpc_rb_event * tail
Definition: rb_event_thread.c:44
argument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:108
grpc_rb_event_unblocking_func
static void grpc_rb_event_unblocking_func(void *arg)
Definition: rb_event_thread.c:107
arg
Definition: cmdline.cc:40
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
grpc_rb_event::callback
void(* callback)(void *)
Definition: rb_event_thread.c:36
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
grpc_rb_event_queue
struct grpc_rb_event_queue grpc_rb_event_queue
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
grpc_rb_event_queue_enqueue
void grpc_rb_event_queue_enqueue(void(*callback)(void *), void *argument)
Definition: rb_event_thread.c:55
grpc_rb_event_queue_destroy
static void grpc_rb_event_queue_destroy()
Definition: rb_event_thread.c:86
rb_grpc.h
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
grpc_rb_event
Definition: rb_event_thread.c:34
alloc.h
grpc_rb_event_queue::mu
gpr_mu mu
Definition: rb_event_thread.c:46
grpc_rb_event_thread
static VALUE grpc_rb_event_thread(VALUE arg)
Definition: rb_event_thread.c:117
rb_event_thread.h
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
event_queue
static grpc_rb_event_queue event_queue
Definition: rb_event_thread.c:53
sync.h
grpc_rb_event_queue::abort
bool abort
Definition: rb_event_thread.c:50
grpc_ruby_init
void grpc_ruby_init()
Definition: rb_grpc.c:286
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:59