blocking_counter.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/synchronization/blocking_counter.h"
00016 
00017 #include "absl/base/internal/raw_logging.h"
00018 
00019 namespace absl {
00020 
00021 // Return whether int *arg is zero.
00022 static bool IsZero(void *arg) {
00023   return 0 == *reinterpret_cast<int *>(arg);
00024 }
00025 
00026 bool BlockingCounter::DecrementCount() {
00027   MutexLock l(&lock_);
00028   count_--;
00029   if (count_ < 0) {
00030     ABSL_RAW_LOG(
00031         FATAL,
00032         "BlockingCounter::DecrementCount() called too many times.  count=%d",
00033         count_);
00034   }
00035   return count_ == 0;
00036 }
00037 
00038 void BlockingCounter::Wait() {
00039   MutexLock l(&this->lock_);
00040   ABSL_RAW_CHECK(count_ >= 0, "BlockingCounter underflow");
00041 
00042   // only one thread may call Wait(). To support more than one thread,
00043   // implement a counter num_to_exit, like in the Barrier class.
00044   ABSL_RAW_CHECK(num_waiting_ == 0, "multiple threads called Wait()");
00045   num_waiting_++;
00046 
00047   this->lock_.Await(Condition(IsZero, &this->count_));
00048 
00049   // At this point, We know that all threads executing DecrementCount have
00050   // released the lock, and so will not touch this object again.
00051   // Therefore, the thread calling this method is free to delete the object
00052   // after we return from this method.
00053 }
00054 
00055 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14