Пример #1
0
static void * rpmrubyThread(void * _ruby)
{
    rpmruby ruby = _ruby;
    rpmzLog zlog = ruby->zlog;
    int i;

    Trace((zlog, "-- %s: running", __FUNCTION__));

    _rpmruby_ruby_to_main(ruby, Qnil);

    for (i = 0; i < 2; i++)
        _rpmruby_ruby_to_main(ruby, Qnil);

    {
	VALUE variable_in_this_stack_frame;
	uint8_t * b = ruby->stack;
	uint8_t * e = b + ruby->nstack;

	/* Start up the ruby interpreter. */
	Trace((zlog, "-- %s: interpreter starting", __FUNCTION__));
	ruby_sysinit(&ruby->ac, (char ***) &ruby->av);

        ruby_bind_stack((VALUE *)b, (VALUE *)e);

	ruby_init_stack(&variable_in_this_stack_frame);
        ruby_init();
	ruby_init_loadpath();

        /* allow Ruby script to relay */
        rb_define_module_function(rb_mKernel, "relay_from_ruby_to_main",
                                  relay_from_ruby_to_main, 0);
	Trace((zlog, "-- %s: interpreter started", __FUNCTION__));

	/* Run file.rb arguments. */
	for (i = 1; i < ruby->ac; i++) {
	    if (*ruby->av[i] == '-')	/* XXX FIXME: skip options. */
		continue;
	    Trace((zlog, "-- %s: require '%s' begin", __FUNCTION__, ruby->av[i]));
	    rpmrubyRunThreadFile(ruby, ruby->av[i], NULL);
	    Trace((zlog, "-- %s: require '%s' end", __FUNCTION__, ruby->av[i]));
	}

	/* Terminate the ruby interpreter. */
	Trace((zlog, "-- %s: interpreter terminating", __FUNCTION__));
	ruby_finalize();
        ruby_cleanup(0);
	Trace((zlog, "-- %s: interpreter terminated", __FUNCTION__));
    }

    /* Report interpreter end to main. */
    ruby->more = 0;
    /* Permit main thread to run without blocking. */
    yarnRelease(ruby->main_coroutine_lock);

    Trace((zlog, "-- %s: ended", __FUNCTION__));
    return NULL;
}
Пример #2
0
static void ruby_coroutine_body(
#ifdef DEMONSTRATE_PTHREAD
    void* dummy_argument_that_is_not_used
#endif
)
{
#ifdef DEMONSTRATE_PTHREAD
    printf("Coroutine: waiting for initial asynchronous relay from main\n");
    relay_from_ruby_to_main(Qnil);
#endif

    printf("Coroutine: begin\n");

    int i;
    for (i = 0; i < 2; i++)
    {
        printf("Coroutine: relay %d\n", i);
        relay_from_ruby_to_main(Qnil);
    }

    printf("Coroutine: Ruby begin\n");

#ifdef HAVE_RUBY_SYSINIT
    int argc = 0;
    char** argv = {""};
    ruby_sysinit(&argc, &argv);
#endif
    {
#ifdef HAVE_RUBY_BIND_STACK
        ruby_bind_stack(
                /* lower memory address */
                (VALUE*)(ruby_coroutine_stack),

                /* upper memory address */
                (VALUE*)(ruby_coroutine_stack + ruby_coroutine_stack_size)
        );
#endif

        RUBY_INIT_STACK;
        ruby_init();
        ruby_init_loadpath();

        /* allow Ruby script to relay */
        rb_define_module_function(rb_mKernel, "relay_from_ruby_to_main",
                                  relay_from_ruby_to_main, 0);

        /* run the "hello world" Ruby script */
        printf("Ruby: require 'hello' begin\n");
        ruby_coroutine_body_require("./hello.rb");
        printf("Ruby: require 'hello' end\n");

        ruby_cleanup(0);
    }

    printf("Coroutine: Ruby end\n");

    printf("Coroutine: end\n");

    ruby_coroutine_finished = true;
    relay_from_ruby_to_main(Qnil);

#ifdef DEMONSTRATE_PTHREAD
    pthread_exit(NULL);
#endif
}
Пример #3
0
rpmruby rpmrubyNew(char ** av, uint32_t flags)
{
    static char * _av[] = { "rpmruby", NULL };
    rpmruby ruby = (flags & 0x80000000)
		? rpmrubyI() : rpmrubyGetPool(_rpmrubyPool);
int xx;

RUBYDBG((stderr, "--> %s(%p,0x%x) ruby %p\n", __FUNCTION__, av, flags, ruby));

    /* If failure, or retrieving already initialized _rpmrubyI, just exit. */
    if (ruby == NULL || ruby == _rpmrubyI)
	goto exit;

    if (av == NULL) av = _av;

    ruby->flags = flags;
    xx = argvAppend(&ruby->av, (ARGV_t)av);
    ruby->ac = argvCount(ruby->av);

    /* XXX FIXME: 0x40000000 => xruby.c wrapper without interpreter. */
    if (ruby->flags & 0x40000000) {
	static size_t _rpmrubyStackSize = 4 * 1024 * 1024;

	/* XXX save as global interpreter. */
	_rpmrubyI = ruby;

	ruby->nstack = _rpmrubyStackSize;
	ruby->stack = malloc(ruby->nstack);
assert(ruby->stack != NULL);

	gettimeofday(&ruby->start, NULL);  /* starting time for log entries */
	if (_rpmruby_debug)
	    ruby->zlog = rpmzLogNew(&ruby->start);  /* initialize logging */

	/* initialize the relay mechanism */
	ruby->ruby_coroutine_lock = yarnNewLock(0);
	ruby->main_coroutine_lock = yarnNewLock(0);

    } else {

#if defined(WITH_RUBYEMBED)
	VALUE variable_in_this_stack_frame;		/* RUBY_INIT_STSCK */

#if defined(HAVE_RUBY_DEFINES_H)	/* XXX ruby-1.9.2 */
	ruby_sysinit(&ruby->ac, (char ***) &ruby->av);
	/* XXX ruby-1.9.2p0 ruby_bind_stack() patch needed */
	{
	    uint8_t * b = ruby->stack;
	    uint8_t * e = b + ruby->nstack;
	    ruby_bind_stack((VALUE *)b, (VALUE *) e);
	}
#endif	/* NOTYET */

	ruby_init_stack(&variable_in_this_stack_frame);	/* RUBY_INIT_STACK */

	ruby_init();
	ruby_init_loadpath();

	ruby_script((char *)av[0]);
	if (av[1])
	    ruby_set_argv(argvCount((ARGV_t)av)-1, av+1);

	rb_gv_set("$result", rb_str_new2(""));
#if !defined(HAVE_RUBY_DEFINES_H)	/* XXX ruby-1.8.6 */
	(void) rpmrubyRun(ruby, rpmrubyInitStringIO, NULL);
#endif
#endif	/* WITH_RUBYEMBED */
    }

exit:
    return rpmrubyLink(ruby);
}