| 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" |
| 11 #include "SkThreadUtils_pthread.h" | 11 #include "SkThreadUtils_pthread.h" |
| 12 | 12 |
| 13 #include <pthread.h> | 13 #include <pthread.h> |
| 14 #include <signal.h> | 14 #include <signal.h> |
| 15 | 15 |
| 16 PThreadEvent::PThreadEvent() : fConditionFlag(false) { | 16 PThreadEvent::PThreadEvent() : fConditionFlag(false) { |
| 17 pthread_cond_init(&fCondition, NULL); | 17 pthread_cond_init(&fCondition, nullptr); |
| 18 pthread_mutex_init(&fConditionMutex, NULL); | 18 pthread_mutex_init(&fConditionMutex, nullptr); |
| 19 } | 19 } |
| 20 PThreadEvent::~PThreadEvent() { | 20 PThreadEvent::~PThreadEvent() { |
| 21 pthread_mutex_destroy(&fConditionMutex); | 21 pthread_mutex_destroy(&fConditionMutex); |
| 22 pthread_cond_destroy(&fCondition); | 22 pthread_cond_destroy(&fCondition); |
| 23 } | 23 } |
| 24 void PThreadEvent::trigger() { | 24 void PThreadEvent::trigger() { |
| 25 pthread_mutex_lock(&fConditionMutex); | 25 pthread_mutex_lock(&fConditionMutex); |
| 26 fConditionFlag = true; | 26 fConditionFlag = true; |
| 27 pthread_cond_signal(&fCondition); | 27 pthread_cond_signal(&fCondition); |
| 28 pthread_mutex_unlock(&fConditionMutex); | 28 pthread_mutex_unlock(&fConditionMutex); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 58 | 58 |
| 59 static void* thread_start(void* arg) { | 59 static void* thread_start(void* arg) { |
| 60 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(arg); | 60 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(arg); |
| 61 // Wait for start signal | 61 // Wait for start signal |
| 62 pthreadData->fStarted.wait(); | 62 pthreadData->fStarted.wait(); |
| 63 | 63 |
| 64 // Call entry point only if thread was not canceled before starting. | 64 // Call entry point only if thread was not canceled before starting. |
| 65 if (!pthreadData->fCanceled.isTriggered()) { | 65 if (!pthreadData->fCanceled.isTriggered()) { |
| 66 pthreadData->fEntryPoint(pthreadData->fParam); | 66 pthreadData->fEntryPoint(pthreadData->fParam); |
| 67 } | 67 } |
| 68 return NULL; | 68 return nullptr; |
| 69 } | 69 } |
| 70 | 70 |
| 71 SkThread::SkThread(entryPointProc entryPoint, void* data) { | 71 SkThread::SkThread(entryPointProc entryPoint, void* data) { |
| 72 SkThread_PThreadData* pthreadData = new SkThread_PThreadData(entryPoint, dat
a); | 72 SkThread_PThreadData* pthreadData = new SkThread_PThreadData(entryPoint, dat
a); |
| 73 fData = pthreadData; | 73 fData = pthreadData; |
| 74 | 74 |
| 75 int ret = pthread_create(&(pthreadData->fPThread), | 75 int ret = pthread_create(&(pthreadData->fPThread), |
| 76 &(pthreadData->fAttr), | 76 &(pthreadData->fAttr), |
| 77 thread_start, | 77 thread_start, |
| 78 pthreadData); | 78 pthreadData); |
| 79 | 79 |
| 80 pthreadData->fValidPThread = (0 == ret); | 80 pthreadData->fValidPThread = (0 == ret); |
| 81 } | 81 } |
| 82 | 82 |
| 83 SkThread::~SkThread() { | 83 SkThread::~SkThread() { |
| 84 if (fData != NULL) { | 84 if (fData != nullptr) { |
| 85 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(f
Data); | 85 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(f
Data); |
| 86 // If created thread but start was never called, kill the thread. | 86 // If created thread but start was never called, kill the thread. |
| 87 if (pthreadData->fValidPThread && !pthreadData->fStarted.isTriggered())
{ | 87 if (pthreadData->fValidPThread && !pthreadData->fStarted.isTriggered())
{ |
| 88 pthreadData->fCanceled.trigger(); | 88 pthreadData->fCanceled.trigger(); |
| 89 if (this->start()) { | 89 if (this->start()) { |
| 90 this->join(); | 90 this->join(); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 delete pthreadData; | 93 delete pthreadData; |
| 94 } | 94 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 106 pthreadData->fStarted.trigger(); | 106 pthreadData->fStarted.trigger(); |
| 107 return true; | 107 return true; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void SkThread::join() { | 110 void SkThread::join() { |
| 111 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(fData
); | 111 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(fData
); |
| 112 if (!pthreadData->fValidPThread || !pthreadData->fStarted.isTriggered()) { | 112 if (!pthreadData->fValidPThread || !pthreadData->fStarted.isTriggered()) { |
| 113 return; | 113 return; |
| 114 } | 114 } |
| 115 | 115 |
| 116 pthread_join(pthreadData->fPThread, NULL); | 116 pthread_join(pthreadData->fPThread, nullptr); |
| 117 } | 117 } |
| OLD | NEW |