Browse Source

Misc

tags/1.9.4
falkTX 11 years ago
parent
commit
ecab42e21c
2 changed files with 70 additions and 19 deletions
  1. +25
    -19
      source/utils/CarlaThread.hpp
  2. +45
    -0
      source/utils/CarlaUtils.hpp

+ 25
- 19
source/utils/CarlaThread.hpp View File

@@ -40,7 +40,7 @@ protected:
: fName(threadName), : fName(threadName),
fShouldExit(false) fShouldExit(false)
{ {
_init();
carla_zeroStruct<pthread_t>(fHandle);
} }


/* /*
@@ -58,6 +58,8 @@ protected:
*/ */
virtual void run() = 0; virtual void run() = 0;


// -------------------------------------------------------------------

public: public:
/* /*
* Check if the thread is running. * Check if the thread is running.
@@ -98,11 +100,6 @@ public:
#else #else
CARLA_SAFE_ASSERT_RETURN(fHandle != 0, false); CARLA_SAFE_ASSERT_RETURN(fHandle != 0, false);
#endif #endif

#if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
if (fName.isNotEmpty())
pthread_setname_np(fHandle, fName);
#endif
pthread_detach(fHandle); pthread_detach(fHandle);


// wait for thread to start // wait for thread to start
@@ -153,10 +150,10 @@ public:
// should never happen! // should never happen!
carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__); carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);


// use a copy thread id so we can clear our own one
// copy thread id so we can clear our one
pthread_t threadId; pthread_t threadId;
carla_copyStruct<pthread_t>(threadId, fHandle); carla_copyStruct<pthread_t>(threadId, fHandle);
_init();
carla_zeroStruct<pthread_t>(fHandle);


try { try {
pthread_cancel(threadId); pthread_cancel(threadId);
@@ -177,6 +174,20 @@ public:
fShouldExit = true; fShouldExit = true;
} }


// -------------------------------------------------------------------

/*
* Returns the name of the thread.
* This is the name that gets set in the constructor.
*/
const CarlaString& getThreadName() const noexcept
{
return fName;
}

/*
* Changes the name of the caller thread.
*/
static void setCurrentThreadName(const char* const name) noexcept static void setCurrentThreadName(const char* const name) noexcept
{ {
CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',); CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
@@ -188,33 +199,28 @@ public:
#endif #endif
} }


// -------------------------------------------------------------------

private: private:
CarlaMutex fLock; // Thread lock CarlaMutex fLock; // Thread lock
const CarlaString fName; // Thread name const CarlaString fName; // Thread name
pthread_t fHandle; // Handle for this thread pthread_t fHandle; // Handle for this thread
volatile bool fShouldExit; // true if thread should exit volatile bool fShouldExit; // true if thread should exit


void _init() noexcept
{
#ifdef CARLA_OS_WIN
fHandle.p = nullptr;
fHandle.x = 0;
#else
fHandle = 0;
#endif
}

void _runEntryPoint() noexcept void _runEntryPoint() noexcept
{ {
// report ready // report ready
fLock.unlock(); fLock.unlock();


setCurrentThreadName(fName);

try { try {
run(); run();
} catch(...) {} } catch(...) {}


// done // done
_init();
const CarlaMutexLocker cml(fLock);
carla_zeroStruct<pthread_t>(fHandle);
} }


static void* _entryPoint(void* userData) noexcept static void* _entryPoint(void* userData) noexcept


+ 45
- 0
source/utils/CarlaUtils.hpp View File

@@ -395,6 +395,21 @@ void carla_zeroStruct(T& structure) noexcept
std::memset(&structure, 0, sizeof(T)); std::memset(&structure, 0, sizeof(T));
} }


#if 0
/*
* Clear a single struct/class, volatile version.
*/
template <typename T>
static inline
void carla_zeroStruct(volatile T& structure) noexcept
{
volatile uint8_t* data((volatile uint8_t*)&structure);

for (size_t i=0; i < sizeof(T); ++i)
*data++ = 0;
}
#endif

/* /*
* Clear an array of struct/classes. * Clear an array of struct/classes.
*/ */
@@ -418,6 +433,36 @@ void carla_copyStruct(T& struct1, const T& struct2) noexcept
std::memcpy(&struct1, &struct2, sizeof(T)); std::memcpy(&struct1, &struct2, sizeof(T));
} }


#if 0
/*
* Copy a single struct/class, volatile version.
*/
template <typename T>
static inline
void carla_copyStruct(volatile T& struct1, const T& struct2) noexcept
{
volatile uint8_t* data1((volatile uint8_t*)&struct1);
const uint8_t* data2((const uint8_t*)&struct2);

for (size_t i=0; i < sizeof(T); ++i)
*data1++ = *data2++;
}

/*
* Copy a single struct/class, volatile version.
*/
template <typename T>
static inline
void carla_copyStruct(T& struct1, const volatile T& struct2) noexcept
{
uint8_t* data1((uint8_t*)&struct1);
volatile const uint8_t* data2((volatile const uint8_t*)&struct2);

for (size_t i=0; i < sizeof(T); ++i)
*data1++ = *data2++;
}
#endif

/* /*
* Copy an array of struct/classes. * Copy an array of struct/classes.
*/ */


Loading…
Cancel
Save