[Groonga-commit] ranguba/rroonga at ac88c50 [master] Bind Groonga::Thread.limit_getter= and limit_setter=

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Sep 8 17:31:39 JST 2015


Kouhei Sutou	2015-09-08 17:31:39 +0900 (Tue, 08 Sep 2015)

  New Revision: ac88c5045985fd3bc42d1cb2fad026a6d5aeec5d
  https://github.com/ranguba/rroonga/commit/ac88c5045985fd3bc42d1cb2fad026a6d5aeec5d

  Message:
    Bind Groonga::Thread.limit_getter= and limit_setter=

  Modified files:
    ext/groonga/rb-grn-thread.c
    test/test-thread.rb

  Modified: ext/groonga/rb-grn-thread.c (+81 -1)
===================================================================
--- ext/groonga/rb-grn-thread.c    2015-09-08 17:14:13 +0900 (d6e581d)
+++ ext/groonga/rb-grn-thread.c    2015-09-08 17:31:39 +0900 (d109b63)
@@ -18,7 +18,7 @@
 
 #include "rb-grn.h"
 
-VALUE rb_cGrnDatabase;
+static ID id_call;
 
 /*
  * Document-class: Groonga::Thread
@@ -66,15 +66,95 @@ rb_grn_thread_s_set_limit (VALUE self, VALUE rb_new_limit)
     return Qnil;
 }
 
+static uint32_t
+rb_grn_thread_get_limit_func (void *data)
+{
+    VALUE callable = (VALUE)data;
+    VALUE rb_limit;
+
+    rb_limit = rb_funcall(callable, id_call, 0);
+
+    return NUM2UINT(rb_limit);
+}
+
+/*
+ * @overload limit_getter=(getter)
+ *
+ *   Sets a callable object that returns the max number of threads.
+ *
+ *   @param getter [#call] Callable object that returns the max number
+ *     of threads.
+ *
+ *   @return [void]
+ *
+ * @since 5.0.5
+ */
+static VALUE
+rb_grn_thread_s_set_limit_getter (VALUE self, VALUE rb_getter)
+{
+    rb_iv_set(self, "@limit_getter", rb_getter);
+
+    if (NIL_P(rb_getter)) {
+        grn_thread_set_get_limit_func(NULL, NULL);
+    } else {
+        grn_thread_set_get_limit_func(rb_grn_thread_get_limit_func,
+                                      (void *)rb_getter);
+    }
+
+    return Qnil;
+}
+
+static void
+rb_grn_thread_set_limit_func (uint32_t new_limit, void *data)
+{
+    VALUE callable = (VALUE)data;
+
+    rb_funcall(callable, id_call, 1, UINT2NUM(new_limit));
+}
+
+/*
+ * @overload limit_setter=(setter)
+ *
+ *   Sets a callable object that sets the max number of threads.
+ *
+ *   @param setter [#call] Callable object that sets the max number
+ *     of threads.
+ *
+ *   @return [void]
+ *
+ * @since 5.0.5
+ */
+static VALUE
+rb_grn_thread_s_set_limit_setter (VALUE self, VALUE rb_setter)
+{
+    rb_iv_set(self, "@limit_setter", rb_setter);
+
+    if (NIL_P(rb_setter)) {
+        grn_thread_set_set_limit_func(NULL, NULL);
+    } else {
+        grn_thread_set_set_limit_func(rb_grn_thread_set_limit_func,
+                                      (void *)rb_setter);
+    }
+
+    return Qnil;
+}
+
 void
 rb_grn_init_thread (VALUE mGrn)
 {
     VALUE rb_mGrnThread;
 
+    CONST_ID(id_call, "call");
+
     rb_mGrnThread = rb_define_module_under(mGrn, "Thread");
 
     rb_define_singleton_method(rb_mGrnThread, "limit",
                                rb_grn_thread_s_get_limit, 0);
     rb_define_singleton_method(rb_mGrnThread, "limit=",
                                rb_grn_thread_s_set_limit, 1);
+
+    rb_define_singleton_method(rb_mGrnThread, "limit_getter=",
+                               rb_grn_thread_s_set_limit_getter, 1);
+    rb_define_singleton_method(rb_mGrnThread, "limit_setter=",
+                               rb_grn_thread_s_set_limit_setter, 1);
 }

  Modified: test/test-thread.rb (+22 -3)
===================================================================
--- test/test-thread.rb    2015-09-08 17:14:13 +0900 (ccb85ad)
+++ test/test-thread.rb    2015-09-08 17:31:39 +0900 (27114c2)
@@ -16,8 +16,27 @@
 class ThreadTest < Test::Unit::TestCase
   include GroongaTestUtils
 
-  def test_limit
-    Groonga::Thread.limit = 10
-    assert_equal(0, Groonga::Thread.limit)
+  sub_test_case "limit" do
+    teardown do
+      Groonga::Thread.limit_getter = nil
+      Groonga::Thread.limit_setter = nil
+    end
+
+    test "default" do
+      assert_equal(0, Groonga::Thread.limit)
+    end
+
+    test "custom" do
+      limit = 0
+      Groonga::Thread.limit_getter = lambda do
+        limit
+      end
+      Groonga::Thread.limit_setter = lambda do |new_limit|
+        limit = new_limit
+      end
+
+      Groonga::Thread.limit = 10
+      assert_equal(10, Groonga::Thread.limit)
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index