From da34bcf6b6f0a8b731308fc17edf6dc6fdabb171 Mon Sep 17 00:00:00 2001
From: Per Hedbor <ph@opera.com>
Date: Mon, 6 Nov 2000 21:41:49 +0100
Subject: [PATCH] o Added `() method in Thread, and inherit thread_id, it's
 thus possible to write a program like this: void foo() {  
 werror("thread\n");   sleep(2); }

void main(int argc, array argv)
{
  Thread t = Thread( foo );
  t->wait();
}


Also, added _sprintf() methods to most of the classes (Queue et.al.)

Rev: lib/modules/Thread.pmod:1.25
---
 lib/modules/Thread.pmod | 115 +++++++++++++++++++++++++++++++---------
 1 file changed, 90 insertions(+), 25 deletions(-)

diff --git a/lib/modules/Thread.pmod b/lib/modules/Thread.pmod
index 78f3674874..2c8e4616bf 100644
--- a/lib/modules/Thread.pmod
+++ b/lib/modules/Thread.pmod
@@ -1,19 +1,33 @@
 #pike __REAL_VERSION__
 
 #if constant(thread_create)
-
 constant Thread=__builtin.thread_id;
-constant MutexKey=__builtin.mutex_key;
-constant Mutex=__builtin.mutex;
-constant Condition=__builtin.condition;
-constant _Disabled=__builtin.threads_disabled;
-constant Local=__builtin.thread_local;
 
-constant thread_create = predef::thread_create;
-constant this_thread = predef::this_thread;
-constant all_threads = predef::all_threads;
+// The reason for this inherit is rather simple.
+// It's now possible to write Thread Thread( ... );
+//
+// This makes the interface look somewhat more thought-through.
+//
+inherit Thread;
+
+optional Thread `()( mixed f, mixed ... args )
+{
+  return thread_create( f, @args );
+}
+
+optional constant MutexKey=__builtin.mutex_key;
+optional constant Mutex=__builtin.mutex;
+optional constant Condition=__builtin.condition;
+optional constant _Disabled=__builtin.threads_disabled;
+optional constant Local=__builtin.thread_local;
+
+optional constant thread_create = predef::thread_create;
+optional constant this_thread = predef::this_thread;
+optional constant all_threads = predef::all_threads;
 
-class Fifo {
+
+
+optional class Fifo {
   inherit Condition : r_cond;
   inherit Condition : w_cond;
   inherit Mutex : lock;
@@ -81,14 +95,25 @@ class Fifo {
     key = 0;
   }
 
-  void create(int|void size)
+  static void create(int|void size)
   {
     write_tres=0;
     buffer=allocate(read_tres=size || 128);
   }
+
+  static string _sprintf( int f )
+  {
+    switch( f )
+    {
+      case 't':
+	return "Thread.Fifo";
+      case 'O':
+	return sprintf( "%t(%d / %d)", this_object(), size(), read_tres );
+    }
+  }
 };
 
-class Queue {
+optional class Queue {
   inherit Condition : r_cond;
   inherit Mutex : lock;
   
@@ -123,11 +148,22 @@ class Queue {
     key=0; // Must free this one _before_ the signal...
     r_cond::signal();
   }
+
+  static string _sprintf( int f )
+  {
+    switch( f )
+    {
+      case 't':
+	return "Thread.Queue";
+      case 'O':
+	return sprintf( "%t(%d)", this_object(), size() );
+    }
+  }
 }
 
 
 
-class Farm
+optional class Farm
 {
   class Result
   {
@@ -175,6 +211,18 @@ class Farm
       if( done_cb )
         done_cb( what, 0 );
     }
+
+
+    static string _sprintf( int f )
+    {
+      switch( f )
+      {
+	case 't':
+	  return "Thread.Farm().Result";
+	case 'O':
+	  return sprintf( "%t(%d %O)", this_object(), ready, value );
+      }
+    }
   }
 
   static class Handler
@@ -247,10 +295,22 @@ class Farm
               +"\n\n");
     }
 
-    void create()
+    static void create()
     {
       thread = thread_create( handler );
     }
+
+
+    static string _sprintf( int f )
+    {
+      switch( f )
+      {
+	case 't':
+	  return "Thread.Farm().Handler";
+	case 'O':
+	  return sprintf( "%t(%f / %d,  %d)", total_time, max_time,handled );
+      }
+    }
   }
 
   static Mutex mutex = Mutex();
@@ -288,12 +348,8 @@ class Farm
       aquire_thread()->run( q[1], q[0] );
   }
 
-  static class ValueAdjuster
+  static class ValueAdjuster( object r, object r2, int i, mapping v )
   {
-    object r, r2;
-    mapping v;
-    int i;
-
     void go(mixed vn, int err)
     {
       ([array]r->value)[ i ] = vn;
@@ -303,10 +359,6 @@ class Farm
         r->provide( r->value );
       destruct();
     }
-    void create( object _1,object _2,int _3,mapping _4 )
-    {
-      r = _1; r2 = _2; i=_3; v=_4;
-    }
   }
 
   object run_multiple( array fun_args )
@@ -380,10 +432,23 @@ class Farm
     return res;
   }
 
-  void create()
+
+  static string _sprintf( int f )
+  {
+    switch( f )
+    {
+      case 't':
+	return "Thread.Farm";
+      case 'O':
+	return sprintf( "%t(/* %s */)", this_object, debug_status() );
+    }
+  }
+
+
+
+  static void create()
   {
     thread_create( dispatcher );
   }
 }
-
 #endif /* constant(thread_create) */
-- 
GitLab