00001 /* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 00004 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00005 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 00006 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00007 * 00008 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00009 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00010 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00011 */ 00012 00013 #include <tick_engine.h> 00014 00015 TickEngine::TickEngine(int initial_value) 00016 { 00017 value_ = initial_value; 00018 } 00019 00020 TickEngine::~TickEngine() {} 00021 00022 void TickEngine::Wait() 00023 { 00024 // Lock acquire (need a unique lock for the condition variable usage) 00025 std::unique_lock<std::mutex> UniqueLock(mutex_); 00026 00027 // If the state is 0 then we have to wait for a signal 00028 if (value_ == 0) 00029 condition_variable_.wait(UniqueLock); 00030 00031 // Once here we decrement the state 00032 value_--; 00033 } 00034 00035 void TickEngine::Tick() 00036 { 00037 // Lock acquire 00038 00039 std::lock_guard<std::mutex> LockGuard(mutex_); 00040 00041 // State increment 00042 value_++; 00043 00044 // Notification 00045 condition_variable_.notify_all(); 00046 }