Barrier.h revision ca89e2a68703bd428e8b66547d033a6ed35b3595
1ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy/*
2ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * Copyright (C) 2013 The Android Open Source Project
3ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy *
4ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * you may not use this file except in compliance with the License.
6ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * You may obtain a copy of the License at
7ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy *
8ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy *
10ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * Unless required by applicable law or agreed to in writing, software
11ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * See the License for the specific language governing permissions and
14ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy * limitations under the License.
15ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy */
16ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
17ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy#ifndef ANDROID_HWUI_BARRIER_H
18ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy#define ANDROID_HWUI_BARRIER_H
19ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
20ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy#include <utils/Condition.h>
21ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
22ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guynamespace android {
23ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guynamespace uirenderer {
24ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
25ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guyclass Barrier {
26ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guypublic:
27ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    Barrier(Condition::WakeUpType type = Condition::WAKE_UP_ALL) : mType(type), mOpened(false) { }
28ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    ~Barrier() { }
29ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
30ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    void open() {
31ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        Mutex::Autolock l(mLock);
32ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        mOpened = true;
33ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        mCondition.signal(mType);
34ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    }
35ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
36ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    void close() {
37ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        Mutex::Autolock l(mLock);
38ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        mOpened = false;
39ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    }
40ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
41ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    void wait() const {
42ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        Mutex::Autolock l(mLock);
43ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        while (!mOpened) {
44ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy            mCondition.wait(mLock);
45ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy        }
46ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    }
47ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
48ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guyprivate:
49ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    Condition::WakeUpType mType;
50ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    volatile bool mOpened;
51ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    mutable Mutex mLock;
52ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy    mutable Condition mCondition;
53ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy};
54ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
55ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy}; // namespace uirenderer
56ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy}; // namespace android
57ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy
58ca89e2a68703bd428e8b66547d033a6ed35b3595Romain Guy#endif // ANDROID_HWUI_BARRIER_H
59