OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 Eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 #ifndef WIN32 | |
19 #include <unistd.h> | |
20 #endif | |
21 | |
22 #include "Thread.h" | |
23 | |
24 using namespace AdblockPlus; | |
25 | |
26 void AdblockPlus::Sleep(const int millis) | |
27 { | |
28 #ifdef WIN32 | |
29 ::Sleep(millis); | |
30 #else | |
31 usleep(millis * 1000); | |
32 #endif | |
33 } | |
34 | |
35 Mutex::Mutex() | |
36 { | |
37 #ifdef WIN32 | |
38 InitializeCriticalSection(&nativeMutex); | |
39 #else | |
40 pthread_mutex_init(&nativeMutex, 0); | |
41 #endif | |
42 } | |
43 | |
44 Mutex::~Mutex() | |
45 { | |
46 #ifdef WIN32 | |
47 DeleteCriticalSection(&nativeMutex); | |
48 #else | |
49 pthread_mutex_destroy(&nativeMutex); | |
50 #endif | |
51 } | |
52 | |
53 void Mutex::Lock() | |
54 { | |
55 #ifdef WIN32 | |
56 EnterCriticalSection(&nativeMutex); | |
57 #else | |
58 pthread_mutex_lock(&nativeMutex); | |
59 #endif | |
60 } | |
61 | |
62 void Mutex::Unlock() | |
63 { | |
64 #ifdef WIN32 | |
65 LeaveCriticalSection(&nativeMutex); | |
66 #else | |
67 pthread_mutex_unlock(&nativeMutex); | |
68 #endif | |
69 } | |
70 | |
71 Lock::Lock(Mutex& mutex) : mutex(mutex) | |
72 { | |
73 mutex.Lock(); | |
74 } | |
75 | |
76 Lock::~Lock() | |
77 { | |
78 mutex.Unlock(); | |
79 } | |
80 | |
81 Thread::Thread(bool deleteSelfOnFinish) | |
82 : m_deleteSelfOnFinish(deleteSelfOnFinish) | |
83 { | |
84 } | |
85 | |
86 Thread::~Thread() | |
87 { | |
88 } | |
89 | |
90 void Thread::Start() | |
91 { | |
92 #ifdef WIN32 | |
93 nativeThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&CallRun, this, 0, 0
); | |
94 #else | |
95 pthread_create(&nativeThread, 0, (void* (*)(void*)) &CallRun, this); | |
96 #endif | |
97 } | |
98 | |
99 void Thread::Join() | |
100 { | |
101 #ifdef WIN32 | |
102 WaitForSingleObject(nativeThread, INFINITE); | |
103 #else | |
104 pthread_join(nativeThread, 0); | |
105 #endif | |
106 } | |
107 | |
108 void Thread::CallRun(Thread* thread) | |
109 { | |
110 thread->Run(); | |
111 if (thread->m_deleteSelfOnFinish) | |
112 delete thread; | |
113 } | |
OLD | NEW |