OR-Tools  9.6
concurrent_calls_guard.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include "absl/base/thread_annotations.h"
17 #include "absl/status/status.h"
18 #include "absl/status/statusor.h"
19 #include "absl/synchronization/mutex.h"
20 #include "absl/log/check.h"
21 
23 
24 absl::StatusOr<ConcurrentCallsGuard> ConcurrentCallsGuard::TryAcquire(
25  Tracker& tracker) ABSL_NO_THREAD_SAFETY_ANALYSIS {
26  {
27  const absl::MutexLock lock(&tracker.mutex_);
28  if (tracker.in_a_call_) {
29  return absl::InvalidArgumentError("concurrent calls are forbidden");
30  }
31  tracker.in_a_call_ = true;
32  }
33  return ConcurrentCallsGuard(&tracker);
34 }
35 
37  if (tracker_ == nullptr) {
38  return;
39  }
40 
41  const absl::MutexLock lock(&tracker_->mutex_);
42  DCHECK(tracker_->in_a_call_);
43  tracker_->in_a_call_ = false;
44 }
45 
46 } // namespace operations_research::math_opt
static absl::StatusOr< ConcurrentCallsGuard > TryAcquire(Tracker &tracker)