OLD | NEW |
1 #include "Thread.h" | 1 #include "Thread.h" |
2 | 2 |
3 using namespace AdblockPlus; | 3 using namespace AdblockPlus; |
4 | 4 |
5 namespace | 5 namespace |
6 { | 6 { |
7 void CallRun(Thread* thread) | 7 void CallRun(Thread* thread) |
8 { | 8 { |
9 thread->Run(); | 9 thread->Run(); |
10 } | 10 } |
11 } | 11 } |
12 | 12 |
13 Thread::Mutex::Mutex() | 13 Mutex::Mutex() |
14 { | 14 { |
15 #ifdef WIN32 | 15 #ifdef WIN32 |
16 InitializeCriticalSection(&nativeMutex); | 16 InitializeCriticalSection(&nativeMutex); |
17 #else | 17 #else |
18 pthread_mutex_init(&nativeMutex, 0); | 18 pthread_mutex_init(&nativeMutex, 0); |
19 #endif | 19 #endif |
20 } | 20 } |
21 | 21 |
22 Thread::Mutex::~Mutex() | 22 Mutex::~Mutex() |
23 { | 23 { |
24 Unlock(); | |
25 #ifdef WIN32 | 24 #ifdef WIN32 |
26 DeleteCriticalSection(&nativeMutex); | 25 DeleteCriticalSection(&nativeMutex); |
27 #else | 26 #else |
28 pthread_mutex_destroy(&nativeMutex); | 27 pthread_mutex_destroy(&nativeMutex); |
29 #endif | 28 #endif |
30 } | 29 } |
31 | 30 |
32 void Thread::Mutex::Lock() | 31 void Mutex::Lock() |
33 { | 32 { |
34 #ifdef WIN32 | 33 #ifdef WIN32 |
35 EnterCriticalSection(&nativeMutex); | 34 EnterCriticalSection(&nativeMutex); |
36 #else | 35 #else |
37 pthread_mutex_lock(&nativeMutex); | 36 pthread_mutex_lock(&nativeMutex); |
38 #endif | 37 #endif |
39 } | 38 } |
40 | 39 |
41 void Thread::Mutex::Unlock() | 40 void Mutex::Unlock() |
42 { | 41 { |
43 #ifdef WIN32 | 42 #ifdef WIN32 |
44 LeaveCriticalSection(&nativeMutex); | 43 LeaveCriticalSection(&nativeMutex); |
45 #else | 44 #else |
46 pthread_mutex_unlock(&nativeMutex); | 45 pthread_mutex_unlock(&nativeMutex); |
47 #endif | 46 #endif |
48 } | 47 } |
49 | 48 |
50 Thread::Condition::Condition() | 49 ConditionVariable::ConditionVariable() |
51 { | 50 { |
52 #ifdef WIN32 | 51 #ifdef WIN32 |
53 InitializeConditionVariable(&nativeCondition); | 52 InitializeConditionVariable(&nativeCondition); |
54 #else | 53 #else |
55 pthread_cond_init(&nativeCondition, 0); | 54 pthread_cond_init(&nativeCondition, 0); |
56 #endif | 55 #endif |
57 } | 56 } |
58 | 57 |
59 Thread::Condition::~Condition() | 58 ConditionVariable::~ConditionVariable() |
60 { | 59 { |
61 #ifndef WIN32 | 60 #ifndef WIN32 |
62 Signal(); | |
63 pthread_cond_destroy(&nativeCondition); | 61 pthread_cond_destroy(&nativeCondition); |
64 #endif | 62 #endif |
65 } | 63 } |
66 | 64 |
67 void Thread::Condition::Wait(Thread::Mutex& mutex) | 65 void ConditionVariable::Wait(Mutex& mutex) |
68 { | 66 { |
69 #ifdef WIN32 | 67 #ifdef WIN32 |
70 SleepConditionVariableCS(&nativeCondition, &mutex.nativeMutex, INFINITE); | 68 SleepConditionVariableCS(&nativeCondition, &mutex.nativeMutex, INFINITE); |
71 #else | 69 #else |
72 pthread_cond_wait(&nativeCondition, &mutex.nativeMutex); | 70 pthread_cond_wait(&nativeCondition, &mutex.nativeMutex); |
73 #endif | 71 #endif |
74 } | 72 } |
75 | 73 |
76 void Thread::Condition::Signal() | 74 void ConditionVariable::Signal() |
77 { | 75 { |
78 #ifdef WIN32 | 76 #ifdef WIN32 |
79 WakeConditionVariable(&nativeCondition); | 77 WakeConditionVariable(&nativeCondition); |
80 #else | 78 #else |
81 pthread_cond_signal(&nativeCondition); | 79 pthread_cond_signal(&nativeCondition); |
82 #endif | 80 #endif |
83 } | 81 } |
84 | 82 |
85 Thread::~Thread() | 83 Thread::~Thread() |
86 { | 84 { |
87 } | 85 } |
88 | 86 |
89 void Thread::Start() | 87 void Thread::Start() |
90 { | 88 { |
91 #ifdef WIN32 | 89 #ifdef WIN32 |
92 thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&CallRun, this, 0, 0); | 90 nativeThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&CallRun, this, 0, 0
); |
93 #else | 91 #else |
94 pthread_create(&thread, 0, (void *(*)(void*)) &CallRun, this); | 92 pthread_create(&nativeThread, 0, (void *(*)(void*)) &CallRun, this); |
95 #endif | 93 #endif |
96 } | 94 } |
97 | 95 |
98 void Thread::Join() | 96 void Thread::Join() |
99 { | 97 { |
100 #ifdef WIN32 | 98 #ifdef WIN32 |
101 WaitForSingleObject(thread, INFINITE); | 99 WaitForSingleObject(nativeThread, INFINITE); |
102 #else | 100 #else |
103 pthread_join(thread, 0); | 101 pthread_join(nativeThread, 0); |
104 #endif | 102 #endif |
105 } | 103 } |
OLD | NEW |