MongoDB  2.7.0
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/thread/condition.hpp>
21 #include "mutex.h"
22 
23 namespace mongo {
24 
25  /*
26  * A class to establish a synchronization point between two threads. One thread is the waiter and one is
27  * the notifier. After the notification event, both proceed normally.
28  *
29  * This class is thread-safe.
30  */
31  class Notification : boost::noncopyable {
32  public:
33  Notification();
34 
35  /*
36  * Blocks until the method 'notifyOne()' is called.
37  */
38  void waitToBeNotified();
39 
40  /*
41  * Notifies the waiter of '*this' that it can proceed. Can only be called once.
42  */
43  void notifyOne();
44 
45  private:
46  mongo::mutex _mutex; // protects state below
47  unsigned long long lookFor;
48  unsigned long long cur;
49  boost::condition _condition; // cond over _notified being true
50  };
51 
55  class NotifyAll : boost::noncopyable {
56  public:
57  NotifyAll();
58 
59  typedef unsigned long long When;
60 
61  When now();
62 
66  void waitFor(When);
67 
69  void awaitBeyondNow();
70 
72  void notifyAll(When);
73 
75  unsigned nWaiting() const { return _nWaiting; }
76 
77  private:
78  mongo::mutex _mutex;
79  boost::condition _condition;
80  When _lastDone;
81  When _lastReturned;
82  unsigned _nWaiting;
83  };
84 
85 } // namespace mongo
void awaitBeyondNow()
a bit faster than waitFor( now() )
Definition: synchronization.cpp:64
unsigned nWaiting() const
indicates how many threads are waiting for a notify.
Definition: synchronization.h:75
void notifyAll(When)
may be called multiple times.
Definition: synchronization.cpp:73
On pthread systems, it is an error to destroy a mutex while held (boost mutex may use pthread)...
Definition: mutex.h:74
void waitFor(When)
awaits the next notifyAll() call by another thread.
Definition: synchronization.cpp:56
Definition: synchronization.h:31
establishes a synchronization point between threads.
Definition: synchronization.h:55