23f3361997-02-27Fredrik Hübinette (Hubbe) constant Mutex=__builtin.mutex; constant Condition=__builtin.condition;
609c0f1997-02-27Fredrik Hübinette (Hubbe)  class Fifo {
7f30a61997-02-27Fredrik Hübinette (Hubbe)  inherit Condition : r_cond; inherit Condition: w_cond; inherit Mutex: lock;
609c0f1997-02-27Fredrik Hübinette (Hubbe) 
37d0891998-05-27Fredrik Hübinette (Hubbe)  array buffer;
87e3691997-02-27Fredrik Hübinette (Hubbe)  int ptr, num;
37d0891998-05-27Fredrik Hübinette (Hubbe)  int read_tres, write_tres;
609c0f1997-02-27Fredrik Hübinette (Hubbe) 
87e3691997-02-27Fredrik Hübinette (Hubbe)  int size() { return num; }
609c0f1997-02-27Fredrik Hübinette (Hubbe)  mixed read() { mixed tmp; object key=lock::lock();
87e3691997-02-27Fredrik Hübinette (Hubbe)  while(!num) r_cond::wait(key);
b1dbcb1997-10-04Henrik Grubbström (Grubba)  tmp=buffer[ptr]; buffer[ptr++] = 0; // Throw away any references.
dff7f71997-02-27Fredrik Hübinette (Hubbe)  ptr%=sizeof(buffer);
0559c61999-06-11Fredrik Hübinette (Hubbe)  if(read_tres < sizeof(buffer)) { if(num-- == read_tres) w_cond::broadcast(); }else{ num--;
37d0891998-05-27Fredrik Hübinette (Hubbe)  w_cond::signal();
0559c61999-06-11Fredrik Hübinette (Hubbe)  }
609c0f1997-02-27Fredrik Hübinette (Hubbe)  return tmp; }
37d0891998-05-27Fredrik Hübinette (Hubbe)  array read_array() { array ret; object key=lock::lock(); 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. } w_cond::signal(); return ret; }
609c0f1997-02-27Fredrik Hübinette (Hubbe)  void write(mixed v) { object key=lock::lock();
87e3691997-02-27Fredrik Hübinette (Hubbe)  while(num == sizeof(buffer)) w_cond::wait(key);
37d0891998-05-27Fredrik Hübinette (Hubbe)  buffer[(ptr + num) % sizeof(buffer)]=v;
0559c61999-06-11Fredrik Hübinette (Hubbe)  if(write_tres) { if(num++ == write_tres) r_cond::broadcast(); }else{ num++;
37d0891998-05-27Fredrik Hübinette (Hubbe)  r_cond::signal();
0559c61999-06-11Fredrik Hübinette (Hubbe)  }
609c0f1997-02-27Fredrik Hübinette (Hubbe)  }
37d0891998-05-27Fredrik Hübinette (Hubbe) 
8d49101998-01-21Fredrik Hübinette (Hubbe)  void create(int|void size)
609c0f1997-02-27Fredrik Hübinette (Hubbe)  {
37d0891998-05-27Fredrik Hübinette (Hubbe)  write_tres=0; buffer=allocate(read_tres=size || 128);
609c0f1997-02-27Fredrik Hübinette (Hubbe)  } }; class Queue {
7f30a61997-02-27Fredrik Hübinette (Hubbe)  inherit Condition: r_cond; inherit Mutex: lock;
609c0f1997-02-27Fredrik Hübinette (Hubbe)  mixed *buffer=allocate(16); int r_ptr, w_ptr; int size() { return w_ptr - r_ptr; } mixed read() { mixed tmp; object key=lock::lock(); while(!size()) r_cond::wait(key);
b1dbcb1997-10-04Henrik Grubbström (Grubba)  tmp=buffer[r_ptr]; buffer[r_ptr++] = 0; // Throw away any references.
8e40e81997-09-01Per Hedbor  key=0;
609c0f1997-02-27Fredrik Hübinette (Hubbe)  return tmp; } void write(mixed v) { object key=lock::lock(); 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++;
8e40e81997-09-01Per Hedbor  key=0; // Must free this one _before_ the signal...
609c0f1997-02-27Fredrik Hübinette (Hubbe)  r_cond::signal(); } };