Thread.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <pthread.h>
  17. #include "Action.h"
  18. #include "Thread.h"
  19. Thread::Thread() {
  20. pthread_cond_init(&cond_, nullptr);
  21. }
  22. Thread::~Thread() {
  23. pthread_cond_destroy(&cond_);
  24. }
  25. void Thread::WaitForReady() {
  26. pthread_mutex_lock(&mutex_);
  27. while (pending_) {
  28. pthread_cond_wait(&cond_, &mutex_);
  29. }
  30. pthread_mutex_unlock(&mutex_);
  31. }
  32. void Thread::WaitForPending() {
  33. pthread_mutex_lock(&mutex_);
  34. while (!pending_) {
  35. pthread_cond_wait(&cond_, &mutex_);
  36. }
  37. pthread_mutex_unlock(&mutex_);
  38. }
  39. void Thread::SetPending() {
  40. pthread_mutex_lock(&mutex_);
  41. pending_ = true;
  42. pthread_mutex_unlock(&mutex_);
  43. pthread_cond_signal(&cond_);
  44. }
  45. void Thread::ClearPending() {
  46. pthread_mutex_lock(&mutex_);
  47. pending_ = false;
  48. pthread_mutex_unlock(&mutex_);
  49. pthread_cond_signal(&cond_);
  50. }
  51. Action* Thread::CreateAction(uintptr_t key_pointer, const char* type, const char* line) {
  52. return Action::CreateAction(key_pointer, type, line, action_memory_);
  53. }