Top Level Namespace

Defined Under Namespace

Modules: Kernel, MemberNames, Zookeeper, ZookeeperACLs, ZookeeperCallbacks, ZookeeperCommon, ZookeeperConstants, ZookeeperEM, ZookeeperExceptions, ZookeeperStat Classes: CallStruct, CallingFunction, Exception, GeneratedCode, WrapperFunction

Constant Summary collapse

HERE =
File.expand_path(File.dirname(__FILE__))
BUNDLE =
Dir.glob("zkc-*.tar.gz").first
BUNDLE_PATH =
File.join(HERE, 'c')
ZK_DEBUG =
(ENV['DEBUG'] or ARGV.any? { |arg| arg == '--debug' })
ZK_DEV =
ENV['ZK_DEV']
DEBUG_CFLAGS =
" -O0 -ggdb3 -DHAVE_DEBUG -fstack-protector-all"
REGEXP =

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;
}
/^ZOOAPI int (zoo_[^(]+)\(([^)]+)\);$/m
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__)

Instance Method Summary collapse

Instance Method Details

#help!Object


290
291
292
293
# File 'ext/generate_gvl_code.rb', line 290

def help!
  $stderr.puts "usage: #{File.basename(__FILE__)} {all|headers|code}"
  exit 1
end

#mainObject


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
# File 'ext/generate_gvl_code.rb', line 295

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

#render_c_file(code) ⇒ Object


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
# File 'ext/generate_gvl_code.rb', line 261

def render_c_file(code)
  StringIO.new('zkrb_wrapper.c', 'w').tap do |fp|
    fp.puts "/*\n\nAutogenerated boilerplate wrappers around zoo_* function calls necessary for using\nrb_thread_blocking_region to release the GIL when calling native code.\n\ngenerated by ext/\#{File.basename(__FILE__)}\n\n*/\n\n#include \"ruby.h\"\n#include \"zkrb_wrapper.h\"\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n\n"

    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

#render_header_file(code) ⇒ Object


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
# File 'ext/generate_gvl_code.rb', line 224

def render_header_file(code)
  StringIO.new('zkrb_wrapper.h', 'w').tap do |fp|
    fp.puts "#ifndef ZKRB_WRAPPER_H\n#define ZKRB_WRAPPER_H\n#if 0\n\nAUTOGENERATED BY \#{File.basename(__FILE__)}\n\n#endif\n\n#include \"ruby.h\"\n#include \"c-client-src/zookeeper.h\"\n#include \"zkrb_wrapper_compat.h\"\n#include \"dbg.h\"\n\n#define ZKRB_FAIL -1\n\n"

    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 "\n#endif /* ZKRB_WRAPPER_H */\n"

  end.string
end

#safe_sh(cmd) ⇒ Object


45
46
47
48
49
50
51
# File 'ext/extconf.rb', line 45

def safe_sh(cmd)
  puts cmd
  system(cmd)
  unless $?.exited? and $?.success?
    raise "command failed! #{cmd}"
  end
end