Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/Thread.cpp

Issue 10026001: Cross-platform thread primitives (Closed)
Patch Set: Created March 29, 2013, 5:26 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include "Thread.h"
2
3 using namespace AdblockPlus;
4
5 namespace
6 {
7 void CallRun(Thread* thread)
8 {
9 thread->Run();
10 }
11 }
12
13 Thread::Mutex::Mutex()
14 {
15 #ifdef WIN32
16 InitializeCriticalSection(&nativeMutex);
17 #else
18 pthread_mutex_init(&nativeMutex, 0);
19 #endif
20 }
21
22 Thread::Mutex::~Mutex()
23 {
24 Unlock();
Wladimir Palant 2013/04/03 13:14:47 That's a race condition - you unlock the mutex so
Felix Dahlke 2013/04/03 16:27:59 Both DeleteCriticalSection and pthread_mutex_destr
Wladimir Palant 2013/04/03 19:30:16 To me it sounds like AutoLock is a better solution
Felix Dahlke 2013/04/04 02:52:58 Yeah, you're probably right. I was planning to wra
25 #ifdef WIN32
26 DeleteCriticalSection(&nativeMutex);
27 #else
28 pthread_mutex_destroy(&nativeMutex);
29 #endif
30 }
31
32 void Thread::Mutex::Lock()
33 {
34 #ifdef WIN32
35 EnterCriticalSection(&nativeMutex);
36 #else
37 pthread_mutex_lock(&nativeMutex);
38 #endif
39 }
40
41 void Thread::Mutex::Unlock()
42 {
43 #ifdef WIN32
44 LeaveCriticalSection(&nativeMutex);
45 #else
46 pthread_mutex_unlock(&nativeMutex);
47 #endif
48 }
Wladimir Palant 2013/04/03 13:14:47 I would be in favor of an AutoLock helper class th
Felix Dahlke 2013/04/03 16:27:59 Agreed, that's neat. I'll add one later with a new
49
50 Thread::Condition::Condition()
51 {
52 #ifdef WIN32
53 InitializeConditionVariable(&nativeCondition);
54 #else
55 pthread_cond_init(&nativeCondition, 0);
56 #endif
57 }
58
59 Thread::Condition::~Condition()
60 {
61 #ifndef WIN32
62 Signal();
Wladimir Palant 2013/04/03 13:14:47 This is a race condition again. Why would we want
Felix Dahlke 2013/04/03 16:27:59 As above, to avoid undefined behaviour.
Wladimir Palant 2013/04/03 19:30:16 I think that not destroying conditions that are st
Felix Dahlke 2013/04/04 02:52:58 Yeah, same as above, removed the Signal() call.
63 pthread_cond_destroy(&nativeCondition);
64 #endif
65 }
66
67 void Thread::Condition::Wait(Thread::Mutex& mutex)
68 {
69 #ifdef WIN32
70 SleepConditionVariableCS(&nativeCondition, &mutex.nativeMutex, INFINITE);
71 #else
72 pthread_cond_wait(&nativeCondition, &mutex.nativeMutex);
73 #endif
74 }
75
76 void Thread::Condition::Signal()
77 {
78 #ifdef WIN32
79 WakeConditionVariable(&nativeCondition);
80 #else
81 pthread_cond_signal(&nativeCondition);
82 #endif
83 }
84
85 Thread::~Thread()
86 {
87 }
88
89 void Thread::Start()
90 {
91 #ifdef WIN32
92 CreateThread(0, 0, &CallRun, 0, this, &thread);
Wladimir Palant 2013/04/03 13:14:47 Wrong order of parameters? |this| should passed as
Felix Dahlke 2013/04/03 16:27:59 Yep, I somehow mixed that up and didn't manage to
93 #else
94 pthread_create(&thread, 0, (void *(*)(void*)) &CallRun, this);
95 #endif
96 }
97
98 void Thread::Join()
99 {
100 #ifdef WIN32
101 WaitForSingleObject(thread, INFINITE);
102 #else
103 pthread_join(thread, 0);
104 #endif
105 }
OLDNEW

Powered by Google App Engine
This is Rietveld