[#41772] [Feature #3513] spawn ..., err: nil — Shyouhei Urabe <redmine@...>
Feature #3513: spawn ..., err: nil
10 messages
2010/07/01
[#41773] Re: [Feature #3513] spawn ..., err: nil
— Tanaka Akira <akr@...>
2010/07/01
2010年7月1日19:09 Shyouhei Urabe <redmine@ruby-lang.org>:
[#41780] Re: [Feature #3513] spawn ..., err: nil
— Urabe Shyouhei <shyouhei@...>
2010/07/02
卜部です。
[#41774] 動的ローディングの仕様について — Hidetoshi NAGAI <nagai@...>
永井@知能.九工大です.
7 messages
2010/07/01
[#41778] [Bug #3515] FreeBSD wrongly raises ECONNRESET on close(2) — Yui NARUSE <redmine@...>
Bug #3515: FreeBSD wrongly raises ECONNRESET on close(2)
12 messages
2010/07/02
[#41826] Re: [Bug #3515] FreeBSD wrongly raises ECONNRESET on close(2)
— Tanaka Akira <akr@...>
2010/07/06
2010年7月2日12:20 Yui NARUSE <redmine@ruby-lang.org>:
[#41828] Re: [Bug #3515] FreeBSD wrongly raises ECONNRESET on close(2)
— Takahiro Kambe <taca@...>
2010/07/06
In message <AANLkTimD2geIuuhr0GQZ4fprYTv3m4kuESajvsrxaItm@mail.gmail.com>
[#41782] [Bug #3522] String::size return invalid size on mswin64 — shintaro kuwamoto <redmine@...>
Bug #3522: String::size return invalid size on mswin64
5 messages
2010/07/02
[#41800] Tempfile#size returns 0 under windows — take_tk <ggb03124@...>
たけ(tk)です。
6 messages
2010/07/03
[#41833] [bug:trunk] GNU/Linux select hang on a socket which TCP state is CLOSED — Tanaka Akira <akr@...>
GNU/Linux で、以下のプログラムがハングします。
7 messages
2010/07/06
[#41834] Re: [bug:trunk] GNU/Linux select hang on a socket which TCP state is CLOSED
— Yukihiro Matsumoto <matz@...>
2010/07/06
まつもと ゆきひろです
[#41835] Re: [bug:trunk] GNU/Linux select hang on a socket which TCP state is CLOSED
— KOSAKI Motohiro <kosaki.motohiro@...>
2010/07/06
kosakiです
[#41856] [Bug #3579] RHEL5のautoconf-2.59だとruby-1.8.7-p299でautoconfが失敗する — Motohiro KOSAKI <redmine@...>
Bug #3579: RHEL5のautoconf-2.59だとruby-1.8.7-p299でautoconfが失敗する
5 messages
2010/07/16
[#41858] Re: [Bug #3579] RHEL5のautoconf-2.59だとruby-1.8.7-p299でautoconfが失敗する
— Nobuyoshi Nakada <nobu@...>
2010/07/17
なかだです。
[#41862] [bug:trunk] rb_data_type_t should be extensible — Nobuyoshi Nakada <nobu@...>
なかだです。
5 messages
2010/07/17
[#41876] redmine.ruby-lang.orgが落ちてる? — kimura wataru <kimuraw@...>
木村(わ)といいます。
4 messages
2010/07/25
[#41883] failed to build ext/tk of ruby-1.9.2-rc2 on Mac OS X — Yutaka Hara <yutaka.hara@...>
yharaです。
6 messages
2010/07/28
[#41884] Re: failed to build ext/tk of ruby-1.9.2-rc2 on Mac OS X
— Hidetoshi NAGAI <nagai@...>
2010/07/28
永井@知能.九工大です.
[#41892] [Feature #3627] catchのブロックを再実行するメソッド — Makoto Kishimoto <redmine@...>
Feature #3627: catchのブロックを再実行するメソッド
6 messages
2010/07/29
[#41893] thread.bind(sym, val) { ... } — Tanaka Akira <akr@...>
スレッド変数を一時的に設定するメソッドを加えるのはどうでしょうか。
7 messages
2010/07/29
[ruby-dev:41893] thread.bind(sym, val) { ... }
From:
Tanaka Akira <akr@...>
Date:
2010-07-29 09:40:43 UTC
List:
ruby-dev #41893
スレッド変数を一時的に設定するメソッドを加えるのはどうでしょうか。
たとえば Thread#bind(sym, val) { ... } として、
ブロックを呼び出しているあいだ、スレッド変数 sym を
val に設定する、というようなものです。
p Thread.current[:a] # nil
Thread.current.bind(:a, 100) {
p Thread.current[:a] # 100
}
p Thread.current[:a] # nil に戻る
スレッド変数はこのような使い方が多いので、メソッドがあってもいい
ように思います。
なお、bind 以外の名前としては let が思い浮かびます。
また、Thread じゃなくて Fiber じゃないか、という話はあるかもしれません。
% svn diff --diff-cmd diff -x '-u -p'
Index: thread.c
===================================================================
--- thread.c (revision 28780)
+++ thread.c (working copy)
@@ -2071,6 +2071,59 @@ rb_thread_aset(VALUE self, VALUE id, VAL
return rb_thread_local_aset(self, rb_to_id(id), val);
}
+struct rb_thread_bind_arg {
+ VALUE thread;
+ ID id;
+ VALUE old;
+};
+
+static VALUE
+rb_thread_bind_ensure(VALUE _arg)
+{
+ struct rb_thread_bind_arg *arg = (struct rb_thread_bind_arg *)_arg;
+ rb_thread_local_aset(arg->thread, arg->id, arg->old);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * thr.bind(sym, obj) { ... }
+ *
+ * Set the attribute temporally.
+ *
+ * This method sets the attribute _sym_ of the thread _thr_ as _obj_
+ * between the block is called.
+ * It restore the attribute after the block exits
+ * even if an exception is raised in the block.
+ *
+ * This method returns the value of the block.
+ *
+ * Thread.current.bind(:a, 1) {
+ * # Thread.current[:a] is 1.
+ * Thread.current.bind(:a, 2) {
+ * # Thread.current[:a] is 2.
+ * Thread.current.bind(:a, 3) {
+ * # Thread.current[:a] is 3.
+ * }
+ * # Thread.current[:a] is 2.
+ * }
+ * # Thread.current[:a] is 1.
+ * }
+ *
+ */
+
+static VALUE
+rb_thread_bind(VALUE thread, VALUE sym, VALUE val)
+{
+ ID id = rb_to_id(sym);
+ struct rb_thread_bind_arg arg;
+ arg.thread = thread;
+ arg.id = id;
+ arg.old = rb_thread_local_aref(thread, id);
+ rb_thread_local_aset(thread, id, val);
+ return rb_ensure(rb_yield, Qnil, rb_thread_bind_ensure, (VALUE)&arg);
+}
+
/*
* call-seq:
* thr.key?(sym) -> true or false
@@ -4202,6 +4255,7 @@ Init_Thread(void)
rb_define_method(rb_cThread, "wakeup", rb_thread_wakeup, 0);
rb_define_method(rb_cThread, "[]", rb_thread_aref, 1);
rb_define_method(rb_cThread, "[]=", rb_thread_aset, 2);
+ rb_define_method(rb_cThread, "bind", rb_thread_bind, 2);
rb_define_method(rb_cThread, "key?", rb_thread_key_p, 1);
rb_define_method(rb_cThread, "keys", rb_thread_keys, 0);
rb_define_method(rb_cThread, "priority", rb_thread_priority, 0);
--
[田中 哲][たなか あきら][Tanaka Akira]