Files
mongo/util/concurrency/task.cpp

144 lines
3.4 KiB
C++
Raw Normal View History

2010-05-17 17:11:48 -04:00
// @file task.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,b
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "task.h"
2010-05-18 12:17:43 -04:00
#include "../goodies.h"
2010-05-19 09:02:27 -04:00
#include "../unittest.h"
#include "boost/thread/condition.hpp"
2010-05-17 17:11:48 -04:00
namespace mongo {
2010-05-17 17:29:19 -04:00
namespace task {
2010-05-19 09:02:27 -04:00
/*void foo() {
boost::mutex m;
boost::mutex::scoped_lock lk(m);
boost::condition cond;
cond.wait(lk);
cond.notify_one();
}*/
2010-05-17 18:06:53 -04:00
Task::Task() {
n = 0;
repeat = 0;
2010-07-28 11:06:04 -04:00
deleteSelf = true;
2010-05-17 18:06:53 -04:00
}
void Task::halt() { repeat = 0; }
void Task::run() {
assert( n == 0 );
while( 1 ) {
n++;
try {
doWork();
}
catch(...) { }
if( repeat == 0 )
break;
sleepmillis(repeat);
2010-07-23 23:26:27 -04:00
if( inShutdown() )
break;
2010-05-17 18:06:53 -04:00
}
}
2010-07-28 11:06:04 -04:00
void Task::begin() {
2010-05-17 18:06:53 -04:00
go();
}
2010-07-28 11:06:04 -04:00
void fork(Task *t) {
t->begin();
2010-05-17 18:06:53 -04:00
}
2010-05-17 17:29:19 -04:00
2010-07-28 11:06:04 -04:00
void repeat(Task *t, unsigned millis) {
2010-05-17 18:06:53 -04:00
t->repeat = millis;
2010-07-28 11:06:04 -04:00
t->begin();
2010-05-17 17:29:19 -04:00
}
2010-05-19 09:02:27 -04:00
2010-05-17 17:29:19 -04:00
}
2010-05-19 09:02:27 -04:00
}
#include "msg.h"
2010-05-17 17:29:19 -04:00
/* task::Server */
2010-05-19 11:56:58 -04:00
2010-05-19 09:02:27 -04:00
namespace mongo {
namespace task {
2010-06-29 13:42:53 -04:00
/* to get back a return value */
2010-05-25 08:34:49 -04:00
struct Ret {
Ret() : done(false) { }
bool done;
boost::mutex m;
boost::condition c;
const lam *msg;
2010-05-25 08:34:49 -04:00
void f() {
(*msg)();
done = true;
c.notify_one();
}
};
void Server::call( const lam& msg ) {
2010-05-25 08:34:49 -04:00
Ret r;
r.msg = &msg;
lam f = boost::bind(&Ret::f, &r);
send(f);
{
boost::mutex::scoped_lock lk(r.m);
while( !r.done )
r.c.wait(lk);
}
}
void Server::send( lam msg ) {
2010-05-19 11:56:58 -04:00
{
boost::mutex::scoped_lock lk(m);
d.push_back(msg);
}
c.notify_one();
}
2010-05-19 09:02:27 -04:00
void Server::doWork() {
2010-06-29 13:42:53 -04:00
starting();
2010-06-29 15:52:35 -04:00
rq = false;
2010-05-19 11:56:58 -04:00
while( 1 ) {
2010-05-25 08:34:49 -04:00
lam f;
2010-05-19 11:56:58 -04:00
{
boost::mutex::scoped_lock lk(m);
while( d.empty() )
2010-05-19 12:23:22 -04:00
c.wait(lk);
2010-05-24 08:06:05 -04:00
f = d.front();
2010-05-19 11:56:58 -04:00
d.pop_front();
}
try {
2010-05-24 08:06:05 -04:00
f();
2010-06-29 15:52:35 -04:00
if( rq ) {
rq = false;
send(f);
}
2010-05-19 11:56:58 -04:00
} catch(std::exception& e) {
log() << "Server::doWork() exception " << e.what() << endl;
2010-05-19 11:56:58 -04:00
}
}
}
2010-05-19 09:02:27 -04:00
}
2010-05-17 17:11:48 -04:00
}