Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: src/core/SkSemaphore.cpp

Issue 1359733002: Make mutex semaphore based. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix mutex leak Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkSemaphore.h ('k') | src/core/SkSharedMutex.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 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkSemaphore.h" 8 #include "../private/SkSemaphore.h"
9 9
10 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) 10 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
11 #include <mach/mach.h> 11 #include <mach/mach.h>
12 struct SkSemaphore::OSSemaphore { 12 struct SkBaseSemaphore::OSSemaphore {
13 semaphore_t fSemaphore; 13 semaphore_t fSemaphore;
14 14
15 OSSemaphore() { 15 OSSemaphore() {
16 semaphore_create(mach_task_self(), &fSemaphore, SYNC_POLICY_LIFO, 0/ *initial count*/); 16 semaphore_create(mach_task_self(), &fSemaphore, SYNC_POLICY_LIFO, 0/ *initial count*/);
17 } 17 }
18 ~OSSemaphore() { semaphore_destroy(mach_task_self(), fSemaphore); } 18 ~OSSemaphore() { semaphore_destroy(mach_task_self(), fSemaphore); }
19 19
20 void signal(int n) { while (n --> 0) { semaphore_signal(fSemaphore); } } 20 void signal(int n) { while (n --> 0) { semaphore_signal(fSemaphore); } }
21 void wait() { semaphore_wait(fSemaphore); } 21 void wait() { semaphore_wait(fSemaphore); }
22 }; 22 };
23 #elif defined(SK_BUILD_FOR_WIN32) 23 #elif defined(SK_BUILD_FOR_WIN32)
24 struct SkSemaphore::OSSemaphore { 24 struct SkBaseSemaphore::OSSemaphore {
25 HANDLE fSemaphore; 25 HANDLE fSemaphore;
26 26
27 OSSemaphore() { 27 OSSemaphore() {
28 fSemaphore = CreateSemaphore(nullptr /*security attributes, optio nal*/, 28 fSemaphore = CreateSemaphore(nullptr /*security attributes, optio nal*/,
29 0 /*initial count*/, 29 0 /*initial count*/,
30 MAXLONG /*max count*/, 30 MAXLONG /*max count*/,
31 nullptr /*name, optional*/); 31 nullptr /*name, optional*/);
32 } 32 }
33 ~OSSemaphore() { CloseHandle(fSemaphore); } 33 ~OSSemaphore() { CloseHandle(fSemaphore); }
34 34
35 void signal(int n) { 35 void signal(int n) {
36 ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, opt ional*/); 36 ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, opt ional*/);
37 } 37 }
38 void wait() { WaitForSingleObject(fSemaphore, INFINITE/*timeout in ms*/) ; } 38 void wait() { WaitForSingleObject(fSemaphore, INFINITE/*timeout in ms*/) ; }
39 }; 39 };
40 #else 40 #else
41 // It's important we test for Mach before this. This code will compile but not work there. 41 // It's important we test for Mach before this. This code will compile but not work there.
42 #include <errno.h> 42 #include <errno.h>
43 #include <semaphore.h> 43 #include <semaphore.h>
44 struct SkSemaphore::OSSemaphore { 44 struct SkBaseSemaphore::OSSemaphore {
45 sem_t fSemaphore; 45 sem_t fSemaphore;
46 46
47 OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial c ount*/); } 47 OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial c ount*/); }
48 ~OSSemaphore() { sem_destroy(&fSemaphore); } 48 ~OSSemaphore() { sem_destroy(&fSemaphore); }
49 49
50 void signal(int n) { while (n --> 0) { sem_post(&fSemaphore); } } 50 void signal(int n) { while (n --> 0) { sem_post(&fSemaphore); } }
51 void wait() { 51 void wait() {
52 // Try until we're not interrupted. 52 // Try until we're not interrupted.
53 while(sem_wait(&fSemaphore) == -1 && errno == EINTR); 53 while(sem_wait(&fSemaphore) == -1 && errno == EINTR);
54 } 54 }
55 }; 55 };
56 #endif 56 #endif
57 57
58 SkSemaphore::SkSemaphore() : fCount(0), fOSSemaphore(new OSSemaphore) {} 58 ///////////////////////////////////////////////////////////////////////////////
59 SkSemaphore::~SkSemaphore() { delete fOSSemaphore; }
60 59
61 void SkSemaphore::signal(int n) { 60 void SkBaseSemaphore::signal(int n) {
62 SkASSERT(n >= 0); 61 SkASSERT(n >= 0);
63 62
64 // We only want to call the OS semaphore when our logical count crosses 63 // We only want to call the OS semaphore when our logical count crosses
65 // from <= 0 to >0 (when we need to wake sleeping threads). 64 // from <= 0 to >0 (when we need to wake sleeping threads).
66 // 65 //
67 // This is easiest to think about with specific examples of prev and n. 66 // This is easiest to think about with specific examples of prev and n.
68 // If n == 5 and prev == -3, there are 3 threads sleeping and we signal 67 // If n == 5 and prev == -3, there are 3 threads sleeping and we signal
69 // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2. 68 // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2.
70 // 69 //
71 // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0, 70 // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0,
72 // so we don't call the OS semaphore, leaving the count at (prev + n). 71 // so we don't call the OS semaphore, leaving the count at (prev + n).
73 int prev = fCount.fetch_add(n, sk_memory_order_release); 72 int prev = sk_atomic_fetch_add(&fCount, n, sk_memory_order_release);
74 int toSignal = SkTMin(-prev, n); 73 int toSignal = SkTMin(-prev, n);
75 if (toSignal > 0) { 74 if (toSignal > 0) {
76 fOSSemaphore->signal(toSignal); 75 this->osSignal(toSignal);
77 } 76 }
78 } 77 }
79 78
80 void SkSemaphore::wait() { 79 static SkBaseSemaphore::OSSemaphore* semaphore(SkBaseSemaphore* semaphore) {
81 // We only wait() on the OS semaphore if the count drops <= 0, 80 return semaphore->fOSSemaphore.get([](){ return new SkBaseSemaphore::OSSemap hore(); });
82 // i.e. when we need to make this thread sleep to wait for it to go back up.
83 if (fCount.fetch_add(-1, sk_memory_order_acquire) <= 0) {
84 fOSSemaphore->wait();
85 }
86 } 81 }
82
83 void SkBaseSemaphore::osSignal(int n) { semaphore(this)->signal(n); }
84
85 void SkBaseSemaphore::osWait() { semaphore(this)->wait(); }
86
87 void SkBaseSemaphore::deleteSemaphore() {
88 delete (OSSemaphore*) fOSSemaphore;
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92
93 SkSemaphore::SkSemaphore(){ fBaseSemaphore = {0, {0}}; }
94
95 SkSemaphore::~SkSemaphore() { fBaseSemaphore.deleteSemaphore(); }
96
97 void SkSemaphore::wait() { fBaseSemaphore.wait(); }
98
99 void SkSemaphore::signal(int n) {fBaseSemaphore.signal(n); }
OLDNEW
« no previous file with comments | « src/core/SkSemaphore.h ('k') | src/core/SkSharedMutex.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698