Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / PosixStack.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2016 XSKY <haomai@xsky.com>
7  *
8  * Author: Haomai Wang <haomaiwang@gmail.com>
9  *
10  * This is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License version 2.1, as published by the Free Software
13  * Foundation.  See file COPYING.
14  *
15  */
16
17 #ifndef CEPH_MSG_ASYNC_POSIXSTACK_H
18 #define CEPH_MSG_ASYNC_POSIXSTACK_H
19
20 #include <thread>
21
22 #include "msg/msg_types.h"
23 #include "msg/async/net_handler.h"
24
25 #include "Stack.h"
26
27 class PosixWorker : public Worker {
28   NetHandler net;
29   void initialize() override;
30  public:
31   PosixWorker(CephContext *c, unsigned i)
32       : Worker(c, i), net(c) {}
33   int listen(entity_addr_t &sa, const SocketOptions &opt,
34                      ServerSocket *socks) override;
35   int connect(const entity_addr_t &addr, const SocketOptions &opts, ConnectedSocket *socket) override;
36 };
37
38 class PosixNetworkStack : public NetworkStack {
39   vector<int> coreids;
40   vector<std::thread> threads;
41
42  public:
43   explicit PosixNetworkStack(CephContext *c, const string &t);
44
45   int get_cpuid(int id) const {
46     if (coreids.empty())
47       return -1;
48     return coreids[id % coreids.size()];
49   }
50   void spawn_worker(unsigned i, std::function<void ()> &&func) override {
51     threads.resize(i+1);
52     threads[i] = std::thread(func);
53   }
54   void join_worker(unsigned i) override {
55     assert(threads.size() > i && threads[i].joinable());
56     threads[i].join();
57   }
58 };
59
60 #endif //CEPH_MSG_ASYNC_POSIXSTACK_H