thread_locals.patch

Aaron Patterson, 10/03/2012 03:47 AM

Download (2.68 KB)

View differences:

test/ruby/test_thread.rb
27 27
    end
28 28
  end
29 29

  
30
  def test_main_thread_local_in_enumerator
31
    assert_equal Thread.main, Thread.current
32

  
33
    Thread.current.set_local :foo, "bar"
34

  
35
    thread, value = Fiber.new {
36
      Fiber.yield [Thread.current, Thread.current.get_local(:foo)]
37
    }.resume
38

  
39
    assert_equal Thread.current, thread
40
    assert_equal Thread.current.get_local(:foo), value
41
  end
42

  
43
  def test_thread_local_in_enumerator
44
    Thread.new {
45
      Thread.current.set_local :foo, "bar"
46

  
47
      thread, value = Fiber.new {
48
        Fiber.yield [Thread.current, Thread.current.get_local(:foo)]
49
      }.resume
50

  
51
      assert_equal Thread.current, thread
52
      assert_equal Thread.current.get_local(:foo), value
53
    }.join
54
  end
55

  
30 56
  def test_mutex_synchronize
31 57
    m = Mutex.new
32 58
    r = 0
thread.c
2562 2562
    return rb_thread_local_aset(self, rb_to_id(id), val);
2563 2563
}
2564 2564

  
2565
static VALUE
2566
rb_thread_get_local(VALUE thread, VALUE id)
2567
{
2568
    VALUE locals;
2569

  
2570
    locals = rb_iv_get(thread, "locals");
2571
    return rb_hash_aref(locals, id);
2572
}
2573

  
2574
static VALUE
2575
rb_thread_set_local(VALUE thread, VALUE id, VALUE val)
2576
{
2577
    VALUE locals;
2578

  
2579
    locals = rb_iv_get(thread, "locals");
2580
    return rb_hash_aset(locals, id, val);
2581
}
2582

  
2565 2583
/*
2566 2584
 *  call-seq:
2567 2585
 *     thr.key?(sym)   -> true or false
......
4528 4546
    rb_define_method(rb_cThread, "wakeup", rb_thread_wakeup, 0);
4529 4547
    rb_define_method(rb_cThread, "[]", rb_thread_aref, 1);
4530 4548
    rb_define_method(rb_cThread, "[]=", rb_thread_aset, 2);
4549
    rb_define_method(rb_cThread, "get_local", rb_thread_get_local, 1);
4550
    rb_define_method(rb_cThread, "set_local", rb_thread_set_local, 2);
4531 4551
    rb_define_method(rb_cThread, "key?", rb_thread_key_p, 1);
4532 4552
    rb_define_method(rb_cThread, "keys", rb_thread_keys, 0);
4533 4553
    rb_define_method(rb_cThread, "priority", rb_thread_priority, 0);
vm.c
1832 1832
    GetThreadPtr(self, th);
1833 1833

  
1834 1834
    th_init(th, self);
1835
    rb_iv_set(self, "locals", rb_hash_new());
1835 1836
    th->vm = vm;
1836 1837

  
1837 1838
    th->top_wrapper = 0;
......
2165 2166

  
2166 2167
	/* create main thread */
2167 2168
	th_self = th->self = TypedData_Wrap_Struct(rb_cThread, &thread_data_type, th);
2169
	rb_iv_set(th_self, "locals", rb_hash_new());
2168 2170
	vm->main_thread = th;
2169 2171
	vm->running_thread = th;
2170 2172
	th->vm = vm;