From: nobu.nokada@... Date: 2005-11-10T08:27:32+09:00 Subject: Re: make $VERBOSE thread-local? 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 $VERBOSE value for + * thr. The default is inherited from the thread created + * thr. See also Thread::verbose=. + */ + +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 $VERBOSE value for thr. + */ + +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); -- Nobu Nakada