00001 // synchronization.h 00002 00003 /* Copyright 2010 10gen Inc. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #pragma once 00019 00020 #include <boost/thread/condition.hpp> 00021 #include "mutex.h" 00022 00023 namespace mongo { 00024 00025 /* 00026 * A class to establish a synchronization point between two threads. One thread is the waiter and one is 00027 * the notifier. After the notification event, both proceed normally. 00028 * 00029 * This class is thread-safe. 00030 */ 00031 class Notification { 00032 public: 00033 Notification(); 00034 ~Notification(); 00035 00036 /* 00037 * Blocks until the method 'notifyOne()' is called. 00038 */ 00039 void waitToBeNotified(); 00040 00041 /* 00042 * Notifies the waiter of '*this' that it can proceed. Can only be called once. 00043 */ 00044 void notifyOne(); 00045 00046 private: 00047 mongo::mutex _mutex; // protects state below 00048 bool _notified; // was notifyOne() issued? 00049 boost::condition _condition; // cond over _notified being true 00050 }; 00051 00055 class NotifyAll : boost::noncopyable { 00056 public: 00057 NotifyAll(); 00058 00062 void wait(); 00063 00065 void notifyAll(); 00066 00067 private: 00068 mongo::mutex _mutex; 00069 unsigned long long _counter; 00070 boost::condition _condition; 00071 }; 00072 00073 } // namespace mongo