Home | History | Annotate | Download | only in utility
      1 /*
      2  * Copyright 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef UTILITY_MONOTONIC_COUNTER_H
     18 #define UTILITY_MONOTONIC_COUNTER_H
     19 
     20 #include <stdint.h>
     21 
     22 /**
     23  * Maintain a 64-bit monotonic counter.
     24  * Can be used to track a 32-bit counter that wraps or gets reset.
     25  *
     26  * Note that this is not atomic and has no interior locks.
     27  * A caller will need to provide their own exterior locking
     28  * if they need to use it from multiple threads.
     29  */
     30 class MonotonicCounter {
     31 
     32 public:
     33     MonotonicCounter() {};
     34     virtual ~MonotonicCounter() {};
     35 
     36     /**
     37      * @return current value of the counter
     38      */
     39     int64_t get() const {
     40         return mCounter64;
     41     }
     42 
     43     /**
     44      * Advance the counter if delta is positive.
     45      * @return current value of the counter
     46      */
     47     int64_t increment(int64_t delta) {
     48         if (delta > 0) {
     49             mCounter64 += delta;
     50         }
     51         return mCounter64;
     52     }
     53 
     54     /**
     55      * Advance the 64-bit counter if (current32 - previousCurrent32) > 0.
     56      * This can be used to convert a 32-bit counter that may be wrapping into
     57      * a monotonic 64-bit counter.
     58      *
     59      * This counter32 should NOT be allowed to advance by more than 0x7FFFFFFF between calls.
     60      * Think of the wrapping counter like a sine wave. If the frequency of the signal
     61      * is more than half the sampling rate (Nyquist rate) then you cannot measure it properly.
     62      * If the counter wraps around every 24 hours then we should measure it with a period
     63      * of less than 12 hours.
     64      *
     65      * @return current value of the 64-bit counter
     66      */
     67     int64_t update32(int32_t counter32) {
     68         int32_t delta = counter32 - mCounter32;
     69         // protect against the mCounter64 going backwards
     70         if (delta > 0) {
     71             mCounter64 += delta;
     72             mCounter32 = counter32;
     73         }
     74         return mCounter64;
     75     }
     76 
     77     /**
     78      * Reset the stored value of the 32-bit counter.
     79      * This is used if your counter32 has been reset to zero.
     80      */
     81     void reset32() {
     82         mCounter32 = 0;
     83     }
     84 
     85 private:
     86     int64_t mCounter64 = 0;
     87     int32_t mCounter32 = 0;
     88 };
     89 
     90 
     91 #endif //UTILITY_MONOTONIC_COUNTER_H
     92