RPM Community Forums

Mailing List Message of <rpm-cvs>

[CVS] RPM: rpm-5_3: rpm/rpmio/ rpmruby.c rpmruby.h xruby.c

From: Jeff Johnson <jbj@rpm5.org>
Date: Mon 20 Sep 2010 - 21:27:06 CEST
Message-Id: <20100920192706.278D5D35D3@rpm5.org>
  RPM Package Manager, CVS Repository
  /cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  jbj@rpm5.org
  Module: rpm                              Date:   20-Sep-2010 21:27:06
  Branch: rpm-5_3                          Handle: 2010092019270500

  Modified files:           (Branch: rpm-5_3)
    rpm/rpmio               rpmruby.c rpmruby.h xruby.c

  Log:
    - ruby: add rpmrubyRunThread(), bury xruby.c guts under 0x40000000.

  Summary:
    Revision    Changes     Path
    2.12.2.9    +116 -4     rpm/rpmio/rpmruby.c
    2.7.2.5     +1  -3      rpm/rpmio/rpmruby.h
    1.1.2.8     +5  -135    rpm/rpmio/xruby.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: rpm/rpmio/rpmruby.c
  ============================================================================
  $ cvs diff -u -r2.12.2.8 -r2.12.2.9 rpmruby.c
  --- rpm/rpmio/rpmruby.c	20 Sep 2010 19:06:35 -0000	2.12.2.8
  +++ rpm/rpmio/rpmruby.c	20 Sep 2010 19:27:05 -0000	2.12.2.9
  @@ -40,7 +40,7 @@
   
   /*==============================================================*/
   /* puts the Ruby coroutine in control */
  -void _rpmruby_main_to_ruby(rpmruby ruby)
  +static void _rpmruby_main_to_ruby(rpmruby ruby)
   {
       rpmzLog zlog = ruby->zlog;
   
  @@ -50,7 +50,7 @@
   }
   
   /* puts the main C program in control */
  -unsigned long _rpmruby_ruby_to_main(rpmruby ruby, unsigned long self)
  +static unsigned long _rpmruby_ruby_to_main(rpmruby ruby, unsigned long self)
   {
       rpmzLog zlog = ruby->zlog;
   
  @@ -60,14 +60,126 @@
       return Qnil;
   }
   
  -#ifdef	NOTYET
   /* Using _rpmrubyI, puts the main C program in control */
   static VALUE relay_from_ruby_to_main(VALUE self)
   {
       /* XXX FIXME: _rpmrubyI is global */
       return _rpmruby_ruby_to_main(_rpmrubyI, self);
   }
  -#endif
  +
  +static rpmRC rpmrubyRunThreadFile(rpmruby ruby, const char * fn,
  +		const char **resultp)
  +{
  +    int error;
  +    VALUE result;
  +    rpmRC rc = RPMRC_FAIL;	/* assume failure */
  +
  +    result = rb_protect((VALUE (*)(VALUE))rb_require, (VALUE)fn, &error);
  +    if (error) {
  +        fprintf(stderr, "rb_require('%s') failed with status=%d\n",
  +               fn, error);
  +
  +        VALUE exception = rb_gv_get("$!");
  +        if (RTEST(exception)) {
  +            fprintf(stderr, "... because an exception was raised:\n");
  +
  +            VALUE inspect = rb_inspect(exception);
  +            rb_io_puts(1, &inspect, rb_stderr);
  +
  +            VALUE backtrace = rb_funcall(
  +                exception, rb_intern("backtrace"), 0);
  +            rb_io_puts(1, &backtrace, rb_stderr);
  +        }
  +    } else {
  +	/* XXX FIXME: check result */
  +	rc = RPMRC_OK;
  +    }
  +
  +    return rc;
  +}
  +
  +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;
  +}
  +
  +int rpmrubyRunThread(rpmruby ruby)
  +{
  +    int ec = 0;
  +
  +    yarnPossess(ruby->ruby_coroutine_lock);
  +    yarnPossess(ruby->main_coroutine_lock);
  +
  +    /* create a thread to house Ruby */
  +    ruby->thread = yarnLaunchStack((void (*)(void *))rpmrubyThread, ruby,
  +				ruby->stack, ruby->nstack);
  +assert(ruby->thread != NULL);
  +
  +    /* Relay control to ruby until nothing more to do. */
  +    ruby->more = (ruby->ac > 1);
  +    while (ruby->more)
  +        _rpmruby_main_to_ruby(ruby);
  +
  +    /* Permit ruby thread to run without blocking. */
  +    yarnRelease(ruby->ruby_coroutine_lock);
  +    /* Reap the ruby thread. */
  +    ruby->thread = yarnJoin(ruby->thread);
  +
  +    return ec;
  +}
   
   /*==============================================================*/
   
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmio/rpmruby.h
  ============================================================================
  $ cvs diff -u -r2.7.2.4 -r2.7.2.5 rpmruby.h
  --- rpm/rpmio/rpmruby.h	20 Sep 2010 19:02:19 -0000	2.7.2.4
  +++ rpm/rpmio/rpmruby.h	20 Sep 2010 19:27:05 -0000	2.7.2.5
  @@ -122,9 +122,7 @@
   	/*@globals fileSystem, internalState @*/
   	/*@modifies ruby, *resultp, fileSystem, internalState @*/;
   
  -void _rpmruby_main_to_ruby(rpmruby ruby)
  -	/*@*/;
  -unsigned long _rpmruby_ruby_to_main(rpmruby ruby, unsigned long _self)
  +int rpmrubyRunThread(rpmruby ruby)
   	/*@*/;
   
   #ifdef __cplusplus
  @@ .
  patch -p0 <<'@@ .'
  Index: rpm/rpmio/xruby.c
  ============================================================================
  $ cvs diff -u -r1.1.2.7 -r1.1.2.8 xruby.c
  --- rpm/rpmio/xruby.c	20 Sep 2010 19:02:19 -0000	1.1.2.7
  +++ rpm/rpmio/xruby.c	20 Sep 2010 19:27:05 -0000	1.1.2.8
  @@ -1,164 +1,34 @@
  -#define	DEBUG
  -
   #include "system.h"
   
  -#define	_RPMRUBY_INTERNAL
   #include <rpmruby.h>
   
  -#undef	xmalloc
  -#undef	xcalloc
  -#undef	xrealloc
  -#pragma GCC diagnostic ignored "-Wstrict-prototypes"
  -#include <ruby.h>
  -#pragma GCC diagnostic warning "-Wstrict-prototypes"
  -
   #include "debug.h"
   
  -static VALUE relay_from_ruby_to_main(VALUE self)
  -{
  -    return _rpmruby_ruby_to_main(_rpmrubyI, self);
  -}
  -
  -rpmRC rpmrubyRunFile(rpmruby ruby, const char * fn, const char **resultp)
  -{
  -    int error;
  -    VALUE result;
  -    rpmRC rc = RPMRC_FAIL;	/* assume failure */
  -
  -    result = rb_protect((VALUE (*)(VALUE))rb_require, (VALUE)fn, &error);
  -    if (error) {
  -        fprintf(stderr, "rb_require('%s') failed with status=%d\n",
  -               fn, error);
  -
  -        VALUE exception = rb_gv_get("$!");
  -        if (RTEST(exception)) {
  -            fprintf(stderr, "... because an exception was raised:\n");
  -
  -            VALUE inspect = rb_inspect(exception);
  -            rb_io_puts(1, &inspect, rb_stderr);
  -
  -            VALUE backtrace = rb_funcall(
  -                exception, rb_intern("backtrace"), 0);
  -            rb_io_puts(1, &backtrace, rb_stderr);
  -        }
  -    } else {
  -	/* XXX FIXME: check result */
  -	rc = RPMRC_OK;
  -    }
  -
  -    return rc;
  -}
  -
  -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]));
  -	    rpmrubyRunFile(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;
  -}
  -
  -static int runOnce(rpmruby ruby)
  -{
  -    int ec = 0;
  -    yarnPossess(ruby->ruby_coroutine_lock);
  -    yarnPossess(ruby->main_coroutine_lock);
  -
  -    /* create a thread to house Ruby */
  -    ruby->thread = yarnLaunchStack((void (*)(void *))rpmrubyThread, ruby,
  -				ruby->stack, ruby->nstack);
  -assert(ruby->thread != NULL);
  -
  -    /* Relay control to ruby until nothing more to do. */
  -    ruby->more = (ruby->ac > 1);
  -    while (ruby->more)
  -        _rpmruby_main_to_ruby(ruby);
  -
  -    /* Permit ruby thread to run without blocking. */
  -    yarnRelease(ruby->ruby_coroutine_lock);
  -    /* Reap the ruby thread. */
  -    ruby->thread = yarnJoin(ruby->thread);
  -
  -    return ec;
  -}
  -
   #ifdef RUBY_GLOBAL_SETUP
   RUBY_GLOBAL_SETUP
   #endif
   
   int main(int argc, char *argv[])
   {
  -    rpmruby ruby;
       static char * _av[] = { "rpmruby", "../ruby/hello.rb", NULL };
  -    ARGV_t av = (ARGV_t)_av;
  -    int ec = 1;
  +    ARGV_t av = (ARGV_t)(argc > 1 ? argv : _av);
  +    rpmruby ruby;
  +    int ec;
   
   _rpmruby_debug = 1;
   
       /* XXX FIXME: 0x40000000 => xruby.c wrapper without interpreter. */
       ruby = rpmrubyNew((char **)av, 0x40000000);
   
  -    ec = runOnce(ruby);
  +    ec = rpmrubyRunThread(ruby);
       if (ec) goto exit;
   
   #ifdef	NOWORKIE	/* XXX can't restart a ruby interpreter. */
  -    ec = runOnce(ruby);
  +    ec = rpmrubyRunThread(ruby);
       if (ec) goto exit;
   #endif	/* NOWORKIE */
   
   exit:
  -    fprintf(stderr, "Main: Goodbye!\n");
  -
       ruby = rpmrubyFree(ruby);
  -
       return ec;
   }
  @@ .
Received on Mon Sep 20 21:27:06 2010
Driven by Jeff Johnson and the RPM project team.
Hosted by OpenPKG and Ralf S. Engelschall.
Powered by FreeBSD and OpenPKG.