0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
This commit is contained in:
Dwight 2010-08-04 12:13:22 -04:00
parent c5ca6d00ee
commit 9c19c738a4
2 changed files with 30 additions and 8 deletions

View File

@ -27,13 +27,13 @@ namespace mongo {
typedef boost::function<void()> lam;
/** typical usage is: task::fork( serverPtr ); */
/** typical usage is: task::fork( new Server("threadname") ); */
class Server : public Task {
public:
/** send a message to the port */
void send(lam);
Server(string name) : _name(name) { }
Server(string name) : _name(name), rq(false) { }
virtual ~Server() { }
/** send message but block until function completes */
@ -42,9 +42,8 @@ namespace mongo {
void requeue() { rq = true; }
protected:
/* this needn't be abstract; i left it that way for now so i remember
to call Client::initThread() when using in mongo... */
virtual void starting() = 0;
/* REMINDER : for use in mongod, you will want to have this call Client::initThread(). */
virtual void starting() { }
private:
virtual bool initClient() { return true; }

View File

@ -117,7 +117,6 @@ namespace mongo {
void Server::doWork() {
starting();
rq = false;
while( 1 ) {
lam f;
try {
@ -134,7 +133,10 @@ namespace mongo {
f();
if( rq ) {
rq = false;
send(f);
{
boost::mutex::scoped_lock lk(m);
d.push_back(f);
}
}
} catch(std::exception& e) {
log() << "Server::doWork() exception " << e.what() << endl;
@ -143,6 +145,27 @@ namespace mongo {
}
}
}
static Server *s;
static void abc(int i) {
cout << "Hello " << i << endl;
s->requeue();
}
class TaskUnitTest : public mongo::UnitTest {
public:
virtual void run() {
lam f = boost::bind(abc, 3);
//f();
s = new Server("unittest");
fork(s);
s->send(f);
sleepsecs(30);
cout <<" done" << endl;
}
}; // not running. taskunittest;
}
}