@@ -48,7 +48,7 @@ class LeapArray {
48
48
const int32_t bucket_length_ms_; // time length of each bucket
49
49
private:
50
50
const std::unique_ptr<WindowWrapSharedPtr<T>[]> array_;
51
- std::mutex mtx_ ;
51
+ mutable std::mutex leap_array_mtx_ ;
52
52
53
53
int32_t CalculateTimeIdx (/* @Valid*/ int64_t time_millis) const ;
54
54
int64_t CalculateWindowStart (/* @Valid*/ int64_t time_millis) const ;
@@ -78,9 +78,12 @@ WindowWrapSharedPtr<T> LeapArray<T>::CurrentWindow(int64_t time_millis) {
78
78
int64_t bucket_start = CalculateWindowStart (time_millis);
79
79
80
80
while (true ) {
81
+ std::unique_lock<std::mutex> lck (leap_array_mtx_, std::defer_lock);
82
+ // TODO: granularity too rough, need to be optimized.
83
+ leap_array_mtx_.lock ();
81
84
WindowWrapSharedPtr<T> old = array_[idx];
85
+ leap_array_mtx_.unlock ();
82
86
if (old == nullptr ) {
83
- std::unique_lock<std::mutex> lck (mtx_, std::defer_lock);
84
87
if (lck.try_lock () && array_[idx] == nullptr ) {
85
88
WindowWrapSharedPtr<T> bucket = std::make_shared<WindowWrap<T>>(
86
89
bucket_length_ms_, bucket_start, NewEmptyBucket (time_millis));
@@ -90,7 +93,7 @@ WindowWrapSharedPtr<T> LeapArray<T>::CurrentWindow(int64_t time_millis) {
90
93
} else if (bucket_start == old->BucketStart ()) {
91
94
return old;
92
95
} else if (bucket_start > old->BucketStart ()) {
93
- std::unique_lock<std::mutex> lck (mtx_ , std::defer_lock);
96
+ std::unique_lock<std::mutex> lck (leap_array_mtx_ , std::defer_lock);
94
97
if (lck.try_lock ()) {
95
98
ResetWindowTo (old, bucket_start);
96
99
return old;
@@ -148,7 +151,10 @@ std::vector<WindowWrapSharedPtr<T>> LeapArray<T>::Buckets(
148
151
}
149
152
int size = sample_count_; // array_.size()
150
153
for (int i = 0 ; i < size; i++) {
154
+ // TODO: granularity too rough, need to be optimized.
155
+ leap_array_mtx_.lock ();
151
156
auto w = array_[i];
157
+ leap_array_mtx_.unlock ();
152
158
if (w == nullptr || IsBucketDeprecated (time_millis, w)) {
153
159
continue ;
154
160
}
@@ -166,7 +172,10 @@ std::vector<std::shared_ptr<T>> LeapArray<T>::Values(
166
172
}
167
173
int size = sample_count_; // array_.size()
168
174
for (int i = 0 ; i < size; i++) {
175
+ // TODO: granularity too rough, need to be optimized.
176
+ leap_array_mtx_.lock ();
169
177
WindowWrapSharedPtr<T> w = array_[i];
178
+ leap_array_mtx_.unlock ();
170
179
if (w == nullptr || IsBucketDeprecated (time_millis, w)) {
171
180
continue ;
172
181
}
0 commit comments