Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_ggate / Server.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RBD_GGATE_SERVER_H
5 #define CEPH_RBD_GGATE_SERVER_H
6
7 #include "include/rbd/librbd.hpp"
8 #include "include/xlist.h"
9 #include "common/Cond.h"
10 #include "common/Mutex.h"
11 #include "common/Thread.h"
12
13 namespace rbd {
14 namespace ggate {
15
16 class Driver;
17 struct Request;
18
19 class Server {
20 public:
21   Server(Driver *drv, librbd::Image& image);
22
23   void run();
24
25 private:
26   struct IOContext {
27     xlist<IOContext*>::item item;
28     Server *server;
29     Request *req = nullptr;
30
31     IOContext(Server *server) : item(this), server(server) {
32     }
33   };
34
35   class ThreadHelper : public Thread {
36   public:
37     typedef void (Server::*entry_func)();
38
39     ThreadHelper(Server *server, entry_func func)
40       : server(server), func(func) {
41     }
42
43   protected:
44     virtual void* entry() {
45       (server->*func)();
46       return nullptr;
47     }
48
49   private:
50     Server *server;
51     entry_func func;
52   };
53
54   friend std::ostream &operator<<(std::ostream &os, const IOContext &ctx);
55
56   Driver *m_drv;
57   librbd::Image &m_image;
58
59   mutable Mutex m_lock;
60   Cond m_cond;
61   bool m_stopping = false;
62   ThreadHelper m_reader_thread, m_writer_thread;
63   xlist<IOContext*> m_io_pending;
64   xlist<IOContext*> m_io_finished;
65
66   static void aio_callback(librbd::completion_t cb, void *arg);
67
68   int start();
69   void stop();
70
71   void reader_entry();
72   void writer_entry();
73
74   void io_start(IOContext *ctx);
75   void io_finish(IOContext *ctx);
76
77   IOContext *wait_io_finish();
78   void wait_clean();
79
80   void handle_aio(IOContext *ctx, int r);
81 };
82
83 std::ostream &operator<<(std::ostream &os, const Server::IOContext &ctx);
84
85 } // namespace ggate
86 } // namespace rbd
87
88 #endif // CEPH_RBD_GGATE_SERVER_H