1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
#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;
class Fifo {
inherit Condition : r_cond;
inherit Condition : w_cond;
inherit Mutex : lock;
array buffer;
int ptr, num;
int read_tres, write_tres;
int size() { return num; }
mixed read()
{
mixed tmp;
object key=lock::lock(2);
while(!num) r_cond::wait(key);
tmp=buffer[ptr];
buffer[ptr++] = 0; // Throw away any references.
ptr%=sizeof(buffer);
if(read_tres < sizeof(buffer))
{
if(num-- == read_tres)
w_cond::broadcast();
}else{
num--;
w_cond::broadcast();
}
key = 0;
return tmp;
}
array read_array()
{
array ret;
object key=lock::lock(2);
while(!num) r_cond::wait(key);
if(num==1)
{
ret=buffer[ptr..ptr];
buffer[ptr++] = 0; // Throw away any references.
ptr%=sizeof(buffer);
num--;
}else{
ret=buffer[ptr..]+buffer[..num-sizeof(ret)-1];
ptr=num=0;
buffer=allocate(sizeof(buffer)); // Throw away any references.
}
key = 0;
w_cond::broadcast();
return ret;
}
void write(mixed v)
{
object key=lock::lock(2);
while(num == sizeof(buffer)) w_cond::wait(key);
buffer[(ptr + num) % sizeof(buffer)]=v;
if(write_tres)
{
if(num++ == write_tres)
r_cond::broadcast();
}else{
num++;
r_cond::broadcast();
}
key = 0;
}
void create(int|void size)
{
write_tres=0;
buffer=allocate(read_tres=size || 128);
}
};
class Queue {
inherit Condition : r_cond;
inherit Mutex : lock;
array buffer=allocate(16);
int r_ptr, w_ptr;
int size() { return w_ptr - r_ptr; }
mixed read()
{
mixed tmp;
object key=lock::lock(2);
while(!size()) r_cond::wait(key);
tmp=buffer[r_ptr];
buffer[r_ptr++] = 0; // Throw away any references.
key=0;
return tmp;
}
void write(mixed v)
{
object key=lock::lock(2);
if(w_ptr >= sizeof(buffer))
{
buffer=buffer[r_ptr..];
buffer+=allocate(sizeof(buffer)+1);
w_ptr-=r_ptr;
r_ptr=0;
}
buffer[w_ptr]=v;
w_ptr++;
key=0; // Must free this one _before_ the signal...
r_cond::signal();
}
}
class Farm
{
class Result
{
int ready;
mixed value;
function done_cb;
int status()
{
return ready;
}
mixed result()
{
return value;
}
mixed `()()
{
while(!ready) ft_cond->wait();
if( ready < 0 ) throw( value );
return value;
}
void set_done_cb( function to )
{
if( ready )
to( value, ready<0 );
else
done_cb = to;
}
void provide_error( mixed what )
{
value = what;
ready = -1;
if( done_cb )
done_cb( what, 1 );
}
void provide( mixed what )
{
ready = 1;
value = what;
if( done_cb )
done_cb( what, 0 );
}
}
static class Handler
{
Condition cond = Condition();
array(object|array(function|array)) job;
object thread;
float total_time;
int handled, max_time;
static int ready;
void handler()
{
array(object|array(function|array)) q;
while( 1 )
{
ready = 1;
cond->wait();
if( q = job )
{
mixed res, err;
int st = gethrtime();
if( err = catch(res = q[1][0]( @q[1][1] )) && q[0])
([object]q[0])->provide_error( err );
else if( q[0] )
([object]q[0])->provide( res );
object lock = mutex->lock();
free_threads += ({ this_object() });
lock = 0;
st = gethrtime()-st;
total_time += st/1000.0;
handled++;
job = 0;
if( st > max_time )
max_time = st;
ft_cond->broadcast();
} else {
object lock = mutex->lock();
threads -= ({ this_object() });
free_threads -= ({ this_object() });
lock = 0;
destruct();
return;
}
}
}
void run( array(function|array) what, object|void resobj )
{
while(!ready) sleep(0.1);
job = ({ resobj, what });
cond->signal();
}
string debug_status()
{
return ("Thread:\n"
" Handled works: "+handled+"\n"+
(handled?
" Average time: "+((int)(total_time / handled))+"ms\n"
" Max time: "+sprintf("%2.2fms\n", max_time/1000.0):"")+
" Status: "+(job?"Working":"Idle" )+"\n"+
(job?
(" "+
replace( describe_backtrace(thread->backtrace()),
"\n",
"\n ")):"")
+"\n\n");
}
void create()
{
thread = thread_create( handler );
}
}
static Mutex mutex = Mutex();
static Condition ft_cond = Condition();
static Queue job_queue = Queue();
static array(Handler) threads = ({});
static array(Handler) free_threads = ({});
static int max_num_threads = 20;
static Handler aquire_thread()
{
object lock = mutex->lock();
while( !sizeof(free_threads) )
{
if( sizeof(threads) < max_num_threads )
{
threads += ({ Handler() });
free_threads += ({ threads[-1] });
} else {
lock = 0;
ft_cond->wait( );
mutex->lock();
}
}
object(Handler) t = free_threads[0];
free_threads = free_threads[1..];
return t;
}
static void dispatcher()
{
while( array q = [array]job_queue->read() )
aquire_thread()->run( q[1], q[0] );
}
static class ValueAdjuster
{
object r, r2;
mapping v;
int i;
void go(mixed vn, int err)
{
([array]r->value)[ i ] = vn;
if( err )
r->provide_error( err );
if( !--v->num_left )
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 )
{
Result r = Result(); // private result..
r->value = allocate( sizeof( fun_args ) );
mapping nl = ([ "num_left":sizeof( fun_args ) ]);
for( int i=0; i<sizeof( fun_args ); i++ )
{
Result r2 = Result();
r2->set_done_cb( ValueAdjuster( r, r2, i, nl )->go );
job_queue->write( ({ r2, fun_args[i] }) );
}
return r;
}
void run_multiple_async( array fun_args )
{
for( int i=0; i<sizeof( fun_args ); i++ )
job_queue->write( ({ 0, fun_args[i] }) );
}
object run( function f, mixed ... args )
{
object ro = Result();
job_queue->write( ({ 0, ({f, args }) }) );
return ro;
}
void run_async( function f, mixed ... args )
{
job_queue->write( ({ 0, ({f, args }) }) );
}
int set_max_num_threads( int to )
{
int omnt = max_num_threads;
if( to <= 0 )
error("Illegal argument 1 to set_max_num_threads,"
"num_threads must be > 0\n");
max_num_threads = to;
while( sizeof( threads ) > max_num_threads )
{
object key = mutex->lock();
while( sizeof( free_threads ) )
free_threads[0]->cond->signal();
key = 0;
if( sizeof( threads ) > max_num_threads)
ft_cond->wait();
}
ft_cond->broadcast( );
return omnt;
}
string debug_status()
{
string res = sprintf("Thread farm\n"
" Max threads = %d\n"
" Current threads = %d\n"
" Working threads = %d\n"
" Jobs in queue = %d\n\n",
max_num_threads, sizeof(threads),
(sizeof(threads)-sizeof(free_threads)),
job_queue->size() );
foreach( threads, Handler t )
res += t->debug_status();
return res;
}
void create()
{
thread_create( dispatcher );
}
}
#endif /* constant(thread_create) */
|