Skip to content
Snippets Groups Projects
Commit da34bcf6 authored by Per Hedbor's avatar Per Hedbor
Browse files

o Added `() method in Thread, and inherit thread_id, it's thus possible to...

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
parent dfd826ab
No related branches found
No related tags found
No related merge requests found
#pike __REAL_VERSION__ #pike __REAL_VERSION__
#if constant(thread_create) #if constant(thread_create)
constant Thread=__builtin.thread_id; 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; // The reason for this inherit is rather simple.
constant this_thread = predef::this_thread; // It's now possible to write Thread Thread( ... );
constant all_threads = predef::all_threads; //
// 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 : r_cond;
inherit Condition : w_cond; inherit Condition : w_cond;
inherit Mutex : lock; inherit Mutex : lock;
...@@ -81,14 +95,25 @@ class Fifo { ...@@ -81,14 +95,25 @@ class Fifo {
key = 0; key = 0;
} }
void create(int|void size) static void create(int|void size)
{ {
write_tres=0; write_tres=0;
buffer=allocate(read_tres=size || 128); 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 Condition : r_cond;
inherit Mutex : lock; inherit Mutex : lock;
...@@ -123,11 +148,22 @@ class Queue { ...@@ -123,11 +148,22 @@ class Queue {
key=0; // Must free this one _before_ the signal... key=0; // Must free this one _before_ the signal...
r_cond::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 class Result
{ {
...@@ -175,6 +211,18 @@ class Farm ...@@ -175,6 +211,18 @@ class Farm
if( done_cb ) if( done_cb )
done_cb( what, 0 ); 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 static class Handler
...@@ -247,10 +295,22 @@ class Farm ...@@ -247,10 +295,22 @@ class Farm
+"\n\n"); +"\n\n");
} }
void create() static void create()
{ {
thread = thread_create( handler ); 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(); static Mutex mutex = Mutex();
...@@ -288,12 +348,8 @@ class Farm ...@@ -288,12 +348,8 @@ class Farm
aquire_thread()->run( q[1], q[0] ); 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) void go(mixed vn, int err)
{ {
([array]r->value)[ i ] = vn; ([array]r->value)[ i ] = vn;
...@@ -303,10 +359,6 @@ class Farm ...@@ -303,10 +359,6 @@ class Farm
r->provide( r->value ); r->provide( r->value );
destruct(); 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 ) object run_multiple( array fun_args )
...@@ -380,10 +432,23 @@ class Farm ...@@ -380,10 +432,23 @@ class Farm
return res; 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 ); thread_create( dispatcher );
} }
} }
#endif /* constant(thread_create) */ #endif /* constant(thread_create) */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment