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

Commit a8f1c6e4 authored by Andy Hung's avatar Andy Hung
Browse files

MediaMetrics: Add copy constructor to TimeMachine

Test: atest mediametrics_tests
Bug: 138583596
Change-Id: I811f3109e84cbfc7f7a44a407fe21c1013a34cd0
parent fb0aaa0f
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ std::ostream & operator<< (std::ostream& s,
 *
 * The TimeMachine is NOT thread safe.
 */
class TimeMachine {
class TimeMachine final { // made final as we have copy constructor instead of dup() override.
public:
    using Elem = Item::Prop::Elem;  // use the Item property element.
    using PropertyHistory = std::multimap<int64_t /* time */, Elem>;
@@ -84,6 +84,8 @@ private:
            putValue(BUNDLE_UID, (int32_t)uid, time);
        }

        KeyHistory(const KeyHistory &other) = default;

        status_t checkPermission(uid_t uidCheck) const {
            return uidCheck != (uid_t)-1 && uidCheck != mUid ? PERMISSION_DENIED : NO_ERROR;
        }
@@ -189,6 +191,34 @@ public:
                  __func__, keyHighWaterMark, keyLowWaterMark);
    }

    // The TimeMachine copy constructor/assignment uses a deep copy,
    // though the snapshot is not instantaneous nor isochronous.
    //
    // If there are concurrent operations ongoing in the other TimeMachine
    // then there may be some history more recent than others (a time shear).
    // This is expected to be a benign addition in history as small number of
    // future elements are incorporated.
    TimeMachine(const TimeMachine& other) {
        *this = other;
    }
    TimeMachine& operator=(const TimeMachine& other) {
        std::lock_guard lock(mLock);
        mHistory.clear();

        {
            std::lock_guard lock2(other.mLock);
            mHistory = other.mHistory;
        }

        // Now that we safely have our own shared pointers, let's dup them
        // to ensure they are decoupled.  We do this by acquiring the other lock.
        for (const auto &[lkey, lhist] : mHistory) {
            std::lock_guard lock2(other.getLockForKey(lkey));
            mHistory[lkey] = std::make_shared<KeyHistory>(*lhist);
        }
        return *this;
    }

    /**
     * Put all the properties from an item into the Time Machine log.
     */