1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-01 16:19:43 +02:00

Move class ODTaskThread out of ODTaskThread.* (!) ...

... freeing three files from dependency cycles.

The class is only constructed by ODManager so move its definition there.

Meanwhile, the (now misnamed) ODTaskThread.cpp implements some thread
synchronization utilities used in several other places, not dependent now on
ODManager.cpp, and also not on ODTask.cpp.

These utilities should be eliminated in favor of standard library alternatives,
but that is a task for later.
This commit is contained in:
Paul Licameli 2019-05-11 13:07:52 -04:00
parent 191cd23b54
commit 6bed41a9be
3 changed files with 104 additions and 102 deletions

View File

@ -26,6 +26,110 @@ ODTask requests and internals.
#include <wx/thread.h>
#include <wx/event.h>
#ifdef __WXMAC__
// On Mac OS X, it's better not to use the wxThread class.
// We use our own implementation based on pthreads instead.
class ODTaskThread {
public:
typedef int ExitCode;
ODTaskThread(ODTask* task);
/*ExitCode*/ void Entry();
void Create() {}
void Delete() {
mDestroy = true;
pthread_join(mThread, NULL);
}
bool TestDestroy() { return mDestroy; }
void Sleep(int ms) {
struct timespec spec;
spec.tv_sec = 0;
spec.tv_nsec = ms * 1000 * 1000;
nanosleep(&spec, NULL);
}
static void *callback(void *p) {
ODTaskThread *th = (ODTaskThread *)p;
#if defined(__WXMAC__)
/*return (void *)*/ th->Entry();
return NULL;
#else
return (void *) th->Entry();
#endif
}
void Run() {
pthread_create(&mThread, NULL, callback, this);
}
///Specifies the priority the thread will run at. Currently doesn't work.
///@param priority value from 0 (min priority) to 100 (max priority)
void SetPriority(int priority)
{
mPriority=priority;
}
private:
int mPriority;
bool mDestroy;
pthread_t mThread;
ODTask* mTask;
};
#else
class ODTaskThread final : public wxThread
{
public:
///Constructs a ODTaskThread
///@param task the task to be launched as an
ODTaskThread(ODTask* task);
protected:
///Executes a part of the task
void* Entry() override;
ODTask* mTask;
};
#endif
ODTaskThread::ODTaskThread(ODTask* task)
#ifndef __WXMAC__
: wxThread()
#endif
{
mTask=task;
#ifdef __WXMAC__
mDestroy = false;
mThread = NULL;
#endif
}
#ifdef __WXMAC__
void ODTaskThread::Entry()
#else
void *ODTaskThread::Entry()
#endif
{
//TODO: Figure out why this has no effect at all.
//wxThread::This()->SetPriority( 40);
//Do at least 5 percent of the task
mTask->DoSome(0.05f);
//release the thread count so that the ODManager knows how many active threads are alive.
ODManager::Instance()->DecrementCurrentThreads();
#ifndef __WXMAC__
return NULL;
#endif
}
static ODLock gODInitedMutex;
static bool gManagerCreated=false;
static bool gPause=false; //to be loaded in and used with Pause/Resume before ODMan init.

View File

@ -18,44 +18,6 @@
#include "ODTaskThread.h"
#include "ODTask.h"
#include "ODManager.h"
ODTaskThread::ODTaskThread(ODTask* task)
#ifndef __WXMAC__
: wxThread()
#endif
{
mTask=task;
#ifdef __WXMAC__
mDestroy = false;
mThread = NULL;
#endif
}
#ifdef __WXMAC__
void ODTaskThread::Entry()
#else
void *ODTaskThread::Entry()
#endif
{
//TODO: Figure out why this has no effect at all.
//wxThread::This()->SetPriority( 40);
//Do at least 5 percent of the task
mTask->DoSome(0.05f);
//release the thread count so that the ODManager knows how many active threads are alive.
ODManager::Instance()->DecrementCurrentThreads();
#ifndef __WXMAC__
return NULL;
#endif
}
#ifdef __WXMAC__
ODCondition::ODCondition(ODLock *lock)

View File

@ -32,57 +32,9 @@ class ODTask;
#ifdef __WXMAC__
// On Mac OS X, it's better not to use the wxThread class.
// We use our own implementation based on pthreads instead.
#include <pthread.h>
#include <time.h>
class ODTaskThread {
public:
typedef int ExitCode;
ODTaskThread(ODTask* task);
/*ExitCode*/ void Entry();
void Create() {}
void Delete() {
mDestroy = true;
pthread_join(mThread, NULL);
}
bool TestDestroy() { return mDestroy; }
void Sleep(int ms) {
struct timespec spec;
spec.tv_sec = 0;
spec.tv_nsec = ms * 1000 * 1000;
nanosleep(&spec, NULL);
}
static void *callback(void *p) {
ODTaskThread *th = (ODTaskThread *)p;
#if defined(__WXMAC__)
/*return (void *)*/ th->Entry();
return NULL;
#else
return (void *) th->Entry();
#endif
}
void Run() {
pthread_create(&mThread, NULL, callback, this);
}
///Specifies the priority the thread will run at. Currently doesn't work.
///@param priority value from 0 (min priority) to 100 (max priority)
void SetPriority(int priority)
{
mPriority=priority;
}
private:
int mPriority;
bool mDestroy;
pthread_t mThread;
ODTask* mTask;
};
class ODLock {
public:
ODLock(){
@ -132,22 +84,6 @@ protected:
#else
class ODTaskThread final : public wxThread
{
public:
///Constructs a ODTaskThread
///@param task the task to be launched as an
ODTaskThread(ODTask* task);
protected:
///Executes a part of the task
void* Entry() override;
ODTask* mTask;
};
//a wrapper for wxMutex.
class AUDACITY_DLL_API ODLock final : public wxMutex
{