| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkTypes.h" | 8 #include "SkTypes.h" |
| 9 | 9 |
| 10 #include "SkThreadUtils.h" | 10 #include "SkThreadUtils.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 if (nullptr == winData->fCancelEvent) { | 47 if (nullptr == winData->fCancelEvent) { |
| 48 return; | 48 return; |
| 49 } | 49 } |
| 50 | 50 |
| 51 winData->fHandle = CreateThread( | 51 winData->fHandle = CreateThread( |
| 52 nullptr, // default security attributes | 52 nullptr, // default security attributes |
| 53 0, // use default stack size | 53 0, // use default stack size |
| 54 thread_start, // thread function name (proxy) | 54 thread_start, // thread function name (proxy) |
| 55 winData, // argument to thread function (proxy args) | 55 winData, // argument to thread function (proxy args) |
| 56 CREATE_SUSPENDED, // create suspended so affinity can be set | 56 CREATE_SUSPENDED, // we used to set processor affinity, which need
ed this |
| 57 &winData->fThreadId); // returns the thread identifier | 57 &winData->fThreadId); // returns the thread identifier |
| 58 } | 58 } |
| 59 | 59 |
| 60 SkThread::~SkThread() { | 60 SkThread::~SkThread() { |
| 61 if (fData != nullptr) { | 61 if (fData != nullptr) { |
| 62 SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData); | 62 SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData); |
| 63 // If created thread but start was never called, kill the thread. | 63 // If created thread but start was never called, kill the thread. |
| 64 if (winData->fHandle != nullptr && !winData->fStarted) { | 64 if (winData->fHandle != nullptr && !winData->fStarted) { |
| 65 if (SetEvent(winData->fCancelEvent) != 0) { | 65 if (SetEvent(winData->fCancelEvent) != 0) { |
| 66 if (this->start()) { | 66 if (this->start()) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 89 } | 89 } |
| 90 | 90 |
| 91 void SkThread::join() { | 91 void SkThread::join() { |
| 92 SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData); | 92 SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData); |
| 93 if (nullptr == winData->fHandle || !winData->fStarted) { | 93 if (nullptr == winData->fHandle || !winData->fStarted) { |
| 94 return; | 94 return; |
| 95 } | 95 } |
| 96 | 96 |
| 97 WaitForSingleObject(winData->fHandle, INFINITE); | 97 WaitForSingleObject(winData->fHandle, INFINITE); |
| 98 } | 98 } |
| 99 | |
| 100 static unsigned int num_bits_set(DWORD_PTR mask) { | |
| 101 unsigned int count; | |
| 102 for (count = 0; mask; ++count) { | |
| 103 mask &= mask - 1; | |
| 104 } | |
| 105 return count; | |
| 106 } | |
| 107 | |
| 108 static unsigned int nth_set_bit(unsigned int n, DWORD_PTR mask) { | |
| 109 n %= num_bits_set(mask); | |
| 110 for (unsigned int setBitsSeen = 0, currentBit = 0; true; ++currentBit) { | |
| 111 if (mask & (static_cast<DWORD_PTR>(1) << currentBit)) { | |
| 112 ++setBitsSeen; | |
| 113 if (setBitsSeen > n) { | |
| 114 return currentBit; | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 bool SkThread::setProcessorAffinity(unsigned int processor) { | |
| 121 SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData); | |
| 122 if (nullptr == winData->fHandle) { | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 DWORD_PTR processAffinityMask; | |
| 127 DWORD_PTR systemAffinityMask; | |
| 128 if (0 == GetProcessAffinityMask(GetCurrentProcess(), | |
| 129 &processAffinityMask, | |
| 130 &systemAffinityMask)) { | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 DWORD_PTR threadAffinityMask = 1 << nth_set_bit(processor, processAffinityMa
sk); | |
| 135 return 0 != SetThreadAffinityMask(winData->fHandle, threadAffinityMask); | |
| 136 } | |
| OLD | NEW |