Skip to content

Commit 6be74d1

Browse files
committedOct 17, 2015
Refactor thread utility interface
- Add "thr_" prefix to thread utility functions - Compare threadid_ts in a portable manner, where possible
1 parent 836486a commit 6be74d1

File tree

6 files changed

+85
-25
lines changed

6 files changed

+85
-25
lines changed
 

Diff for: ‎src/client/tile.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ TextureSource::TextureSource(IrrlichtDevice *device):
439439
{
440440
assert(m_device); // Pre-condition
441441

442-
m_main_thread = get_current_thread_id();
442+
m_main_thread = thr_get_current_thread_id();
443443

444444
// Add a NULL TextureInfo as the first index, named ""
445445
m_textureinfo_cache.push_back(TextureInfo(""));
@@ -502,7 +502,7 @@ u32 TextureSource::getTextureId(const std::string &name)
502502
/*
503503
Get texture
504504
*/
505-
if (get_current_thread_id() == m_main_thread)
505+
if (thr_is_current_thread(m_main_thread))
506506
{
507507
return generateTexture(name);
508508
}
@@ -604,7 +604,7 @@ u32 TextureSource::generateTexture(const std::string &name)
604604
/*
605605
Calling only allowed from main thread
606606
*/
607-
if (get_current_thread_id() != m_main_thread) {
607+
if (!thr_is_current_thread(m_main_thread)) {
608608
errorstream<<"TextureSource::generateTexture() "
609609
"called not from main thread"<<std::endl;
610610
return 0;
@@ -704,7 +704,7 @@ void TextureSource::insertSourceImage(const std::string &name, video::IImage *im
704704
{
705705
//infostream<<"TextureSource::insertSourceImage(): name="<<name<<std::endl;
706706

707-
sanity_check(get_current_thread_id() == m_main_thread);
707+
sanity_check(thr_is_current_thread(m_main_thread));
708708

709709
m_sourcecache.insert(name, img, true, m_device->getVideoDriver());
710710
m_source_image_existence.set(name, true);
@@ -2057,7 +2057,7 @@ video::ITexture *TextureSource::getShaderFlagsTexture(bool normalmap_present)
20572057
{
20582058
std::string tname = "__shaderFlagsTexture";
20592059
tname += normalmap_present ? "1" : "0";
2060-
2060+
20612061
if (isKnownSourceImage(tname)) {
20622062
return getTexture(tname);
20632063
} else {

Diff for: ‎src/debug.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void sanity_check_fn(const char *assertion, const char *file,
4545
unsigned int line, const char *function)
4646
{
4747
errorstream << std::endl << "In thread " << std::hex
48-
<< (unsigned long)get_current_thread_id() << ":" << std::endl;
48+
<< (unsigned long)thr_get_current_thread_id() << ":" << std::endl;
4949
errorstream << file << ":" << line << ": " << function
5050
<< ": An engine assumption '" << assertion << "' failed." << std::endl;
5151

@@ -58,7 +58,7 @@ void fatal_error_fn(const char *msg, const char *file,
5858
unsigned int line, const char *function)
5959
{
6060
errorstream << std::endl << "In thread " << std::hex
61-
<< (unsigned long)get_current_thread_id() << ":" << std::endl;
61+
<< (unsigned long)thr_get_current_thread_id() << ":" << std::endl;
6262
errorstream << file << ":" << line << ": " << function
6363
<< ": A fatal error occured: " << msg << std::endl;
6464

@@ -130,6 +130,13 @@ void DebugStack::print(std::ostream &os, bool everything)
130130
os<<"Probably overflown."<<std::endl;
131131
}
132132

133+
// Note: Using pthread_t (that is, threadid_t on POSIX platforms) as the key to
134+
// a std::map is naughty. Formally, a pthread_t may only be compared using
135+
// pthread_equal() - pthread_t lacks the well-ordered property needed for
136+
// comparisons in binary searches. This should be fixed at some point by
137+
// defining a custom comparator with an arbitrary but stable ordering of
138+
// pthread_t, but it isn't too important since none of our supported platforms
139+
// implement pthread_t as a non-ordinal type.
133140
std::map<threadid_t, DebugStack*> g_debug_stacks;
134141
Mutex g_debug_stacks_mutex;
135142

@@ -158,7 +165,7 @@ void debug_stacks_print()
158165

159166
DebugStacker::DebugStacker(const char *text)
160167
{
161-
threadid_t threadid = get_current_thread_id();
168+
threadid_t threadid = thr_get_current_thread_id();
162169

163170
MutexAutoLock lock(g_debug_stacks_mutex);
164171

Diff for: ‎src/itemdef.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class CItemDefManager: public IWritableItemDefManager
241241
{
242242

243243
#ifndef SERVER
244-
m_main_thread = get_current_thread_id();
244+
m_main_thread = thr_get_current_thread_id();
245245
#endif
246246
clear();
247247
}
@@ -317,7 +317,7 @@ class CItemDefManager: public IWritableItemDefManager
317317
<<name<<"\""<<std::endl;
318318

319319
// This is not thread-safe
320-
sanity_check(get_current_thread_id() == m_main_thread);
320+
sanity_check(thr_is_current_thread(m_main_thread));
321321

322322
// Skip if already in cache
323323
ClientCached *cc = NULL;
@@ -448,7 +448,7 @@ class CItemDefManager: public IWritableItemDefManager
448448
if(cc)
449449
return cc;
450450

451-
if(get_current_thread_id() == m_main_thread)
451+
if(thr_is_current_thread(m_main_thread))
452452
{
453453
return createClientCachedDirect(name, gamedef);
454454
}

Diff for: ‎src/log.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ void Logger::setLevelSilenced(LogLevel lev, bool silenced)
184184

185185
void Logger::registerThread(const std::string &name)
186186
{
187-
threadid_t id = get_current_thread_id();
187+
threadid_t id = thr_get_current_thread_id();
188188
MutexAutoLock lock(m_mutex);
189189
m_thread_names[id] = name;
190190
}
191191

192192
void Logger::deregisterThread()
193193
{
194-
threadid_t id = get_current_thread_id();
194+
threadid_t id = thr_get_current_thread_id();
195195
MutexAutoLock lock(m_mutex);
196196
m_thread_names.erase(id);
197197
}
@@ -215,7 +215,7 @@ const std::string Logger::getThreadName()
215215
{
216216
std::map<threadid_t, std::string>::const_iterator it;
217217

218-
threadid_t id = get_current_thread_id();
218+
threadid_t id = thr_get_current_thread_id();
219219
it = m_thread_names.find(id);
220220
if (it != m_thread_names.end())
221221
return it->second;

Diff for: ‎src/shader.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ ShaderSource::ShaderSource(IrrlichtDevice *device):
367367

368368
m_shader_callback = new ShaderCallback(this, "default");
369369

370-
m_main_thread = get_current_thread_id();
370+
m_main_thread = thr_get_current_thread_id();
371371

372372
// Add a dummy ShaderInfo as the first index, named ""
373373
m_shaderinfo_cache.push_back(ShaderInfo());
@@ -397,7 +397,7 @@ u32 ShaderSource::getShader(const std::string &name,
397397
Get shader
398398
*/
399399

400-
if(get_current_thread_id() == m_main_thread){
400+
if (thr_is_current_thread(m_main_thread)) {
401401
return getShaderIdDirect(name, material_type, drawtype);
402402
} else {
403403
/*errorstream<<"getShader(): Queued: name=\""<<name<<"\""<<std::endl;*/
@@ -456,7 +456,7 @@ u32 ShaderSource::getShaderIdDirect(const std::string &name,
456456
/*
457457
Calling only allowed from main thread
458458
*/
459-
if(get_current_thread_id() != m_main_thread){
459+
if (!thr_is_current_thread(m_main_thread)) {
460460
errorstream<<"ShaderSource::getShaderIdDirect() "
461461
"called not from main thread"<<std::endl;
462462
return 0;
@@ -504,7 +504,7 @@ void ShaderSource::insertSourceShader(const std::string &name_of_shader,
504504
"name_of_shader=\""<<name_of_shader<<"\", "
505505
"filename=\""<<filename<<"\""<<std::endl;*/
506506

507-
sanity_check(get_current_thread_id() == m_main_thread);
507+
sanity_check(thr_is_current_thread(m_main_thread));
508508

509509
m_sourcecache.insert(name_of_shader, filename, program, true);
510510
}

Diff for: ‎src/threads.h

+60-7
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,75 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#ifndef THREADS_HEADER
2121
#define THREADS_HEADER
2222

23+
//
24+
// Determine which threading API we will use
25+
//
26+
#if __cplusplus >= 201103L
27+
#define USE_CPP11_THREADS 1
28+
#elif defined(_WIN32)
29+
#define USE_WIN_THREADS 1
30+
#else
31+
#define USE_POSIX_THREADS 1
32+
#endif
33+
34+
///////////////
35+
36+
37+
#if USE_CPP11_THREADS
38+
#include <thread>
39+
#endif
40+
2341
#include "threading/mutex.h"
2442

25-
#if defined(WIN32) || defined(_WIN32_WCE)
26-
typedef DWORD threadid_t;
27-
#else
28-
typedef pthread_t threadid_t;
43+
//
44+
// threadid_t, threadhandle_t
45+
//
46+
#if USE_CPP11_THREADS
47+
typedef std::thread::id threadid_t;
48+
typedef std::thread::native_handle_type threadhandle_t;
49+
#elif USE_WIN_THREADS
50+
typedef DWORD threadid_t;
51+
typedef HANDLE threadhandle_t;
52+
#elif USE_POSIX_THREADS
53+
typedef pthread_t threadid_t;
54+
typedef pthread_t threadhandle_t;
55+
#endif
56+
57+
//
58+
// ThreadStartFunc
59+
//
60+
#if USE_CPP11_THREADS || USE_POSIX_THREADS
61+
typedef void *(ThreadStartFunc)(void *param);
62+
#elif defined(_WIN32_WCE)
63+
typedef DWORD (ThreadStartFunc)(LPVOID param);
64+
#elif defined(_WIN32)
65+
typedef DWORD WINAPI (ThreadStartFunc)(LPVOID param);
2966
#endif
3067

31-
inline threadid_t get_current_thread_id()
68+
69+
inline threadid_t thr_get_current_thread_id()
3270
{
33-
#if defined(WIN32) || defined(_WIN32_WCE)
71+
#if USE_CPP11_THREADS
72+
return std::this_thread::get_id();
73+
#elif USE_WIN_THREADS
3474
return GetCurrentThreadId();
35-
#else
75+
#elif USE_POSIX_THREADS
3676
return pthread_self();
3777
#endif
3878
}
3979

80+
inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2)
81+
{
82+
#if USE_POSIX_THREADS
83+
return pthread_equal(thr1, thr2);
84+
#else
85+
return thr1 == thr2;
4086
#endif
87+
}
4188

89+
inline bool thr_is_current_thread(threadid_t thr)
90+
{
91+
return thr_compare_thread_id(thr_get_current_thread_id(), thr);
92+
}
93+
94+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.