Skip to content

Commit 8fda3bf

Browse files
otmeta-codesync[bot]
authored andcommitted
Kill MutexWrapper
Summary: `fb303` uses `SharedMutex` for its `Synchronized` objects, but it does so through the `MutexWrapper` adapter, so that all the `lock()` calls did not have to be codemodded to `wlock()`. However, in some codepaths it is actually possible to use an rlock, so it's best to remove the wrapper and just codemod all the call sites. Reviewed By: uvdn7 Differential Revision: D84917811 fbshipit-source-id: 33c3fbee5d9058c027da821bca30f2e725c124a5
1 parent 066456f commit 8fda3bf

12 files changed

Lines changed: 59 additions & 120 deletions

fb303/BUCK

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ cpp_library(
7474
],
7575
exported_deps = [
7676
":export_type",
77-
":mutex_wrapper",
7877
":timeseries",
7978
"//folly:synchronized",
8079
"//folly/container:f14_hash",
@@ -162,7 +161,6 @@ cpp_library(
162161
":dynamic_counters",
163162
":export_type",
164163
":exported_stat_map_impl",
165-
":mutex_wrapper",
166164
":timeseries_histogram",
167165
"//folly:function",
168166
"//folly:map_util",
@@ -175,17 +173,6 @@ cpp_library(
175173
],
176174
)
177175

178-
cpp_library(
179-
name = "mutex_wrapper",
180-
srcs = [],
181-
headers = ["MutexWrapper.h"],
182-
modular_headers = True,
183-
exported_deps = [
184-
"//folly:shared_mutex",
185-
"//folly:synchronized",
186-
],
187-
)
188-
189176
cpp_library(
190177
name = "service_data",
191178
srcs = ["ServiceData.cpp"],
@@ -337,8 +324,8 @@ cpp_library(
337324
],
338325
exported_deps = [
339326
":export_type",
340-
":mutex_wrapper",
341327
":timeseries",
328+
"//folly:synchronized",
342329
],
343330
external_deps = [
344331
"glog",

fb303/ExportedHistogramMap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CounterType getHistogramPercentile(
2626
const ExportedHistogramMap::HistogramPtr& hist,
2727
int level,
2828
double percentile) {
29-
auto lockedHist = hist->lock();
29+
auto lockedHist = hist->wlock();
3030

3131
// make sure the histogram is up to date and data is decayed appropriately
3232
lockedHist->update(get_legacy_stats_time());
@@ -145,7 +145,7 @@ void ExportedHistogramMap::checkAdd(
145145
int64_t max) const {
146146
// Log an error if someone tries to create an existing histogram with
147147
// different parameters.
148-
auto lockedHist = item->lock();
148+
auto lockedHist = item->wlock();
149149
if (lockedHist->getBucketSize() != bucketWidth ||
150150
lockedHist->getMin() != min || lockedHist->getMax() != max) {
151151
LOG(ERROR) << "Attempted to create an existing histogram with "
@@ -202,7 +202,7 @@ void ExportedHistogramMap::unexportStat(StringPiece name, ExportType type) {
202202
void ExportedHistogramMap::clearAllHistograms() {
203203
auto lockedHistMap = histMap_.wlock();
204204
for (auto& histPtrKvp : *lockedHistMap) {
205-
histPtrKvp.second->lock()->clear();
205+
histPtrKvp.second->wlock()->clear();
206206
}
207207
}
208208
} // namespace facebook::fb303

fb303/ExportedHistogramMap.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <fb303/DynamicCounters.h>
2020
#include <fb303/ExportType.h>
21-
#include <fb303/MutexWrapper.h>
2221
#include <fb303/TimeseriesHistogram.h>
2322
#include <folly/Function.h>
2423
#include <folly/MapUtil.h>
@@ -62,9 +61,9 @@ using ExportedStatForHistogram = MultiLevelTimeSeries<
6261

6362
class ExportedHistogramMap {
6463
public:
65-
using SyncHistogram = folly::Synchronized<ExportedHistogram, MutexWrapper>;
64+
using SyncHistogram = folly::Synchronized<ExportedHistogram>;
6665
using HistogramPtr = std::shared_ptr<SyncHistogram>;
67-
using LockedHistogramPtr = SyncHistogram::LockedPtr;
66+
using LockedHistogramPtr = SyncHistogram::WLockedPtr;
6867
using HistMap = folly::F14NodeMap<std::string, HistogramPtr>;
6968
using MakeExportedHistogram = folly::FunctionRef<ExportedHistogram()>;
7069

@@ -136,7 +135,7 @@ class ExportedHistogramMap {
136135
LockedHistogramPtr getHistogram(folly::StringPiece name) {
137136
auto hist = getHistogramUnlocked(name);
138137
if (hist) {
139-
return hist->lock();
138+
return hist->wlock();
140139
}
141140
return LockedHistogramPtr();
142141
}
@@ -302,7 +301,7 @@ class ExportedHistogramMap {
302301
int64_t times = 1) {
303302
HistogramPtr hist = getHistogramUnlocked(name);
304303
if (hist) {
305-
hist->lock()->addValue(now, value, times);
304+
hist->wlock()->addValue(now, value, times);
306305
}
307306
}
308307

@@ -312,7 +311,7 @@ class ExportedHistogramMap {
312311
const folly::Histogram<CounterType>& values) {
313312
HistogramPtr hist = getHistogramUnlocked(name);
314313
if (hist) {
315-
hist->lock()->addValues(now, values);
314+
hist->wlock()->addValues(now, values);
316315
}
317316
}
318317

@@ -360,7 +359,7 @@ class ExportedHistogramMap {
360359
void clearHistogram(folly::StringPiece name) {
361360
HistogramPtr hist = getHistogramUnlocked(name);
362361
if (hist) {
363-
hist->lock()->clear();
362+
hist->wlock()->clear();
364363
}
365364
}
366365

fb303/ExportedHistogramMapImpl.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ExportedHistogramMapImpl : public ExportedHistogramMap {
4747
* locked as long as the LockedHistogramPtr remains in scope.
4848
*/
4949
LockedHistogramPtr makeLockGuard() const {
50-
return hist_->lock();
50+
return hist_->wlock();
5151
}
5252

5353
/** Return true if histogram is null. */
@@ -65,23 +65,23 @@ class ExportedHistogramMapImpl : public ExportedHistogramMap {
6565
* locked for the duration of this call.
6666
*/
6767
CounterType getBucketSize() const {
68-
return hist_->lock()->getBucketSize();
68+
return hist_->wlock()->getBucketSize();
6969
}
7070

7171
/**
7272
* Return the minimum value of this associated histogram. The histogram
7373
* remains locked for the duration of this call.
7474
*/
7575
CounterType getMin() const {
76-
return hist_->lock()->getMin();
76+
return hist_->wlock()->getMin();
7777
}
7878

7979
/**
8080
* Return the maximum value of the associated histogram. The histogram
8181
* remains locked for the duration of this call.
8282
*/
8383
CounterType getMax() const {
84-
return hist_->lock()->getMax();
84+
return hist_->wlock()->getMax();
8585
}
8686

8787
/**
@@ -91,7 +91,7 @@ class ExportedHistogramMapImpl : public ExportedHistogramMap {
9191
*/
9292
void addValue(time_t now, const CounterType value, int64_t times = 1)
9393
const {
94-
hist_->lock()->addValue(now, value, times);
94+
hist_->wlock()->addValue(now, value, times);
9595
}
9696

9797
/**
@@ -115,10 +115,10 @@ class ExportedHistogramMapImpl : public ExportedHistogramMap {
115115
* histogram. The histogram remains locked for the duration of this call.
116116
*/
117117
void addValues(std::chrono::seconds now, const Histogram& values) const {
118-
hist_->lock()->addValues(now, values);
118+
hist_->wlock()->addValues(now, values);
119119
}
120120
void addValues(time_t now, const Histogram& values) const {
121-
hist_->lock()->addValues(now, values);
121+
hist_->wlock()->addValues(now, values);
122122
}
123123

124124
/**
@@ -217,7 +217,7 @@ class ExportedHistogramMapImpl : public ExportedHistogramMap {
217217
time_t now,
218218
CounterType value,
219219
int64_t times = 1) {
220-
histogram->lock()->addValue(now, value, times);
220+
histogram->wlock()->addValue(now, value, times);
221221
}
222222

223223
/*

fb303/ExportedStatMap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ void ExportedStatMap::forgetStatsFor(StringPiece name) {
135135
void ExportedStatMap::flushAllStats() {
136136
auto lockedStatMap = statMap_.wlock();
137137
for (auto& [_, ptr] : *lockedStatMap) {
138-
ptr->lock()->flush();
138+
ptr->wlock()->flush();
139139
}
140140
}
141141

142142
void ExportedStatMap::clearAllStats() {
143143
auto lockedStatMap = statMap_.wlock();
144144
for (auto& [_, ptr] : *lockedStatMap) {
145-
ptr->lock()->clear();
145+
ptr->wlock()->clear();
146146
}
147147
}
148148

fb303/ExportedStatMap.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <memory>
2020

2121
#include <fb303/ExportType.h>
22-
#include <fb303/MutexWrapper.h>
2322
#include <fb303/Timeseries.h>
2423
#include <folly/Synchronized.h>
2524
#include <folly/container/F14Map.h>
@@ -40,9 +39,9 @@ using TimePoint = ExportedStat::TimePoint;
4039

4140
class ExportedStatMap {
4241
public:
43-
using SyncStat = folly::Synchronized<ExportedStat, MutexWrapper>;
42+
using SyncStat = folly::Synchronized<ExportedStat>;
4443
using StatPtr = std::shared_ptr<SyncStat>;
45-
using LockedStatPtr = SyncStat::LockedPtr;
44+
using LockedStatPtr = SyncStat::WLockedPtr;
4645
using StatMap = folly::F14FastMap<std::string, StatPtr>;
4746

4847
/*
@@ -119,7 +118,7 @@ class ExportedStatMap {
119118
* --
120119
*/
121120
LockedStatPtr getLockedStatPtr(folly::StringPiece name) {
122-
auto result = getStatPtr(name)->lock();
121+
auto result = getStatPtr(name)->wlock();
123122
result->flush();
124123
return result;
125124
}
@@ -179,7 +178,7 @@ class ExportedStatMap {
179178
TimePoint now,
180179
CounterType value,
181180
ExportType type) {
182-
getStatPtr(name, &type)->lock()->addValue(now, value);
181+
getStatPtr(name, &type)->wlock()->addValue(now, value);
183182
}
184183

185184
/*
@@ -191,7 +190,7 @@ class ExportedStatMap {
191190
TimePoint now,
192191
CounterType value,
193192
folly::Range<const ExportType*> exportTypes) {
194-
getStatPtr(name, exportTypes)->lock()->addValue(now, value);
193+
getStatPtr(name, exportTypes)->wlock()->addValue(now, value);
195194
}
196195

197196
/*
@@ -203,7 +202,7 @@ class ExportedStatMap {
203202
TimePoint now,
204203
CounterType value,
205204
int64_t times = 1) {
206-
getStatPtr(name)->lock()->addValue(now, value, times);
205+
getStatPtr(name)->wlock()->addValue(now, value, times);
207206
}
208207

209208
/*
@@ -215,14 +214,14 @@ class ExportedStatMap {
215214
TimePoint now,
216215
CounterType sum,
217216
int64_t nsamples) {
218-
getStatPtr(name)->lock()->addValueAggregated(now, sum, nsamples);
217+
getStatPtr(name)->wlock()->addValueAggregated(now, sum, nsamples);
219218
}
220219

221220
/*
222221
* Removes all entries from the map specified by 'name.'
223222
*/
224223
void clearValue(folly::StringPiece name) {
225-
getStatPtr(name)->lock()->clear();
224+
getStatPtr(name)->wlock()->clear();
226225
}
227226

228227
/*

fb303/ExportedStatMapImpl.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class ExportedStatMapImpl : public ExportedStatMap {
4141

4242
/*
4343
* Locks the timeseries object that is held by this LockableStat object
44-
* and returns the LockedPtr for it. The histogram remains locked as long as
45-
* the LockedPtr object remains in scope.
44+
* and returns the LockedStatPtr for it. The histogram remains locked as
45+
* long as the LockedStatPtr object remains in scope.
4646
*/
4747
LockedStatPtr lock() const {
48-
return stat_->lock();
48+
return stat_->wlock();
4949
}
5050

5151
/* Return true if the stat held by this object is null. */
@@ -70,7 +70,7 @@ class ExportedStatMapImpl : public ExportedStatMap {
7070
*/
7171
void addValue(TimePoint now, const CounterType value, int64_t times = 1)
7272
const {
73-
stat_->lock()->addValue(now, value, times);
73+
stat_->wlock()->addValue(now, value, times);
7474
}
7575

7676
/*
@@ -81,15 +81,15 @@ class ExportedStatMapImpl : public ExportedStatMap {
8181
TimePoint now,
8282
const CounterType value,
8383
int64_t numSamples) const {
84-
stat_->lock()->addValueAggregated(now, value, numSamples);
84+
stat_->wlock()->addValueAggregated(now, value, numSamples);
8585
}
8686

8787
/*
8888
* Add a value at time 'now' to all levels. The optional 'times' parameter
8989
* can be used here to add multiple copies of the value at a time.
9090
*
9191
* This method assumes that the object has already been locked, and requires
92-
* the appropriate LockedPtr object as a parameter.
92+
* the appropriate LockedStatPtr object as a parameter.
9393
*/
9494
void addValueLocked(
9595
const LockedStatPtr& lockedObj,
@@ -104,7 +104,7 @@ class ExportedStatMapImpl : public ExportedStatMap {
104104
* Update the histogram with the given time value.
105105
*
106106
* This method assumes that the object has already been locked, and requires
107-
* the appropriate LockedPtr object as a parameter.
107+
* the appropriate LockedStatPtr object as a parameter.
108108
*/
109109
void updateLocked(const LockedStatPtr& lockedObj, TimePoint now) {
110110
DCHECK(!lockedObj.isNull());
@@ -115,7 +115,7 @@ class ExportedStatMapImpl : public ExportedStatMap {
115115
* Flush all cached updates.
116116
*
117117
* This method assumes that the object has already been locked, and requires
118-
* the appropriate LockedPtr object as a parameter.
118+
* the appropriate LockedStatPtr object as a parameter.
119119
*/
120120
void flushLocked(const LockedStatPtr& lockedObj) {
121121
DCHECK(!lockedObj.isNull());
@@ -128,14 +128,14 @@ class ExportedStatMapImpl : public ExportedStatMap {
128128
*/
129129
template <typename ReturnType>
130130
ReturnType rate(int level) {
131-
return stat_->lock()->rate<ReturnType>(level);
131+
return stat_->wlock()->rate<ReturnType>(level);
132132
}
133133

134134
/*
135135
* Return the sum of all the data points currently tracked at this level.
136136
*
137137
* This method assumes that the object has already been locked, and requires
138-
* the appropriate LockedPtr object as a parameter.
138+
* the appropriate LockedStatPtr object as a parameter.
139139
*/
140140
CounterType getSumLocked(const LockedStatPtr& lockedObj, int level) {
141141
DCHECK(!lockedObj.isNull());
@@ -217,7 +217,7 @@ class ExportedStatMapImpl : public ExportedStatMap {
217217
*/
218218
void
219219
addValue(StatPtr& item, TimePoint now, CounterType value, int64_t times = 1) {
220-
item->lock()->addValue(now, value, times);
220+
item->wlock()->addValue(now, value, times);
221221
}
222222

223223
using ExportedStatMap::addValueAggregated;
@@ -238,7 +238,7 @@ class ExportedStatMapImpl : public ExportedStatMap {
238238
TimePoint now,
239239
CounterType sum,
240240
int64_t nsamples) {
241-
item->lock()->addValueAggregated(now, sum, nsamples);
241+
item->wlock()->addValueAggregated(now, sum, nsamples);
242242
}
243243

244244
using ExportedStatMap::exportStat;

0 commit comments

Comments
 (0)