-
Notifications
You must be signed in to change notification settings - Fork 75
/
generate_gvl_code.rb
executable file
·321 lines (237 loc) · 6.98 KB
/
generate_gvl_code.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env ruby
=begin
the idea here is to take each zoo_* function declaration in the header file, and turn it into
a calling arguments struct, a wrapper function and a macro for packing the values.
so for:
ZOOAPI int zoo_acreate(zhandle_t *zh, const char *path, const char *value,
int valuelen, const struct ACL_vector *acl, int flags,
string_completion_t completion, const void *data);
we want
typedef struct {
zhandle_t *zh;
const char *path;
const char *value;
int valuelen;
const struct ACL_vector *acl;
int flags;
string_completion_t completion;
const void *data;
} zkrb_zoo_acreate_args_t;
static VALUE zkrb_gvl_zoo_acreate(void *data) {
zkrb_zoo_acreate_args_t *a = (zkrb_zoo_acreate_args_t *)data;
a->rc = zoo_acreate(a->zh, a->path, a->value, a->valuelen, a->acl, a->flags, a->completion, a->data);
return Qnil;
}
static int zkrb_call_zoo_acreate(zhandle_t *zh, const char *path, const char *value,
int valuelen, const struct ACL_vector *acl, int flags,
string_completion_t completion, const void *data) {
zkrb_zoo_acreate_args_t args = {
.rc = ZKRB_FAIL,
.zh = zh,
.path = path,
.value = value,
.valuelen = valuelen,
.acl = acl,
.flags = flags,
.completion = completion,
.data = data
};
zkrb_thread_blocking_region(zkrb_gvl_zoo_acreate, (void *)&args);
return args.rc;
}
=end
REGEXP = /^ZOOAPI int (zoo_[^(]+)\(([^)]+)\);$/m
require 'forwardable'
require 'stringio'
THIS_DIR = File.expand_path('..', __FILE__)
ZKRB_WRAPPER_H_PATH = File.expand_path('../zkrb_wrapper.h', __FILE__)
ZKRB_WRAPPER_C_PATH = File.expand_path('../zkrb_wrapper.c', __FILE__)
# the struct that holds the call args for zoo_fn_name
class CallStruct
attr_reader :zoo_fn_name, :typed_args, :name
def initialize(zoo_fn_name, typed_args)
@zoo_fn_name, @typed_args = zoo_fn_name, typed_args
@name = "zkrb_#{zoo_fn_name}_args_t"
end
def body
@body ||= (
lines = ["typedef struct {"]
lines += typed_args.map{|n| " #{n};"}
lines << " int rc;"
lines << "} #{name};"
lines.join("\n")
)
end
end
module MemberNames
def member_names
@member_names ||= typed_args.map { |n| n.split(/[ *]/).last }
end
end
# the zkrb_gvl_zoo_* function
class WrapperFunction
extend Forwardable
include MemberNames
PREFIX = 'zkrb_gvl'
def_delegators :struct, :typed_args
attr_reader :struct, :name, :zoo_fn_name
def initialize(zoo_fn_name, struct)
@zoo_fn_name = zoo_fn_name
@struct = struct
@name = "#{PREFIX}_#{zoo_fn_name}"
end
def fn_signature
@fn_signature ||= "static VALUE #{name}(void *data)"
end
def body
@body ||= (
lines = ["#{fn_signature} {"]
lines << " #{struct.name} *a = (#{struct.name} *)data;"
funcall = " a->rc = #{zoo_fn_name}("
funcall << member_names.map { |m| "a->#{m}" }.join(', ')
funcall << ');'
lines << funcall
lines << " return Qnil;"
lines << "}"
lines.join("\n")
)
end
end
# the zkrb_call_zoo_* function
class CallingFunction
extend Forwardable
include MemberNames
PREFIX = 'zkrb_call'
def_delegators :struct, :typed_args
attr_reader :struct, :wrapper_fn, :zoo_fn_name, :name
def initialize(zoo_fn_name, struct, wrapper_fn)
@zoo_fn_name, @struct, @wrapper_fn = zoo_fn_name, struct, wrapper_fn
@name = "#{PREFIX}_#{zoo_fn_name}"
end
def fn_signature
@fn_signature ||= "int #{name}(#{typed_args.join(', ')})"
end
def initializer_lines
@initializer_lines ||= member_names.map { |n| " .#{n} = #{n}" }.join(",\n")
end
def top
<<-EOS
// wrapper that calls #{zoo_fn_name} via #{wrapper_fn.name} inside rb_thread_blocking_region
#{fn_signature} {
#{struct.name} args = {
.rc = ZKRB_FAIL,
#{initializer_lines}
};
EOS
end
def rb_thread_blocking_region_call
" zkrb_thread_blocking_region(#{wrapper_fn.name}, (void *)&args);"
end
def bottom
<<-EOS
return args.rc;
}
EOS
end
def body
@body ||= [top, rb_thread_blocking_region_call, bottom].join("\n")
end
end
class GeneratedCode < Struct.new(:structs, :wrapper_fns, :calling_fns)
def initialize(*a)
super
self.structs ||= []
self.wrapper_fns ||= []
self.calling_fns ||= []
end
def self.from_zookeeper_h(text)
new.tap do |code|
while true
break unless text =~ REGEXP
text = $~.post_match
zoo_fn_name, argstr = $1
argstr = $2
typed_args = argstr.split(',').map(&:strip)
# gah, fix up functions which have a void_completion_t with no name assigned
if idx = typed_args.index('void_completion_t')
typed_args[idx] = 'void_completion_t completion'
end
struct = CallStruct.new(zoo_fn_name, typed_args)
wrapper_fn = WrapperFunction.new(zoo_fn_name, struct)
calling_fn = CallingFunction.new(zoo_fn_name, struct, wrapper_fn)
code.structs << struct
code.wrapper_fns << wrapper_fn
code.calling_fns << calling_fn
end
end
end
end
def render_header_file(code)
StringIO.new('zkrb_wrapper.h', 'w').tap do |fp|
fp.puts <<-EOS
#ifndef ZKRB_WRAPPER_H
#define ZKRB_WRAPPER_H
#if 0
AUTOGENERATED BY #{File.basename(__FILE__)}
#endif
#include "ruby.h"
#include "zookeeper/zookeeper.h"
#include "zkrb_wrapper_compat.h"
#include "dbg.h"
#define ZKRB_FAIL -1
EOS
code.structs.each do |struct|
fp.puts(struct.body)
fp.puts
end
code.calling_fns.each do |cf|
fp.puts "#{cf.fn_signature};"
end
fp.puts <<-EOS
#endif /* ZKRB_WRAPPER_H */
EOS
end.string
end
def render_c_file(code)
StringIO.new('zkrb_wrapper.c', 'w').tap do |fp|
fp.puts <<-EOS
/*
Autogenerated boilerplate wrappers around zoo_* function calls necessary for using
rb_thread_blocking_region to release the GIL when calling native code.
generated by ext/#{File.basename(__FILE__)}
*/
#include "ruby.h"
#include "zkrb_wrapper.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
EOS
code.wrapper_fns.zip(code.calling_fns) do |wrap_fn, call_fn|
fp.puts "#{wrap_fn.body}\n\n"
fp.puts "#{call_fn.body}\n\n"
end
end.string
end
def help!
$stderr.puts "usage: #{File.basename(__FILE__)} {all|headers|code}"
exit 1
end
def main
help! if ARGV.empty?
opts = []
zookeeper_h_path = Dir[File.join(THIS_DIR, "**/zookeeper.h")].first
raise "Could not locate zookeeper.h!" unless zookeeper_h_path
text = File.read(zookeeper_h_path)
code = GeneratedCode.from_zookeeper_h(text)
cmd = ARGV.first
help! unless %w[headers all code].include?(cmd)
if %w[headers all].include?(cmd)
$stderr.puts "writing #{ZKRB_WRAPPER_H_PATH}"
File.open(ZKRB_WRAPPER_H_PATH, 'w') { |fp| fp.write(render_header_file(code)) }
end
if %w[code all].include?(cmd)
$stderr.puts "writing #{ZKRB_WRAPPER_C_PATH}"
File.open(ZKRB_WRAPPER_C_PATH, 'w') { |fp| fp.write(render_c_file(code)) }
end
end
main