rsSignal.cpp revision 12b14ae9fa34f4fd0bf21a2a4ac95a4864248fe9
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
17#include "rsSignal.h"
18
19using namespace android;
20using namespace android::renderscript;
21
22
23Signal::Signal()
24{
25    mSet = true;
26}
27
28Signal::~Signal()
29{
30    pthread_mutex_destroy(&mMutex);
31    pthread_cond_destroy(&mCondition);
32}
33
34bool Signal::init()
35{
36    int status = pthread_mutex_init(&mMutex, NULL);
37    if (status) {
38        LOGE("LocklessFifo mutex init failure");
39        return false;
40    }
41
42    status = pthread_cond_init(&mCondition, NULL);
43    if (status) {
44        LOGE("LocklessFifo condition init failure");
45        pthread_mutex_destroy(&mMutex);
46        return false;
47    }
48
49    return true;
50}
51
52void Signal::set()
53{
54    int status;
55
56    status = pthread_mutex_lock(&mMutex);
57    if (status) {
58        LOGE("LocklessCommandFifo: error %i locking for set condition.", status);
59        return;
60    }
61
62    mSet = true;
63
64    status = pthread_cond_signal(&mCondition);
65    if (status) {
66        LOGE("LocklessCommandFifo: error %i on set condition.", status);
67    }
68
69    status = pthread_mutex_unlock(&mMutex);
70    if (status) {
71        LOGE("LocklessCommandFifo: error %i unlocking for set condition.", status);
72    }
73}
74
75void Signal::wait()
76{
77    int status;
78
79    status = pthread_mutex_lock(&mMutex);
80    if (status) {
81        LOGE("LocklessCommandFifo: error %i locking for condition.", status);
82        return;
83    }
84
85    if (!mSet) {
86        status = pthread_cond_wait(&mCondition, &mMutex);
87        if (status) {
88            LOGE("LocklessCommandFifo: error %i waiting on condition.", status);
89        }
90    }
91    mSet = false;
92
93    status = pthread_mutex_unlock(&mMutex);
94    if (status) {
95        LOGE("LocklessCommandFifo: error %i unlocking for condition.", status);
96    }
97}
98
99