doudou / orocos-rtt forked from psoetens/orocos-rtt

Orocos Real-Time Toolkit

This URL has Read+Write access

Janosch Machowinski (author)
Wed Nov 26 07:50:12 -0800 2008
Sylvain Joyeux (committer)
Fri Dec 19 07:31:38 -0800 2008
orocos-rtt / src / BufferLockFree.hpp
100644 238 lines (210 sloc) 8.659 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
tag: Peter Soetens Thu Jan 13 10:24:51 CET 2005 BufferLockFree.hpp
 
BufferLockFree.hpp - description
-------------------
begin : Thu January 13 2005
copyright : (C) 2005 Peter Soetens
email : peter.soetens@mech.kuleuven.ac.be
 
***************************************************************************
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public *
* License as published by the Free Software Foundation; *
* version 2 of the License. *
* *
* As a special exception, you may use this file as part of a free *
* software library without restriction. Specifically, if other files *
* instantiate templates or use macros or inline functions from this *
* file, or you compile this file and link it with other files to *
* produce an executable, this file does not by itself cause the *
* resulting executable to be covered by the GNU General Public *
* License. This exception does not however invalidate any other *
* reasons why the executable file might be covered by the GNU General *
* Public License. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307 USA *
* *
***************************************************************************/
 
#ifndef ORO_BUFFER_LOCK_FREE_HPP
#define ORO_BUFFER_LOCK_FREE_HPP
 
#include "os/oro_atomic.h"
#include "os/CAS.hpp"
#include "BufferPolicy.hpp"
#include "BufferInterface.hpp"
#include "AtomicQueue.hpp"
#include "MemoryPool.hpp"
#include <vector>
 
#ifdef ORO_PRAGMA_INTERFACE
#pragma interface
#endif
 
namespace RTT
{
 
    using OS::CAS;
 
    /**
* A Lock-free buffer implementation to read and write
* data of type \a T in a FIFO way. Optionally block (synchronize) on an
* \a empty of \a full buffer (See \a Policy below ). The
* default Policy is thus completely Asynchronous (non-blocking).
* No memory allocation is done during read or write, any number of threads
* may access this buffer concurrently.
* @param T The value type to be stored in the Buffer.
* Example : BufferLockFree<A> is a buffer which holds values of type A.
* @param ReadPolicy The Policy to block (wait) on \a empty (during read),
* using \a BlockingPolicy, or to return \a false, using \a NonBlockingPolicy (Default).
* This does not influence partial filled buffer behaviour.
* @param WritePolicy The Policy to block (wait) on \a full (during write),
* using \a BlockingPolicy, or to return \a false, using \a NonBlockingPolicy (Default).
* This does not influence partial filled buffer behaviour.
* @ingroup Ports
*/
    template< class T, class ReadPolicy = NonBlockingPolicy, class WritePolicy = NonBlockingPolicy >
    class BufferLockFree
        : public BufferInterface<T>
    {
    public:
        typedef typename ReadInterface<T>::reference_t reference_t;
        typedef typename WriteInterface<T>::param_t param_t;
        typedef typename BufferInterface<T>::size_type size_type;
        typedef T value_t;
    private:
        typedef T Item;
        AtomicQueue<Item*,ReadPolicy,WritePolicy> bufs;
        // is mutable because of reference counting.
        mutable FixedSizeMemoryPool<Item> mpool;
    public:
        /**
* Create a lock-free buffer wich can store \a bufsize elements.
* @param bufsize the capacity of the buffer.
' */
        BufferLockFree( unsigned int bufsize, const T& initial_value = T() )
            : bufs( bufsize ), mpool(bufsize, initial_value)
        {
        }
 
        size_type capacity() const
        {
            return bufs.capacity();
        }
 
        size_type size() const
        {
            return bufs.size();
        }
 
        bool empty() const
        {
            return bufs.isEmpty();
        }
 
        bool full() const
        {
            return bufs.isFull();
        }
 
        void clear()
        {
            Item* item;
            while ( bufs.dequeue(item) )
                mpool.deallocate( item );
        }
 
        /**
* Write a single value to the buffer.
* @param d the value to write
* @return false if the buffer is full.
* @deprecated by Push( param_t item )
*/
        bool write( param_t d ) {
            return this->Push( d );
        }
 
        bool Push( param_t item)
        {
            Item* mitem = mpool.allocate();
            if ( mitem == 0 ) // queue full.
                return false;
            // copy over.
            *mitem = item;
            if (bufs.enqueue( mitem ) == false ) { // can this ever happen ?
                //mpool.deallocate( mitem );
                //return false;
                assert(false && "Race detected in Push()");
            }
            return true;
        }
 
        /**
* Write a sequence of values to the buffer.
* Block if full if Policy is BlockingPolicy.
* @param d the values to write
* @return the number of values written (may be less than d.size())
* @deprecated by Push(const std::vector<T>& item)
*/
        size_type write( const std::vector<T>& d) {
            return this->Push( d );
        }
 
        size_type Push(const std::vector<T>& items)
        {
            int towrite = items.size();
            typename std::vector<T>::const_iterator it;
            for( it = items.begin(); it != items.end(); ++it)
                if ( this->Push( *it ) == false )
                    break;
            return towrite - (items.end() - it);
        }
 
 
        /**
* Read the oldest value from the buffer.
* Block if empty if Policy is BlockingPolicy.
* @param res is to be filled with a value from the buffer.
* @return true if something was read.
* @deprecated by Pop( reference_t item )
*/
        bool read(T& res) {
            return this->Pop( res );
        }
 
        value_t front() const {
            Item* orig;
            orig = bufs.lockfront(mpool);
            // if orig == 0, then queue is empty
            if (orig == 0)
                return value_t();
 
            // ok, copy, unlock and return front.
            value_t ret = *orig;
            mpool.unlock( orig );
            return ret;
        }
 
        bool Pop( reference_t item )
        {
            Item* ipop;
            if (bufs.dequeue( ipop ) == false )
                return false;
            item = *ipop;
            if (mpool.deallocate( ipop ) == false )
                assert(false);
            return true;
        }
 
        /**
* Read the whole buffer. Block if empty if Policy is BlockingPolicy.
* @param res is to be filled with all values in the buffer,
* with res.begin() the oldest value.
* @return the number of items read.
* @deprecated by Pop( std::vector<T>& items )
*/
        size_type read(std::vector<T>& res)
        {
            return this->Pop( res );
        }
 
        size_type Pop(std::vector<T>& items )
        {
            Item* ipop;
            items.clear();
            while( bufs.dequeue(ipop) ) {
                items.push_back( *ipop );
                if (mpool.deallocate(ipop) == false)
                    assert(false);
            }
            return items.size();
        }
 
    };
 
    extern template class BufferLockFree<double>;
}
 
#endif