Skip to content

Commit

Permalink
fix dll export prob on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Nov 24, 2023
1 parent f607156 commit c9488af
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 6 additions & 6 deletions include/co/co/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
#include <pthread.h>
#endif

// add std::mutex_guard
namespace std {
typedef std::lock_guard<std::mutex> mutex_guard;
} // std

namespace co {
namespace xx {

__coapi extern __thread uint32 g_tid;
__coapi uint32 thread_id();

#ifdef _WIN32
typedef DWORD tls_key_t;
inline void tls_init(tls_key_t* k) { *k = TlsAlloc(); assert(*k != TLS_OUT_OF_INDEXES); }
Expand All @@ -38,13 +34,18 @@ inline void tls_init(tls_key_t* k) { int r = pthread_key_create(k, 0); (void)r;
inline void tls_free(tls_key_t k) { int r = pthread_key_delete(k); (void)r; assert(r == 0); }
inline void* tls_get(tls_key_t k) { return pthread_getspecific(k); }
inline void tls_set(tls_key_t k, void* v) { int r = pthread_setspecific(k, v); (void)r; assert(r == 0); }
__coapi extern __thread uint32 g_tid;
__coapi uint32 thread_id();
#endif
} // xx

// get id of the current thread
#ifdef _WIN32
inline uint32 thread_id() { return GetCurrentThreadId(); }
#else
inline uint32 thread_id() {
return xx::g_tid != 0 ? xx::g_tid : (xx::g_tid = xx::thread_id());
}
#endif

template<typename T>
class tls {
Expand Down Expand Up @@ -75,7 +76,6 @@ class tls {
DISALLOW_COPY_AND_ASSIGN(tls);
};

// for threads only
class __coapi sync_event {
public:
explicit sync_event(bool manual_reset = false, bool signaled = false);
Expand Down
6 changes: 2 additions & 4 deletions src/co/co.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
namespace co {
namespace xx {

__thread uint32 g_tid;

#ifdef _WIN32
typedef CRITICAL_SECTION mutex_t;
typedef CONDITION_VARIABLE cv_t;
Expand All @@ -27,8 +25,6 @@ inline bool cv_wait(cv_t* c, mutex_t* m, uint32 ms) { return SleepConditionVaria
inline void cv_notify_one(cv_t* c) { WakeConditionVariable(c); }
inline void cv_notify_all(cv_t* c) { WakeAllConditionVariable(c); }

uint32 thread_id() { return GetCurrentThreadId(); }

class mutex {
public:
mutex() { InitializeCriticalSection(&_m); }
Expand Down Expand Up @@ -93,6 +89,8 @@ inline void cv_wait(cv_t* c, mutex_t* m) { pthread_cond_wait(c, m); }
inline void cv_notify_one(cv_t* c) { pthread_cond_signal(c); }
inline void cv_notify_all(cv_t* c) { pthread_cond_broadcast(c); }

__thread uint32 g_tid;

#ifdef __linux__
#ifndef SYS_gettid
#define SYS_gettid __NR_gettid
Expand Down

0 comments on commit c9488af

Please sign in to comment.