[前][次][番号順一覧][スレッド一覧]

ruby-changes:13061

From: nobu <ko1@a...>
Date: Wed, 9 Sep 2009 12:24:54 +0900 (JST)
Subject: [ruby-changes:13061] Ruby:r24808 (trunk): * thread.c (thgroup_data_type, mutex_data_type, barrier_data_type):

nobu	2009-09-09 12:21:14 +0900 (Wed, 09 Sep 2009)

  New Revision: 24808

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24808

  Log:
    * thread.c (thgroup_data_type, mutex_data_type, barrier_data_type):
      typed.

  Modified files:
    trunk/ChangeLog
    trunk/thread.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 24807)
+++ ChangeLog	(revision 24808)
@@ -1,4 +1,4 @@
-Wed Sep  9 12:01:36 2009  Nobuyoshi Nakada  <nobu@r...>
+Wed Sep  9 12:21:12 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* dir.c (dir_data_type): typed.
 
@@ -9,6 +9,9 @@
 
 	* file.c (stat_data_type): typed.
 
+	* thread.c (thgroup_data_type, mutex_data_type, barrier_data_type):
+	  typed.
+
 Wed Sep  9 11:11:33 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* include/ruby/ruby.h (rb_data_type_struct): constified dsize.
Index: thread.c
===================================================================
--- thread.c	(revision 24807)
+++ thread.c	(revision 24808)
@@ -2724,6 +2724,17 @@
     VALUE group;
 };
 
+static size_t
+thgroup_memsize(const void *ptr)
+{
+    return ptr ? sizeof(struct thgroup) : 0;
+}
+
+static const rb_data_type_t thgroup_data_type = {
+    "thgroup",
+    NULL, RUBY_TYPED_DEFAULT_FREE, thgroup_memsize,
+};
+
 /*
  * Document-class: ThreadGroup
  *
@@ -2736,14 +2747,13 @@
  *  were created.
  */
 
-static VALUE thgroup_s_alloc(VALUE);
 static VALUE
 thgroup_s_alloc(VALUE klass)
 {
     VALUE group;
     struct thgroup *data;
 
-    group = Data_Make_Struct(klass, struct thgroup, 0, -1, data);
+    group = TypedData_Make_Struct(klass, struct thgroup, &thgroup_data_type, data);
     data->enclosed = 0;
     data->group = group;
 
@@ -2816,7 +2826,7 @@
 {
     struct thgroup *data;
 
-    Data_Get_Struct(group, struct thgroup, data);
+    TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
     data->enclosed = 1;
 
     return group;
@@ -2836,7 +2846,7 @@
 {
     struct thgroup *data;
 
-    Data_Get_Struct(group, struct thgroup, data);
+    TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
     if (data->enclosed)
 	return Qtrue;
     return Qfalse;
@@ -2881,7 +2891,7 @@
     if (OBJ_FROZEN(group)) {
 	rb_raise(rb_eThreadError, "can't move to the frozen thread group");
     }
-    Data_Get_Struct(group, struct thgroup, data);
+    TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
     if (data->enclosed) {
 	rb_raise(rb_eThreadError, "can't move to the enclosed thread group");
     }
@@ -2893,7 +2903,7 @@
     if (OBJ_FROZEN(th->thgroup)) {
 	rb_raise(rb_eThreadError, "can't move from the frozen thread group");
     }
-    Data_Get_Struct(th->thgroup, struct thgroup, data);
+    TypedData_Get_Struct(th->thgroup, struct thgroup, &thgroup_data_type, data);
     if (data->enclosed) {
 	rb_raise(rb_eThreadError,
 		 "can't move from the enclosed thread group");
@@ -2930,10 +2940,12 @@
  */
 
 #define GetMutexPtr(obj, tobj) \
-  Data_Get_Struct(obj, mutex_t, tobj)
+    TypedData_Get_Struct(obj, mutex_t, &mutex_data_type, tobj)
 
 static const char *mutex_unlock(mutex_t *mutex, rb_thread_t volatile *th);
 
+#define mutex_mark NULL
+
 static void
 mutex_free(void *ptr)
 {
@@ -2950,13 +2962,24 @@
     ruby_xfree(ptr);
 }
 
+static size_t
+mutex_memsize(const void *ptr)
+{
+    return ptr ? sizeof(mutex_t) : 0;
+}
+
+static const rb_data_type_t mutex_data_type = {
+    "mutex",
+    mutex_mark, mutex_free, mutex_memsize,
+};
+
 static VALUE
 mutex_alloc(VALUE klass)
 {
     VALUE volatile obj;
     mutex_t *mutex;
 
-    obj = Data_Make_Struct(klass, mutex_t, NULL, mutex_free, mutex);
+    obj = TypedData_Make_Struct(klass, mutex_t, &mutex_data_type, mutex);
     native_mutex_initialize(&mutex->lock);
     native_cond_initialize(&mutex->cond);
     return obj;
@@ -3297,12 +3320,25 @@
 /*
  * Document-class: Barrier
  */
+static void
+barrier_mark(void *ptr)
+{
+    rb_gc_mark((VALUE)ptr);
+}
+
+static const rb_data_type_t barrier_data_type = {
+    "barrier",
+    barrier_mark, 0, 0,
+};
+
 static VALUE
 barrier_alloc(VALUE klass)
 {
-    return Data_Wrap_Struct(klass, rb_gc_mark, 0, (void *)mutex_alloc(0));
+    return TypedData_Wrap_Struct(klass, &barrier_data_type, (void *)mutex_alloc(0));
 }
 
+#define GetBarrierPtr(obj) (VALUE)rb_check_typeddata(obj, &barrier_data_type)
+
 VALUE
 rb_barrier_new(void)
 {
@@ -3314,7 +3350,7 @@
 VALUE
 rb_barrier_wait(VALUE self)
 {
-    VALUE mutex = (VALUE)DATA_PTR(self);
+    VALUE mutex = GetBarrierPtr(self);
     mutex_t *m;
 
     if (!mutex) return Qfalse;
@@ -3329,13 +3365,13 @@
 VALUE
 rb_barrier_release(VALUE self)
 {
-    return rb_mutex_unlock((VALUE)DATA_PTR(self));
+    return rb_mutex_unlock(GetBarrierPtr(self));
 }
 
 VALUE
 rb_barrier_destroy(VALUE self)
 {
-    VALUE mutex = (VALUE)DATA_PTR(self);
+    VALUE mutex = GetBarrierPtr(self);
     DATA_PTR(self) = 0;
     return rb_mutex_unlock(mutex);
 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]