blob: 3afe4883cf46cbf06e8a4b68d52af50955829e48 [file] [log] [blame]
Zach Reizner49446bc2015-11-13 16:09:39 -08001/*
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
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-auto-lock"
19
20#include "autolock.h"
21
Zach Reizner49446bc2015-11-13 16:09:39 -080022#include <pthread.h>
23
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020024#include <cerrno>
Zach Reizner49446bc2015-11-13 16:09:39 -080025
Roman Stratiienkod21071f2021-03-09 21:56:50 +020026#include "utils/log.h"
27
Zach Reizner49446bc2015-11-13 16:09:39 -080028namespace android {
29
30int AutoLock::Lock() {
31 if (locked_) {
32 ALOGE("Invalid attempt to double lock AutoLock %s", name_);
33 return -EINVAL;
34 }
35 int ret = pthread_mutex_lock(mutex_);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020036 if (ret != 0) {
Zach Reizner49446bc2015-11-13 16:09:39 -080037 ALOGE("Failed to acquire %s lock %d", name_, ret);
38 return ret;
39 }
40 locked_ = true;
41 return 0;
42}
43
44int AutoLock::Unlock() {
45 if (!locked_) {
46 ALOGE("Invalid attempt to unlock unlocked AutoLock %s", name_);
47 return -EINVAL;
48 }
49 int ret = pthread_mutex_unlock(mutex_);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020050 if (ret != 0) {
Zach Reizner49446bc2015-11-13 16:09:39 -080051 ALOGE("Failed to release %s lock %d", name_, ret);
52 return ret;
53 }
54 locked_ = false;
55 return 0;
56}
Sean Paulf72cccd2018-08-27 13:59:08 -040057} // namespace android