make $VERBOSE thread-local?

  • Thread starter Clifford T. Matthews
  • Start date
C

Clifford T. Matthews

I'm new to ruby, so setting $VERBOSE to true is very useful for me.

However, there are some places where I use someone else's library and
I get warnings that I'm not interested in. I can easily wrap the code
with save_verbose = $VERBOSE; $VERBOSE = false; code; $VERBOSE =
save_verbose, but that turns off warnings in my other threads.

Looking at the source to ruby 1.8.3 it doesn't look like it would be
too hard for me to make it so that $VERBOSE would be thread-local. Of
course I've underestimated implementation times before :-(.

Looking up the value of a thread-local variable will take a lot more
time than simply RTEST'ing ruby_verbose, so the naive implementation
might slow things down enough to be a bad idea. On the other hand,
adding a second global variable that specifies whether or not to defer
to a thread-specific variable is conceptually ugly, but means that
people who don't want the slowdown of thread-specific lookup won't
take a hit.

What do others think? If there's a consensus that a particular change
would be worthwhile, I don't mind submitting an RCR and a sample
patch. OTOH, if nobody cares or if someone else wants to run with the
ball, I'm pretty darned lazy and won't mind doing nothing.
 
N

nobu.nokada

Hi,

At Fri, 4 Nov 2005 05:41:14 +0900,
Clifford T. Matthews wrote in [ruby-talk:163997]:
However, there are some places where I use someone else's library and
I get warnings that I'm not interested in. I can easily wrap the code
with save_verbose = $VERBOSE; $VERBOSE = false; code; $VERBOSE =
save_verbose, but that turns off warnings in my other threads.

It might be interesting.
Looking up the value of a thread-local variable will take a lot more
time than simply RTEST'ing ruby_verbose, so the naive implementation
might slow things down enough to be a bad idea. On the other hand,
adding a second global variable that specifies whether or not to defer
to a thread-specific variable is conceptually ugly, but means that
people who don't want the slowdown of thread-specific lookup won't
take a hit.

It would not be nice to change ruby_verbose itself.


Index: eval.c
===================================================================
RCS file: /cvs/ruby/src/ruby/eval.c,v
retrieving revision 1.844
diff -p -U 2 -r1.844 eval.c
--- eval.c 8 Nov 2005 08:49:45 -0000 1.844
+++ eval.c 9 Nov 2005 23:23:09 -0000
@@ -9699,4 +9699,5 @@ struct thread {
VALUE last_line;
VALUE last_match;
+ VALUE verbose;

int safe;
@@ -9739,4 +9740,5 @@ struct thread_status_t {
VALUE last_line;
VALUE last_match;
+ VALUE verbose;

int safe;
@@ -9761,4 +9763,5 @@ struct thread_status_t {
(dst)->last_line = (src)->last_line, \
(dst)->last_match = (src)->last_match, \
+ (dst)->verbose = (src)->verbose, \
\
(dst)->safe = (src)->safe, \
@@ -9914,4 +9917,5 @@ thread_mark(rb_thread_t th)
rb_gc_mark(th->last_line);
rb_gc_mark(th->last_match);
+ rb_gc_mark(th->verbose);
rb_mark_tbl(th->locals);
rb_gc_mark(th->thgroup);
@@ -10092,4 +10096,5 @@ rb_thread_save_context(rb_thread_t th)
rb_backref_set(th->last_match);
th->last_match = tval;
+ th->verbose = ruby_verbose;
th->safe = ruby_safe_level;

@@ -10192,4 +10197,5 @@ rb_thread_restore_context(rb_thread_t th
ruby_errinfo = th->errinfo;
rb_last_status = th->last_status;
+ ruby_verbose = th->verbose;
ruby_safe_level = th->safe;

@@ -11396,4 +11402,36 @@ rb_thread_abort_exc_set(VALUE thread, VA
/*
* call-seq:
+ * thr.verbose => true, false or nil
+ *
+ * Returns the thread-local <code>$VERBOSE</code> value for
+ * <i>thr</i>. The default is inherited from the thread created
+ * <i>thr</i>. See also <code>Thread::verbose=</code>.
+ */
+
+static VALUE
+rb_thread_verbose(VALUE thread)
+{
+ return rb_thread_check(thread)->verbose;
+}
+
+
+/*
+ * call-seq:
+ * thr.verbose= boolean => true, false or nil
+ *
+ * Sets the thread-local <code>$VERBOSE</code> value for <i>thr</i>.
+ */
+
+static VALUE
+rb_thread_verbose_set(VALUE thread, VALUE val)
+{
+ rb_secure(4);
+ rb_thread_check(thread)->verbose = RTEST(val) ? Qtrue : val;
+ return val;
+}
+
+
+/*
+ * call-seq:
* thr.group => thgrp or nil
*
@@ -11456,4 +11494,5 @@ rb_thread_group(VALUE thread)
th->last_line = 0;\
th->last_match = Qnil;\
+ th->verbose = ruby_verbose;\
th->abort = 0;\
th->priority = 0;\
@@ -12773,4 +12812,7 @@ Init_Thread(void)
rb_define_method(rb_cThread, "abort_on_exception=", rb_thread_abort_exc_set, 1);

+ rb_define_method(rb_cThread, "verbose", rb_thread_verbose, 0);
+ rb_define_method(rb_cThread, "verbose=", rb_thread_verbose_set, 1);
+
rb_define_method(rb_cThread, "priority", rb_thread_priority, 0);
rb_define_method(rb_cThread, "priority=", rb_thread_priority_set, 1);
 
C

Clifford T. Matthews

Thanks for the nice patch.

I knew Ruby had its own threads, but I didn't think of such a clean
solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top