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

Commit 5c5513f2 authored by Yao Chen's avatar Yao Chen Committed by Android (Google) Code Review
Browse files

Merge "Add a timeout option in shell subscriber."

parents d6c1bdfe 35cb8d65
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -332,6 +332,8 @@ java_library {
        "src/stats_log.proto",
        "src/statsd_config.proto",
        "src/atoms.proto",
        "src/shell/shell_config.proto",
        "src/shell/shell_data.proto",
    ],

    static_libs: [
+5 −1
Original line number Diff line number Diff line
@@ -360,7 +360,11 @@ status_t StatsService::command(int in, int out, int err, Vector<String8>& args,
            if (mShellSubscriber == nullptr) {
                mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager);
            }
            mShellSubscriber->startNewSubscription(in, out, resultReceiver);
            int timeoutSec = -1;
            if (argCount >= 2) {
                timeoutSec = atoi(args[1].c_str());
            }
            mShellSubscriber->startNewSubscription(in, out, resultReceiver, timeoutSec);
            return NO_ERROR;
        }
    }
+10 −2
Original line number Diff line number Diff line
@@ -30,7 +30,8 @@ namespace statsd {

const static int FIELD_ID_ATOM = 1;

void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver> resultReceiver) {
void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver> resultReceiver,
                                           int timeoutSec) {
    VLOG("start new shell subscription");
    {
        std::lock_guard<std::mutex> lock(mMutex);
@@ -50,12 +51,19 @@ void ShellSubscriber::startNewSubscription(int in, int out, sp<IResultReceiver>
    // Read config forever until EOF is reached. Clients may send multiple configs -- each new
    // config replace the previous one.
    readConfig(in);
    VLOG("timeout : %d", timeoutSec);

    // Now we have read an EOF we now wait for the semaphore until the client exits.
    VLOG("Now wait for client to exit");
    std::unique_lock<std::mutex> lk(mMutex);

    if (timeoutSec > 0) {
        mShellDied.wait_for(lk, timeoutSec * 1s,
                            [this, resultReceiver] { return mResultReceiver != resultReceiver; });
    } else {
        mShellDied.wait(lk, [this, resultReceiver] { return mResultReceiver != resultReceiver; });
    }
}

void ShellSubscriber::updateConfig(const ShellSubscription& config) {
    std::lock_guard<std::mutex> lock(mMutex);
+2 −1
Original line number Diff line number Diff line
@@ -65,7 +65,8 @@ public:
    /**
     * Start a new subscription.
     */
    void startNewSubscription(int inFd, int outFd, sp<IResultReceiver> resultReceiver);
    void startNewSubscription(int inFd, int outFd, sp<IResultReceiver> resultReceiver,
                              int timeoutSec);

    void binderDied(const wp<IBinder>& who);

+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ void runShellTest(ShellSubscription config, sp<MockUidMap> uidMap,

    // mimic a binder thread that a shell subscriber runs on. it would block.
    std::thread reader([&resultReceiver, &fds_config, &fds_data, &shellClient] {
        shellClient->startNewSubscription(fds_config[0], fds_data[1], resultReceiver);
        shellClient->startNewSubscription(fds_config[0], fds_data[1], resultReceiver, -1);
    });
    reader.detach();