summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_ggate/Server.h
blob: bb31b89f7afa89f31acec85d9f7cdf0ff542ced5 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_RBD_GGATE_SERVER_H
#define CEPH_RBD_GGATE_SERVER_H

#include "include/rbd/librbd.hpp"
#include "include/xlist.h"
#include "common/ceph_mutex.h"
#include "common/Thread.h"

namespace rbd {
namespace ggate {

class Driver;
struct Request;

class Server {
public:
  Server(Driver *drv, librbd::Image& image);

  void run();

private:
  struct IOContext {
    xlist<IOContext*>::item item;
    Server *server;
    Request *req = nullptr;

    IOContext(Server *server) : item(this), server(server) {
    }
  };

  class ThreadHelper : public Thread {
  public:
    typedef void (Server::*entry_func)();

    ThreadHelper(Server *server, entry_func func)
      : server(server), func(func) {
    }

  protected:
    virtual void* entry() {
      (server->*func)();
      return nullptr;
    }

  private:
    Server *server;
    entry_func func;
  };

  friend std::ostream &operator<<(std::ostream &os, const IOContext &ctx);

  Driver *m_drv;
  librbd::Image &m_image;

  mutable ceph::mutex m_lock =
    ceph::make_mutex("rbd::ggate::Server::m_lock");
  ceph::condition_variable m_cond;
  bool m_stopping = false;
  ThreadHelper m_reader_thread, m_writer_thread;
  xlist<IOContext*> m_io_pending;
  xlist<IOContext*> m_io_finished;

  static void aio_callback(librbd::completion_t cb, void *arg);

  int start();
  void stop();

  void reader_entry();
  void writer_entry();

  void io_start(IOContext *ctx);
  void io_finish(IOContext *ctx);

  IOContext *wait_io_finish();
  void wait_clean();

  void handle_aio(IOContext *ctx, int r);
};

std::ostream &operator<<(std::ostream &os, const Server::IOContext &ctx);

} // namespace ggate
} // namespace rbd

#endif // CEPH_RBD_GGATE_SERVER_H