rsSignal.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2009 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 "rsSignal.h"
  17. #include <errno.h>
  18. namespace android {
  19. namespace renderscript {
  20. Signal::Signal() {
  21. mSet = true;
  22. }
  23. Signal::~Signal() {
  24. pthread_mutex_destroy(&mMutex);
  25. pthread_cond_destroy(&mCondition);
  26. }
  27. bool Signal::init() {
  28. int status = pthread_mutex_init(&mMutex, nullptr);
  29. if (status) {
  30. ALOGE("Signal::init: mutex init failure: %s", strerror(status));
  31. return false;
  32. }
  33. status = pthread_cond_init(&mCondition, nullptr);
  34. if (status) {
  35. ALOGE("Signal::init: condition init failure: %s", strerror(status));
  36. pthread_mutex_destroy(&mMutex);
  37. return false;
  38. }
  39. return true;
  40. }
  41. void Signal::set() {
  42. int status = pthread_mutex_lock(&mMutex);
  43. if (status) {
  44. ALOGE("Signal::set: error locking for set condition: %s", strerror(status));
  45. return;
  46. }
  47. mSet = true;
  48. status = pthread_cond_signal(&mCondition);
  49. if (status) {
  50. ALOGE("Signal::set: error on set condition: %s", strerror(status));
  51. }
  52. status = pthread_mutex_unlock(&mMutex);
  53. if (status) {
  54. ALOGE("Signal::set: error unlocking for set condition: %s", strerror(status));
  55. }
  56. }
  57. void Signal::wait() {
  58. int status = pthread_mutex_lock(&mMutex);
  59. if (status) {
  60. ALOGE("Signal::wait: error locking for condition: %s", strerror(status));
  61. return;
  62. }
  63. if (!mSet) {
  64. status = pthread_cond_wait(&mCondition, &mMutex);
  65. }
  66. if (!status) {
  67. mSet = false;
  68. } else {
  69. ALOGE("Signal::wait: error waiting for condition: %s", strerror(status));
  70. }
  71. status = pthread_mutex_unlock(&mMutex);
  72. if (status) {
  73. ALOGE("Signal::wait: error unlocking for condition: %s", strerror(status));
  74. }
  75. }
  76. } // namespace renderscript
  77. } // namespace android