created 2 subclasses of RsThread, one for ticking services, and one for single shot jobs. Now all threads use the same base code.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8288 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2015-05-22 20:54:38 +00:00
parent f2d4a237ca
commit e9b9dce9f5
28 changed files with 317 additions and 335 deletions

View file

@ -56,11 +56,33 @@ void *RsThread::rsthread_init(void* p)
{
return NULL;
}
thread -> run();
// tell the OS to free the thread resources when this function exits
// it is a replacement for pthread_join()
pthread_detach(pthread_self());
thread -> runloop();
return NULL;
}
RsThread::RsThread () : mMutex("RsThread")
{
sem_init(&mHasStoppedSemaphore,0,1) ;
void RsThread::shutdown()
#ifdef WINDOWS_SYS
memset (&mTid, 0, sizeof(mTid));
#else
mTid = 0;
#endif
}
bool RsThread::isRunning()
{
// do we need a mutex for this ?
int sval =0;
sem_getvalue(&mHasStoppedSemaphore,&sval) ;
return !sval ;
}
void RsTickingThread::shutdown()
{
#ifdef DEBUG_THREADS
std::cerr << "pqithreadstreamer::stop()" << std::endl;
@ -83,7 +105,7 @@ void RsThread::shutdown()
sem_post(&mShouldStopSemaphore) ;
}
void RsThread::fullstop()
void RsTickingThread::fullstop()
{
shutdown() ;
@ -107,7 +129,6 @@ void RsThread::start()
std::cerr << " initing should_stop=0" << std::endl;
std::cerr << " initing has_stopped=1" << std::endl;
#endif
sem_init(&mShouldStopSemaphore,0,0) ;
sem_init(&mHasStoppedSemaphore,0,0) ;
int err ;
@ -125,37 +146,19 @@ void RsThread::start()
}
RsThread::RsThread () : mMutex("RsThread")
RsTickingThread::RsTickingThread ()
{
sem_init(&mShouldStopSemaphore,0,0) ;
sem_init(&mHasStoppedSemaphore,0,1) ;
#ifdef WINDOWS_SYS
memset (&mTid, 0, sizeof(mTid));
#else
mTid = 0;
#endif
}
bool RsThread::isRunning()
{
// do we need a mutex for this ?
int sval =0;
sem_getvalue(&mHasStoppedSemaphore,&sval) ;
return !sval ;
}
void RsThread::run()
void RsTickingThread::runloop()
{
#ifdef DEBUG_THREADS
std::cerr << "pqithreadstream::run()";
std::cerr << std::endl;
#endif
// tell the OS to free the thread resources when this function exits
// it is a replacement for pthread_join()
pthread_detach(pthread_self());
sem_init(&mShouldStopSemaphore,0,0) ;
while(1)
{

View file

@ -177,31 +177,50 @@ pthread_t createThread(RsThread &thread);
class RsThread
{
public:
public:
RsThread();
virtual ~RsThread() {}
void start() ;
void shutdown();
void fullstop();
void join() { fullstop() ; } // used for compatibility
bool isRunning();
protected:
void run() ; /* called once the thread is started. Should be overloaded by subclasses. */
virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */
private:
static void *rsthread_init(void*) ;
pthread_t mTid;
RsMutex mMutex;
sem_t mShouldStopSemaphore;
sem_t mHasStoppedSemaphore;
static void *rsthread_init(void*) ;
RsMutex mMutex;
pthread_t mTid;
};
class RsTickingThread: public RsThread
{
public:
RsTickingThread();
class RsQueueThread: public RsThread
void shutdown();
void fullstop();
void join() { fullstop() ; } // used for compatibility
virtual void data_tick() =0;
private:
virtual void runloop() ; /* called once the thread is started. Should be overloaded by subclasses. */
sem_t mShouldStopSemaphore;
};
class RsSingleJobThread: public RsThread
{
public:
virtual void run() =0;
protected:
virtual void runloop() { run(); } /* called once the thread is started. Should be overloaded by subclasses. */
};
class RsQueueThread: public RsTickingThread
{
public: