MongoDB  2.0.3
synchronization.h
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 
00059         typedef unsigned long long When;
00060 
00061         When now();
00062 
00066         void waitFor(When);
00067 
00069         void awaitBeyondNow();
00070 
00072         void notifyAll(When);
00073 
00075         unsigned nWaiting() const { return _nWaiting; }
00076 
00077     private:
00078         mongo::mutex _mutex;
00079         boost::condition _condition;
00080         When _lastDone;
00081         When _lastReturned;
00082         unsigned _nWaiting;
00083     };
00084 
00085 } // namespace mongo