3
/* Copyright 2009 10gen Inc.
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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
/* This is based on haskell's MVar synchronization primitive:
21
* http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Control-Concurrent-MVar.html
23
* It is a thread-safe queue that can hold at most one object.
24
* You can also think of it as a box that can be either full or empty.
30
enum State {EMPTY=0, FULL};
32
// create an empty MVar
37
// creates a full MVar
43
// puts val into the MVar and returns true or returns false if full
45
bool tryPut(const T& val){
46
// intentionally repeat test before and after lock
47
if (_state == FULL) return false;
48
Mutex::scoped_lock lock(_mutex);
49
if (_state == FULL) return false;
54
// unblock threads waiting to 'take'
55
_condition.notify_all();
60
// puts val into the MVar
61
// will block if the MVar is already full
62
void put(const T& val){
63
Mutex::scoped_lock lock(_mutex);
65
// unlocks lock while waiting and relocks before returning
66
_condition.wait(lock);
70
// takes val out of the MVar and returns true or returns false if empty
73
// intentionally repeat test before and after lock
74
if (_state == EMPTY) return false;
75
Mutex::scoped_lock lock(_mutex);
76
if (_state == EMPTY) return false;
81
// unblock threads waiting to 'put'
82
_condition.notify_all();
87
// takes val out of the MVar
88
// will block if the MVar is empty
92
Mutex::scoped_lock lock(_mutex);
93
while (!tryTake(ret)){
94
// unlocks lock while waiting and relocks before returning
95
_condition.wait(lock);
102
// Note: this is fast because there is no locking, but state could
103
// change before you get a chance to act on it.
104
// Mainly useful for sanity checks / asserts.
105
State getState(){ return _state; }
111
typedef boost::recursive_mutex Mutex;
113
boost::condition _condition;