Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1a734123 authored by Yifan Hong's avatar Yifan Hong Committed by Automerger Merge Worker
Browse files

Merge "Add RpcServer::start()" am: c77116c3

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1712597

Change-Id: Id97b6e2712c522954b02d98eb316ae5bdf2e2cf3
parents 4343a6f6 c77116c3
Loading
Loading
Loading
Loading
+22 −0
Original line number Original line Diff line number Diff line
@@ -128,6 +128,17 @@ sp<IBinder> RpcServer::getRootObject() {
    return ret;
    return ret;
}
}


static void joinRpcServer(sp<RpcServer>&& thiz) {
    thiz->join();
}

void RpcServer::start() {
    LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
    std::lock_guard<std::mutex> _l(mLock);
    LOG_ALWAYS_FATAL_IF(mJoinThread.get(), "Already started!");
    mJoinThread = std::make_unique<std::thread>(&joinRpcServer, sp<RpcServer>::fromExisting(this));
}

void RpcServer::join() {
void RpcServer::join() {
    LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
    LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");


@@ -178,6 +189,17 @@ bool RpcServer::shutdown() {
    mShutdownTrigger->trigger();
    mShutdownTrigger->trigger();
    while (mJoinThreadRunning) mShutdownCv.wait(_l);
    while (mJoinThreadRunning) mShutdownCv.wait(_l);


    // At this point, we know join() is about to exit, but the thread that calls
    // join() may not have exited yet.
    // If RpcServer owns the join thread (aka start() is called), make sure the thread exits;
    // otherwise ~thread() may call std::terminate(), which may crash the process.
    // If RpcServer does not own the join thread (aka join() is called directly),
    // then the owner of RpcServer is responsible for cleaning up that thread.
    if (mJoinThread.get()) {
        mJoinThread->join();
        mJoinThread.reset();
    }

    mShutdownTrigger = nullptr;
    mShutdownTrigger = nullptr;
    return true;
    return true;
}
}
+7 −1
Original line number Original line Diff line number Diff line
@@ -116,6 +116,11 @@ public:
    void setRootObjectWeak(const wp<IBinder>& binder);
    void setRootObjectWeak(const wp<IBinder>& binder);
    sp<IBinder> getRootObject();
    sp<IBinder> getRootObject();


    /**
     * Runs join() in a background thread. Immediately returns.
     */
    void start();

    /**
    /**
     * You must have at least one client session before calling this.
     * You must have at least one client session before calling this.
     *
     *
@@ -159,12 +164,13 @@ private:
    base::unique_fd mServer; // socket we are accepting sessions on
    base::unique_fd mServer; // socket we are accepting sessions on


    std::mutex mLock; // for below
    std::mutex mLock; // for below
    std::unique_ptr<std::thread> mJoinThread;
    bool mJoinThreadRunning = false;
    std::map<std::thread::id, std::thread> mConnectingThreads;
    std::map<std::thread::id, std::thread> mConnectingThreads;
    sp<IBinder> mRootObject;
    sp<IBinder> mRootObject;
    wp<IBinder> mRootObjectWeak;
    wp<IBinder> mRootObjectWeak;
    std::map<int32_t, sp<RpcSession>> mSessions;
    std::map<int32_t, sp<RpcSession>> mSessions;
    int32_t mSessionIdCounter = 0;
    int32_t mSessionIdCounter = 0;
    bool mJoinThreadRunning = false;
    std::unique_ptr<RpcSession::FdTrigger> mShutdownTrigger;
    std::unique_ptr<RpcSession::FdTrigger> mShutdownTrigger;
    std::condition_variable mShutdownCv;
    std::condition_variable mShutdownCv;
};
};