mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-02 14:03:41 +01:00
- fixed PS build script environment vars generation
- fixed MongoDB 64-bit std::size_t warnings - added Thread::trySleep()/wakeUp() and tests
This commit is contained in:
@@ -58,6 +58,11 @@ public:
|
||||
virtual void run() = 0;
|
||||
/// Do whatever the thread needs to do. Must
|
||||
/// be overridden by subclasses.
|
||||
|
||||
protected:
|
||||
void sleep(long milliseconds);
|
||||
|
||||
void trySleep(long milliseconds);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Event.h"
|
||||
#include "Poco/Mutex.h"
|
||||
|
||||
|
||||
@@ -186,6 +187,23 @@ public:
|
||||
bool isRunning() const;
|
||||
/// Returns true if the thread is running.
|
||||
|
||||
void trySleep(long milliseconds);
|
||||
/// Starts an interruptible sleep. Thread will remain suspended
|
||||
/// until (a) the timeout expires or (b) wakeUp() is called.
|
||||
/// The trySleep()/wakeUp() pair of fnctions should be used with
|
||||
/// understanding that this suspended state is not a true sleep,
|
||||
/// but rather a state of waiting for an event, with timeout
|
||||
/// expiration. This in essence means that calling wakeUp()
|
||||
/// before calling trySleep() will prevent the next trySleep()
|
||||
/// call to actually suspend the thread (which, in some scenarios,
|
||||
/// may be desirable behavior).
|
||||
|
||||
void wakeUp();
|
||||
/// Wakes up the thread which is in the state of interruptible
|
||||
/// sleep. For threads that are not suspended, calling this
|
||||
/// function has the effect of preventing the subsequent
|
||||
/// trySleep() call to put thread in a suspended state.
|
||||
|
||||
static void sleep(long milliseconds);
|
||||
/// Suspends the current thread for the specified
|
||||
/// amount of time.
|
||||
@@ -220,6 +238,7 @@ private:
|
||||
int _id;
|
||||
std::string _name;
|
||||
ThreadLocalStorage* _pTLS;
|
||||
Event _event;
|
||||
mutable FastMutex _mutex;
|
||||
|
||||
friend class ThreadLocalStorage;
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
|
||||
#include "Poco/Runnable.h"
|
||||
#include "Poco/Thread.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -50,4 +51,16 @@ Runnable::~Runnable()
|
||||
}
|
||||
|
||||
|
||||
void Runnable::sleep(long milliseconds)
|
||||
{
|
||||
Thread::sleep(milliseconds);
|
||||
}
|
||||
|
||||
|
||||
void Runnable::trySleep(long milliseconds)
|
||||
{
|
||||
Thread::current()->trySleep(milliseconds);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
@@ -62,6 +62,7 @@ Thread::Thread():
|
||||
_name(makeName()),
|
||||
_pTLS(0)
|
||||
{
|
||||
_event.reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +71,7 @@ Thread::Thread(const std::string& name):
|
||||
_name(name),
|
||||
_pTLS(0)
|
||||
{
|
||||
_event.reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -122,6 +124,18 @@ bool Thread::tryJoin(long milliseconds)
|
||||
}
|
||||
|
||||
|
||||
void Thread::trySleep(long milliseconds)
|
||||
{
|
||||
_event.tryWait(milliseconds);
|
||||
_event.reset();
|
||||
}
|
||||
|
||||
void Thread::wakeUp()
|
||||
{
|
||||
_event.set();
|
||||
}
|
||||
|
||||
|
||||
ThreadLocalStorage& Thread::tls()
|
||||
{
|
||||
if (!_pTLS)
|
||||
|
||||
@@ -134,6 +134,33 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class TrySleepRunnable : public Runnable
|
||||
{
|
||||
public:
|
||||
TrySleepRunnable() : _counter(0)
|
||||
{
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
trySleep(300000);
|
||||
++_counter;
|
||||
trySleep(300000);
|
||||
++_counter;
|
||||
trySleep(10);
|
||||
++_counter;
|
||||
}
|
||||
|
||||
int counter() const
|
||||
{
|
||||
return _counter;
|
||||
}
|
||||
|
||||
private:
|
||||
int _counter;
|
||||
};
|
||||
|
||||
|
||||
ThreadTest::ThreadTest(const std::string& name): CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
@@ -264,6 +291,33 @@ void ThreadTest::testNotJoin()
|
||||
}
|
||||
|
||||
|
||||
void ThreadTest::testTrySleep()
|
||||
{
|
||||
Thread thread;
|
||||
TrySleepRunnable r;
|
||||
assert(!thread.isRunning());
|
||||
assert(r.counter() == 0);
|
||||
thread.start(r);
|
||||
assert(thread.isRunning());
|
||||
assert(r.counter() == 0);
|
||||
Thread::sleep(100);
|
||||
assert(r.counter() == 0);
|
||||
thread.wakeUp();
|
||||
Thread::sleep(10);
|
||||
assert(r.counter() == 1);
|
||||
Thread::sleep(100);
|
||||
assert(r.counter() == 1);
|
||||
thread.wakeUp();
|
||||
Thread::sleep(10);
|
||||
assert(r.counter() == 2);
|
||||
Thread::sleep(100);
|
||||
assert(r.counter() == 3);
|
||||
assert(!thread.isRunning());
|
||||
thread.wakeUp();
|
||||
assert(!thread.isRunning());
|
||||
}
|
||||
|
||||
|
||||
void ThreadTest::testNotRun()
|
||||
{
|
||||
Thread thread;
|
||||
@@ -389,6 +443,7 @@ CppUnit::Test* ThreadTest::suite()
|
||||
CppUnit_addTest(pSuite, ThreadTest, testNotJoin);
|
||||
CppUnit_addTest(pSuite, ThreadTest, testNotRun);
|
||||
CppUnit_addTest(pSuite, ThreadTest, testNotRunJoin);
|
||||
CppUnit_addTest(pSuite, ThreadTest, testTrySleep);
|
||||
CppUnit_addTest(pSuite, ThreadTest, testThreadTarget);
|
||||
CppUnit_addTest(pSuite, ThreadTest, testThreadFunction);
|
||||
CppUnit_addTest(pSuite, ThreadTest, testThreadStackSize);
|
||||
|
||||
@@ -54,6 +54,7 @@ public:
|
||||
void testNotJoin();
|
||||
void testNotRun();
|
||||
void testNotRunJoin();
|
||||
void testTrySleep();
|
||||
void testThreadTarget();
|
||||
void testThreadFunction();
|
||||
void testThreadStackSize();
|
||||
|
||||
Reference in New Issue
Block a user