Browse Source

Misc changes

tags/1.9.4
falkTX 11 years ago
parent
commit
808e2fb805
2 changed files with 47 additions and 32 deletions
  1. +4
    -0
      source/utils/CarlaString.hpp
  2. +43
    -32
      source/utils/CarlaThread.hpp

+ 4
- 0
source/utils/CarlaString.hpp View File

@@ -306,6 +306,7 @@ public:
return (std::strncmp(fBuffer, prefix, prefixLen) == 0); return (std::strncmp(fBuffer, prefix, prefixLen) == 0);
} }


#if 0
/* /*
* Check if the string starts with the string 'prefix'. * Check if the string starts with the string 'prefix'.
*/ */
@@ -313,6 +314,7 @@ public:
{ {
return startsWith(prefix.fBuffer); return startsWith(prefix.fBuffer);
} }
#endif


/* /*
* Check if the string ends with the character 'c'. * Check if the string ends with the character 'c'.
@@ -339,6 +341,7 @@ public:
return (std::strncmp(fBuffer + (fBufferLen-suffixLen), suffix, suffixLen) == 0); return (std::strncmp(fBuffer + (fBufferLen-suffixLen), suffix, suffixLen) == 0);
} }


#if 0
/* /*
* Check if the string ends with the string 'suffix'. * Check if the string ends with the string 'suffix'.
*/ */
@@ -346,6 +349,7 @@ public:
{ {
return endsWith(suffix.fBuffer); return endsWith(suffix.fBuffer);
} }
#endif


/* /*
* Find the first occurrence of character 'c' in the string. * Find the first occurrence of character 'c' in the string.


+ 43
- 32
source/utils/CarlaThread.hpp View File

@@ -21,6 +21,12 @@
#include "CarlaMutex.hpp" #include "CarlaMutex.hpp"
#include "CarlaString.hpp" #include "CarlaString.hpp"


#if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
// has pthread_setname_np
#elif defined(CARLA_OS_LINUX)
# include <sys/prctl.h>
#endif

// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// CarlaThread class // CarlaThread class


@@ -76,33 +82,36 @@ public:
/* /*
* Start the thread. * Start the thread.
*/ */
void startThread() noexcept
bool startThread() noexcept
{ {
CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(),);
// check if already running
CARLA_SAFE_ASSERT_RETURN(! isThreadRunning(), true);


const CarlaMutexLocker sl(fLock);
const CarlaMutexLocker cml(fLock);


fShouldExit = false; fShouldExit = false;


pthread_t threadId;

if (pthread_create(&threadId, nullptr, _entryPoint, this) == 0)
if (pthread_create(&fHandle, nullptr, _entryPoint, this) == 0)
{ {
#if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
if (fName.isNotEmpty())
pthread_setname_np(threadId, fName);
#endif
pthread_detach(threadId);

#ifdef CARLA_OS_WIN #ifdef CARLA_OS_WIN
*(const_cast<pthread_t*>(&fHandle)) = threadId;
CARLA_SAFE_ASSERT_RETURN(fHandle.p != nullptr, false);
#else #else
fHandle = threadId;
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);

// wait for thread to start // wait for thread to start
fLock.lock(); fLock.lock();

return true;
} }

return false;
} }


/* /*
@@ -114,7 +123,7 @@ public:
*/ */
bool stopThread(const int timeOutMilliseconds) noexcept bool stopThread(const int timeOutMilliseconds) noexcept
{ {
const CarlaMutexLocker sl(fLock);
const CarlaMutexLocker cml(fLock);


if (isThreadRunning()) if (isThreadRunning())
{ {
@@ -125,7 +134,7 @@ public:
// Wait for the thread to stop // Wait for the thread to stop
int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2; int timeOutCheck = (timeOutMilliseconds == 1 || timeOutMilliseconds == -1) ? timeOutMilliseconds : timeOutMilliseconds/2;


while (isThreadRunning())
for (; isThreadRunning();)
{ {
carla_msleep(2); carla_msleep(2);


@@ -142,9 +151,11 @@ public:
if (isThreadRunning()) if (isThreadRunning())
{ {
// should never happen! // should never happen!
carla_stderr2("Carla assertion failure: \"! isRunning()\" in file %s, line %i", __FILE__, __LINE__);
carla_stderr2("Carla assertion failure: \"! isThreadRunning()\" in file %s, line %i", __FILE__, __LINE__);


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


try { try {
@@ -166,25 +177,24 @@ public:
fShouldExit = true; fShouldExit = true;
} }


static void setCurrentThreadName(const char* const name) noexcept
{
CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);

#if defined(__GLIBC__) && (__GLIBC__ * 1000 + __GLIBC_MINOR__) >= 2012
if (fName.isNotEmpty())
pthread_setname_np(pthread_self(), fName);
#elif defined(CARLA_OS_LINUX)
prctl(PR_SET_NAME, name, 0, 0, 0);
#endif
}

private: private:
CarlaMutex fLock; // Thread lock CarlaMutex fLock; // Thread lock
const CarlaString fName; // Thread name const CarlaString fName; // Thread name
volatile 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


/*
* Static null thread.
*/
static pthread_t& _null() noexcept
{
#ifdef CARLA_OS_WIN
static pthread_t sThread = { nullptr, 0 };
#else
static pthread_t sThread = 0;
#endif
return sThread;
}

void _init() noexcept void _init() noexcept
{ {
#ifdef CARLA_OS_WIN #ifdef CARLA_OS_WIN
@@ -214,6 +224,7 @@ private:
return nullptr; return nullptr;
} }


CARLA_PREVENT_HEAP_ALLOCATION
CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread) CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThread)
}; };




Loading…
Cancel
Save