Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / stream.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 /*
3  * This file is open source software, licensed to you under the terms
4  * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
5  * distributed with this work for additional information regarding copyright
6  * ownership.  You may not use this file except in compliance with the License.
7  *
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 /*
20  * Copyright (C) 2014 Cloudius Systems, Ltd.
21  */
22 /*
23  * Ceph - scalable distributed file system
24  *
25  * Copyright (C) 2015 XSky <haomai@xsky.com>
26  *
27  * Author: Haomai Wang <haomaiwang@gmail.com>
28  *
29  * This is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Lesser General Public
31  * License version 2.1, as published by the Free Software
32  * Foundation.  See file COPYING.
33  *
34  */
35
36 #ifndef CEPH_MSG_STREAM_H_
37 #define CEPH_MSG_STREAM_H_
38
39 #include <exception>
40 #include <cassert>
41
42 // A stream<> is the producer side.  It may call produce() as long
43 // as the returned from the previous invocation is ready.
44 // To signify no more data is available, call close().
45 //
46 // A subscription<> is the consumer side.  It is created by a call
47 // to stream::listen().  Calling subscription::start(),
48 // which registers the data processing callback, starts processing
49 // events.  It may register for end-of-stream notifications by
50 // return the when_done() future, which also delivers error
51 // events (as exceptions).
52 //
53 // The consumer can pause generation of new data by returning
54 // positive integer; when it becomes ready, the producer
55 // will resume processing.
56
57 template <typename... T>
58 class subscription;
59
60 template <typename... T>
61 class stream {
62   subscription<T...>* _sub = nullptr;
63   int done;
64   bool ready;
65  public:
66   using next_fn = std::function<int (T...)>;
67   stream() = default;
68   stream(const stream&) = delete;
69   stream(stream&&) = delete;
70   ~stream() {
71     if (_sub) {
72       _sub->_stream = nullptr;
73     }
74   }
75
76   void operator=(const stream&) = delete;
77   void operator=(stream&&) = delete;
78
79   // Returns a subscription that reads value from this
80   // stream.
81   subscription<T...> listen() {
82     return subscription<T...>(this);
83   }
84
85   // Returns a subscription that reads value from this
86   // stream, and also sets up the listen function.
87   subscription<T...> listen(next_fn next) {
88     auto sub = subscription<T...>(this);
89     sub.start(std::move(next));
90     return sub;
91   }
92
93   // Becomes ready when the listener is ready to accept
94   // values.  Call only once, when beginning to produce
95   // values.
96   bool started() {
97     return ready;
98   }
99
100   // Produce a value.  Call only after started(), and after
101   // a previous produce() is ready.
102   int produce(T... data) {
103       return _sub->_next(std::move(data)...);
104   }
105
106   // End the stream.   Call only after started(), and after
107   // a previous produce() is ready.  No functions may be called
108   // after this.
109   void close() {
110     done = 1;
111   }
112
113   // Signal an error.   Call only after started(), and after
114   // a previous produce() is ready.  No functions may be called
115   // after this.
116   void set_exception(int error) {
117     done = error;
118   }
119  private:
120   void start();
121   friend class subscription<T...>;
122 };
123
124 template <typename... T>
125 class subscription {
126  public:
127   using next_fn = typename stream<T...>::next_fn;
128  private:
129   stream<T...>* _stream;
130   next_fn _next;
131  private:
132   explicit subscription(stream<T...>* s): _stream(s) {
133     assert(!_stream->_sub);
134     _stream->_sub = this;
135   }
136
137  public:
138   subscription(subscription&& x)
139     : _stream(x._stream), _next(std::move(x._next)) {
140     x._stream = nullptr;
141     if (_stream) {
142       _stream->_sub = this;
143     }
144   }
145   ~subscription() {
146     if (_stream) {
147       _stream->_sub = nullptr;
148     }
149   }
150
151   /// \brief Start receiving events from the stream.
152   ///
153   /// \param next Callback to call for each event
154   void start(std::function<int (T...)> next) {
155     _next = std::move(next);
156     _stream->ready = true;
157   }
158
159   // Becomes ready when the stream is empty, or when an error
160   // happens (in that case, an exception is held).
161   int done() {
162     return _stream->done;
163   }
164
165   friend class stream<T...>;
166 };
167
168 #endif /* CEPH_MSG_STREAM_H_ */