MongoDB C++ Driver  legacy-1.0.3
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
synchronization.h
1 // synchronization.h
2 
3 /* Copyright 2010 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <boost/noncopyable.hpp>
21 #include <boost/thread/condition_variable.hpp>
22 #include <boost/thread/mutex.hpp>
23 
24 namespace mongo {
25 
26  /*
27  * A class to establish a synchronization point between two threads. One thread is the waiter and one is
28  * the notifier. After the notification event, both proceed normally.
29  *
30  * This class is thread-safe.
31  */
32  class Notification : boost::noncopyable {
33  public:
34  Notification();
35 
36  /*
37  * Blocks until the method 'notifyOne()' is called.
38  */
39  void waitToBeNotified();
40 
41  /*
42  * Notifies the waiter of '*this' that it can proceed. Can only be called once.
43  */
44  void notifyOne();
45 
46  private:
47  boost::mutex _mutex; // protects state below
48  unsigned long long lookFor;
49  unsigned long long cur;
50  boost::condition_variable _condition; // cond over _notified being true
51  };
52 
56  class NotifyAll : boost::noncopyable {
57  public:
58  NotifyAll();
59 
60  typedef unsigned long long When;
61 
62  When now();
63 
67  void waitFor(When);
68 
70  void awaitBeyondNow();
71 
73  void notifyAll(When);
74 
76  unsigned nWaiting() const { return _nWaiting; }
77 
78  private:
79  boost::mutex _mutex;
80  boost::condition_variable _condition;
81  When _lastDone;
82  When _lastReturned;
83  unsigned _nWaiting;
84  };
85 
86 } // namespace mongo
void awaitBeyondNow()
a bit faster than waitFor( now() )
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
establishes a synchronization point between threads.
Definition: synchronization.h:56
void waitFor(When)
awaits the next notifyAll() call by another thread.
unsigned nWaiting() const
indicates how many threads are waiting for a notify.
Definition: synchronization.h:76
Definition: synchronization.h:32
void notifyAll(When)
may be called multiple times.