Browse Source

portable thread local storage class (#2229)

tags/20201208
nihui GitHub 5 years ago
parent
commit
a8f38f6170
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 65 additions and 39 deletions
  1. +65
    -39
      src/platform.h.in

+ 65
- 39
src/platform.h.in View File

@@ -60,30 +60,7 @@ private:
// NOTE SRWLock is available from windows vista
SRWLOCK srwlock;
};
#else // _WIN32
class Mutex
{
public:
Mutex() { pthread_mutex_init(&mutex, 0); }
~Mutex() { pthread_mutex_destroy(&mutex); }
void lock() { pthread_mutex_lock(&mutex); }
void unlock() { pthread_mutex_unlock(&mutex); }
private:
friend class ConditionVariable;
pthread_mutex_t mutex;
};
#endif // _WIN32

class MutexLockGuard
{
public:
MutexLockGuard(Mutex& _mutex) : mutex(_mutex) { mutex.lock(); }
~MutexLockGuard() { mutex.unlock(); }
private:
Mutex& mutex;
};

#if (defined _WIN32 && !(defined __MINGW32__))
class ConditionVariable
{
public:
@@ -95,21 +72,7 @@ public:
private:
CONDITION_VARIABLE condvar;
};
#else // _WIN32
class ConditionVariable
{
public:
ConditionVariable() { pthread_cond_init(&cond, 0); }
~ConditionVariable() { pthread_cond_destroy(&cond); }
void wait(Mutex& mutex) { pthread_cond_wait(&cond, &mutex.mutex); }
void broadcast() { pthread_cond_broadcast(&cond); }
void signal() { pthread_cond_signal(&cond); }
private:
pthread_cond_t cond;
};
#endif // _WIN32

#if (defined _WIN32 && !(defined __MINGW32__))
static unsigned __stdcall start_wrapper(void* args);
class Thread
{
@@ -129,7 +92,41 @@ private:
void* _args;
};

#else // _WIN32
class ThreadLocalStorage
{
public:
ThreadLocalStorage() { key = TlsAlloc(); }
~ThreadLocalStorage() { TlsFree(key); }
void set(void* value) { TlsSetValue(key, (LPVOID)value); }
void* get() { return (void*)TlsGetValue(key); }
private:
DWORD key;
};
#else // (defined _WIN32 && !(defined __MINGW32__))
class Mutex
{
public:
Mutex() { pthread_mutex_init(&mutex, 0); }
~Mutex() { pthread_mutex_destroy(&mutex); }
void lock() { pthread_mutex_lock(&mutex); }
void unlock() { pthread_mutex_unlock(&mutex); }
private:
friend class ConditionVariable;
pthread_mutex_t mutex;
};

class ConditionVariable
{
public:
ConditionVariable() { pthread_cond_init(&cond, 0); }
~ConditionVariable() { pthread_cond_destroy(&cond); }
void wait(Mutex& mutex) { pthread_cond_wait(&cond, &mutex.mutex); }
void broadcast() { pthread_cond_broadcast(&cond); }
void signal() { pthread_cond_signal(&cond); }
private:
pthread_cond_t cond;
};

class Thread
{
public:
@@ -139,7 +136,18 @@ public:
private:
pthread_t t;
};
#endif // _WIN32

class ThreadLocalStorage
{
public:
ThreadLocalStorage() { pthread_key_create(&key, 0); }
~ThreadLocalStorage() { pthread_key_delete(key); }
void set(void* value) { pthread_setspecific(key, value); }
void* get() { return pthread_getspecific(key); }
private:
pthread_key_t key;
};
#endif // (defined _WIN32 && !(defined __MINGW32__))
#else // NCNN_THREADS
class Mutex
{
@@ -167,8 +175,26 @@ public:
~Thread() {}
void join() {}
};

class ThreadLocalStorage
{
public:
ThreadLocalStorage() {}
~ThreadLocalStorage() {}
void set(void* /*value*/) {}
void* get() { return 0; }
};
#endif // NCNN_THREADS

class MutexLockGuard
{
public:
MutexLockGuard(Mutex& _mutex) : mutex(_mutex) { mutex.lock(); }
~MutexLockGuard() { mutex.unlock(); }
private:
Mutex& mutex;
};

} // namespace ncnn

#if NCNN_SIMPLESTL


Loading…
Cancel
Save