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

Commit 7c671925 authored by Yu Shan's avatar Yu Shan
Browse files

Remove lock for fakeVehicleHardware callbacks.

The callbacks are called very frequently, guarding them with the
main mLock will cause the mLock to become very hot and possibly
blocking other operations from owning the lock. Since the callback
is only set once by DefaultVehicleHal during initialization, it
is really not necessary to guard them with lock.

Test: manual run on gcar_emu
Bug: 255574557
Change-Id: Icc1f90b89578a27729ef8beae3a475966f72d318
parent efa1e9a5
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -138,9 +138,12 @@ class FakeVehicleHardware : public IVehicleHardware {
    std::unique_ptr<RecurrentTimer> mRecurrentTimer;
    // GeneratorHub is thread-safe.
    std::unique_ptr<GeneratorHub> mGeneratorHub;

    // Only allowed to set once.
    std::unique_ptr<const PropertyChangeCallback> mOnPropertyChangeCallback;
    std::unique_ptr<const PropertySetErrorCallback> mOnPropertySetErrorCallback;

    std::mutex mLock;
    std::unique_ptr<const PropertyChangeCallback> mOnPropertyChangeCallback GUARDED_BY(mLock);
    std::unique_ptr<const PropertySetErrorCallback> mOnPropertySetErrorCallback GUARDED_BY(mLock);
    std::unordered_map<PropIdAreaId, std::shared_ptr<RecurrentTimer::Callback>, PropIdAreaIdHash>
            mRecurrentActions GUARDED_BY(mLock);
    std::unordered_map<PropIdAreaId, VehiclePropValuePool::RecyclableType, PropIdAreaIdHash>
+8 −4
Original line number Diff line number Diff line
@@ -1228,13 +1228,19 @@ StatusCode FakeVehicleHardware::checkHealth() {

void FakeVehicleHardware::registerOnPropertyChangeEvent(
        std::unique_ptr<const PropertyChangeCallback> callback) {
    std::scoped_lock<std::mutex> lockGuard(mLock);
    if (mOnPropertyChangeCallback != nullptr) {
        ALOGE("registerOnPropertyChangeEvent must only be called once");
        return;
    }
    mOnPropertyChangeCallback = std::move(callback);
}

void FakeVehicleHardware::registerOnPropertySetErrorEvent(
        std::unique_ptr<const PropertySetErrorCallback> callback) {
    std::scoped_lock<std::mutex> lockGuard(mLock);
    if (mOnPropertySetErrorCallback != nullptr) {
        ALOGE("registerOnPropertySetErrorEvent must only be called once");
        return;
    }
    mOnPropertySetErrorCallback = std::move(callback);
}

@@ -1278,8 +1284,6 @@ StatusCode FakeVehicleHardware::updateSampleRate(int32_t propId, int32_t areaId,
}

void FakeVehicleHardware::onValueChangeCallback(const VehiclePropValue& value) {
    std::scoped_lock<std::mutex> lockGuard(mLock);

    if (mOnPropertyChangeCallback == nullptr) {
        return;
    }
+2 −1
Original line number Diff line number Diff line
@@ -116,11 +116,12 @@ class IVehicleHardware {
    virtual aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() = 0;

    // Register a callback that would be called when there is a property change event from vehicle.
    // Must only be called once during initialization.
    virtual void registerOnPropertyChangeEvent(
            std::unique_ptr<const PropertyChangeCallback> callback) = 0;

    // Register a callback that would be called when there is a property set error event from
    // vehicle.
    // vehicle. Must only be called once during initialization.
    virtual void registerOnPropertySetErrorEvent(
            std::unique_ptr<const PropertySetErrorCallback> callback) = 0;
};