2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-drm-worker"
24 #include <sys/resource.h>
25 #include <sys/signal.h>
28 #include <cutils/log.h>
32 static const int64_t kBillion = 1000000000LL;
34 Worker::Worker(const char *name, int priority)
35 : name_(name), priority_(priority), exit_(false), initialized_(false) {
42 pthread_kill(thread_, SIGTERM);
43 pthread_cond_destroy(&cond_);
44 pthread_mutex_destroy(&lock_);
47 int Worker::InitWorker() {
48 pthread_condattr_t cond_attr;
49 pthread_condattr_init(&cond_attr);
50 pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
51 int ret = pthread_cond_init(&cond_, &cond_attr);
53 ALOGE("Failed to int thread %s condition %d", name_.c_str(), ret);
57 ret = pthread_mutex_init(&lock_, NULL);
59 ALOGE("Failed to init thread %s lock %d", name_.c_str(), ret);
60 pthread_cond_destroy(&cond_);
64 ret = pthread_create(&thread_, NULL, InternalRoutine, this);
66 ALOGE("Could not create thread %s %d", name_.c_str(), ret);
67 pthread_mutex_destroy(&lock_);
68 pthread_cond_destroy(&cond_);
75 bool Worker::initialized() const {
80 return pthread_mutex_lock(&lock_);
83 int Worker::Unlock() {
84 return pthread_mutex_unlock(&lock_);
87 int Worker::SignalLocked() {
88 return SignalThreadLocked(false);
91 int Worker::ExitLocked() {
92 int signal_ret = SignalThreadLocked(true);
94 ALOGE("Failed to signal thread %s with exit %d", name_.c_str(), signal_ret);
96 int join_ret = pthread_join(thread_, NULL);
97 if (join_ret && join_ret != ESRCH)
98 ALOGE("Failed to join thread %s in exit %d", name_.c_str(), join_ret);
100 return signal_ret | join_ret;
103 int Worker::Signal() {
106 ALOGE("Failed to acquire lock in Signal() %d\n", ret);
110 int signal_ret = SignalLocked();
114 ALOGE("Failed to release lock in Signal() %d\n", ret);
123 ALOGE("Failed to acquire lock in Exit() %d\n", ret);
127 int exit_ret = ExitLocked();
131 ALOGE("Failed to release lock in Exit() %d\n", ret);
137 int Worker::WaitForSignalOrExitLocked(int64_t max_nanoseconds) {
142 if (max_nanoseconds < 0) {
143 ret = pthread_cond_wait(&cond_, &lock_);
145 struct timespec abs_deadline;
146 ret = clock_gettime(CLOCK_MONOTONIC, &abs_deadline);
149 int64_t nanos = (int64_t)abs_deadline.tv_nsec + max_nanoseconds;
150 abs_deadline.tv_sec += nanos / kBillion;
151 abs_deadline.tv_nsec = nanos % kBillion;
152 ret = pthread_cond_timedwait(&cond_, &lock_, &abs_deadline);
153 if (ret == ETIMEDOUT)
164 void *Worker::InternalRoutine(void *arg) {
165 Worker *worker = (Worker *)arg;
167 setpriority(PRIO_PROCESS, 0, worker->priority_);
170 int ret = worker->Lock();
172 ALOGE("Failed to lock %s thread %d", worker->name_.c_str(), ret);
176 bool exit = worker->exit_;
178 ret = worker->Unlock();
180 ALOGE("Failed to unlock %s thread %d", worker->name_.c_str(), ret);
191 int Worker::SignalThreadLocked(bool exit) {
195 int ret = pthread_cond_signal(&cond_);
197 ALOGE("Failed to signal condition on %s thread %d", name_.c_str(), ret);