summaryrefslogtreecommitdiff
path: root/libs/binder/include/binder/RpcThreads.h
blob: d25f29277c1eb0741f3e819b78b3d581a361027e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <pthread.h>

#include <condition_variable>
#include <functional>
#include <memory>
#include <thread>

namespace android {

#ifdef BINDER_RPC_SINGLE_THREADED
class RpcMutex {
public:
    void lock() {}
    void unlock() {}
};

class RpcMutexUniqueLock {
public:
    RpcMutexUniqueLock(RpcMutex&) {}
    void unlock() {}
};

class RpcMutexLockGuard {
public:
    RpcMutexLockGuard(RpcMutex&) {}
};

class RpcConditionVariable {
public:
    void notify_one() {}
    void notify_all() {}

    void wait(RpcMutexUniqueLock&) {}

    template <typename Predicate>
    void wait(RpcMutexUniqueLock&, Predicate stop_waiting) {
        LOG_ALWAYS_FATAL_IF(!stop_waiting(), "RpcConditionVariable::wait condition not met");
    }

    template <typename Duration>
    std::cv_status wait_for(RpcMutexUniqueLock&, const Duration&) {
        return std::cv_status::no_timeout;
    }

    template <typename Duration, typename Predicate>
    bool wait_for(RpcMutexUniqueLock&, const Duration&, Predicate stop_waiting) {
        return stop_waiting();
    }
};

class RpcMaybeThread {
public:
    RpcMaybeThread() = default;

    template <typename Function, typename... Args>
    RpcMaybeThread(Function&& f, Args&&... args) {
        // std::function requires a copy-constructible closure,
        // so we need to wrap both the function and its arguments
        // in a shared pointer that std::function can copy internally
        struct Vars {
            std::decay_t<Function> f;
            std::tuple<std::decay_t<Args>...> args;

            explicit Vars(Function&& f, Args&&... args)
                  : f(std::move(f)), args(std::move(args)...) {}
        };
        auto vars = std::make_shared<Vars>(std::forward<Function>(f), std::forward<Args>(args)...);
        mFunc = [vars]() { std::apply(std::move(vars->f), std::move(vars->args)); };
    }

    void join() {
        if (mFunc) {
            // Move mFunc into a temporary so we can clear mFunc before
            // executing the callback. This avoids infinite recursion if
            // the callee then calls join() again directly or indirectly.
            decltype(mFunc) func = nullptr;
            mFunc.swap(func);
            func();
        }
    }
    void detach() { join(); }

    class id {
    public:
        bool operator==(const id&) const { return true; }
        bool operator!=(const id&) const { return false; }
        bool operator<(const id&) const { return false; }
        bool operator<=(const id&) const { return true; }
        bool operator>(const id&) const { return false; }
        bool operator>=(const id&) const { return true; }
    };

    id get_id() const { return id(); }

private:
    std::function<void(void)> mFunc;
};

namespace rpc_this_thread {
static inline RpcMaybeThread::id get_id() {
    return RpcMaybeThread::id();
}
} // namespace rpc_this_thread

static inline void rpcJoinIfSingleThreaded(RpcMaybeThread& t) {
    t.join();
}
#else  // BINDER_RPC_SINGLE_THREADED
using RpcMutex = std::mutex;
using RpcMutexUniqueLock = std::unique_lock<std::mutex>;
using RpcMutexLockGuard = std::lock_guard<std::mutex>;
using RpcConditionVariable = std::condition_variable;
using RpcMaybeThread = std::thread;
namespace rpc_this_thread = std::this_thread;

static inline void rpcJoinIfSingleThreaded(RpcMaybeThread&) {}
#endif // BINDER_RPC_SINGLE_THREADED

} // namespace android