Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:14

0001 // Copyright 2021 The Abseil Authors
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //     https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_SCOPE_H_
0016 #define ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_SCOPE_H_
0017 
0018 #include "absl/base/config.h"
0019 #include "absl/base/optimization.h"
0020 #include "absl/base/thread_annotations.h"
0021 #include "absl/strings/internal/cord_internal.h"
0022 #include "absl/strings/internal/cordz_info.h"
0023 #include "absl/strings/internal/cordz_update_tracker.h"
0024 
0025 namespace absl {
0026 ABSL_NAMESPACE_BEGIN
0027 namespace cord_internal {
0028 
0029 // CordzUpdateScope scopes an update to the provided CordzInfo.
0030 // The class invokes `info->Lock(method)` and `info->Unlock()` to guard
0031 // cordrep updates. This class does nothing if `info` is null.
0032 // See also the 'Lock`, `Unlock` and `SetCordRep` methods in `CordzInfo`.
0033 class ABSL_SCOPED_LOCKABLE CordzUpdateScope {
0034  public:
0035   CordzUpdateScope(CordzInfo* info, CordzUpdateTracker::MethodIdentifier method)
0036       ABSL_EXCLUSIVE_LOCK_FUNCTION(info)
0037       : info_(info) {
0038     if (ABSL_PREDICT_FALSE(info_)) {
0039       info->Lock(method);
0040     }
0041   }
0042 
0043   // CordzUpdateScope can not be copied or assigned to.
0044   CordzUpdateScope(CordzUpdateScope&& rhs) = delete;
0045   CordzUpdateScope(const CordzUpdateScope&) = delete;
0046   CordzUpdateScope& operator=(CordzUpdateScope&& rhs) = delete;
0047   CordzUpdateScope& operator=(const CordzUpdateScope&) = delete;
0048 
0049   ~CordzUpdateScope() ABSL_UNLOCK_FUNCTION() {
0050     if (ABSL_PREDICT_FALSE(info_)) {
0051       info_->Unlock();
0052     }
0053   }
0054 
0055   void SetCordRep(CordRep* rep) const {
0056     if (ABSL_PREDICT_FALSE(info_)) {
0057       info_->SetCordRep(rep);
0058     }
0059   }
0060 
0061   CordzInfo* info() const { return info_; }
0062 
0063  private:
0064   CordzInfo* info_;
0065 };
0066 
0067 }  // namespace cord_internal
0068 ABSL_NAMESPACE_END
0069 }  // namespace absl
0070 
0071 #endif  // ABSL_STRINGS_INTERNAL_CORDZ_UPDATE_SCOPE_H_