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

Side by Side Diff: src/Thread.cpp

Issue 10105002: Add Sleep and Lock utilities (Closed)
Patch Set: Created April 4, 2013, 5:34 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
« no previous file with comments | « src/Thread.h ('k') | test/Thread.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #ifndef WIN32
2 #include <unistd.h>
3 #endif
4
1 #include "Thread.h" 5 #include "Thread.h"
2 6
3 using namespace AdblockPlus; 7 using namespace AdblockPlus;
4 8
5 namespace 9 namespace
6 { 10 {
7 void CallRun(Thread* thread) 11 void CallRun(Thread* thread)
8 { 12 {
9 thread->Run(); 13 thread->Run();
10 } 14 }
11 } 15 }
12 16
17 void AdblockPlus::Sleep(const int millis)
18 {
19 #ifdef WIN32
20 ::Sleep(millis);
21 #else
22 usleep(millis * 1000);
23 #endif
24 }
25
13 Mutex::Mutex() 26 Mutex::Mutex()
14 { 27 {
15 #ifdef WIN32 28 #ifdef WIN32
16 InitializeCriticalSection(&nativeMutex); 29 InitializeCriticalSection(&nativeMutex);
17 #else 30 #else
18 pthread_mutex_init(&nativeMutex, 0); 31 pthread_mutex_init(&nativeMutex, 0);
19 #endif 32 #endif
20 } 33 }
21 34
22 Mutex::~Mutex() 35 Mutex::~Mutex()
(...skipping 16 matching lines...) Expand all
39 52
40 void Mutex::Unlock() 53 void Mutex::Unlock()
41 { 54 {
42 #ifdef WIN32 55 #ifdef WIN32
43 LeaveCriticalSection(&nativeMutex); 56 LeaveCriticalSection(&nativeMutex);
44 #else 57 #else
45 pthread_mutex_unlock(&nativeMutex); 58 pthread_mutex_unlock(&nativeMutex);
46 #endif 59 #endif
47 } 60 }
48 61
62 Lock::Lock(Mutex& mutex) : mutex(mutex)
63 {
64 mutex.Lock();
65 }
66
67 Lock::~Lock()
68 {
69 mutex.Unlock();
70 }
71
49 ConditionVariable::ConditionVariable() 72 ConditionVariable::ConditionVariable()
50 { 73 {
51 #ifdef WIN32 74 #ifdef WIN32
52 InitializeConditionVariable(&nativeCondition); 75 InitializeConditionVariable(&nativeCondition);
53 #else 76 #else
54 pthread_cond_init(&nativeCondition, 0); 77 pthread_cond_init(&nativeCondition, 0);
55 #endif 78 #endif
56 } 79 }
57 80
58 ConditionVariable::~ConditionVariable() 81 ConditionVariable::~ConditionVariable()
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 } 117 }
95 118
96 void Thread::Join() 119 void Thread::Join()
97 { 120 {
98 #ifdef WIN32 121 #ifdef WIN32
99 WaitForSingleObject(nativeThread, INFINITE); 122 WaitForSingleObject(nativeThread, INFINITE);
100 #else 123 #else
101 pthread_join(nativeThread, 0); 124 pthread_join(nativeThread, 0);
102 #endif 125 #endif
103 } 126 }
OLDNEW
« no previous file with comments | « src/Thread.h ('k') | test/Thread.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld