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

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

Issue 1947153002: Modernize SkMutex and SkSemaphore. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: this time no destructors Created 4 years, 7 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 | « include/private/SkSemaphore.h ('k') | no next file » | 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 "../private/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)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 /////////////////////////////////////////////////////////////////////////////// 58 ///////////////////////////////////////////////////////////////////////////////
59 59
60 void SkBaseSemaphore::signal(int n) { 60 void SkBaseSemaphore::osSignal(int n) {
61 SkASSERT(n >= 0); 61 fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
62 62 fOSSemaphore->signal(n);
63 // We only want to call the OS semaphore when our logical count crosses
64 // from <= 0 to >0 (when we need to wake sleeping threads).
65 //
66 // This is easiest to think about with specific examples of prev and n.
67 // If n == 5 and prev == -3, there are 3 threads sleeping and we signal
68 // SkTMin(-(-3), 5) == 3 times on the OS semaphore, leaving the count at 2.
69 //
70 // If prev >= 0, no threads are waiting, SkTMin(-prev, n) is always <= 0,
71 // so we don't call the OS semaphore, leaving the count at (prev + n).
72 int prev = sk_atomic_fetch_add(&fCount, n, sk_memory_order_release);
73 int toSignal = SkTMin(-prev, n);
74 if (toSignal > 0) {
75 this->osSignal(toSignal);
76 }
77 } 63 }
78 64
79 static SkBaseSemaphore::OSSemaphore* semaphore(SkBaseSemaphore* semaphore) { 65 void SkBaseSemaphore::osWait() {
80 return semaphore->fOSSemaphore.get([](){ return new SkBaseSemaphore::OSSemap hore(); }); 66 fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
67 fOSSemaphore->wait();
81 } 68 }
82 69
83 void SkBaseSemaphore::osSignal(int n) { semaphore(this)->signal(n); } 70 void SkBaseSemaphore::cleanup() {
84 71 delete fOSSemaphore;
85 void SkBaseSemaphore::osWait() { semaphore(this)->wait(); }
86
87 void SkBaseSemaphore::deleteSemaphore() {
88 delete (OSSemaphore*) fOSSemaphore;
89 } 72 }
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 | « include/private/SkSemaphore.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698