forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutorService.h
More file actions
132 lines (108 loc) · 4.03 KB
/
Copy pathExecutorService.h
File metadata and controls
132 lines (108 loc) · 4.03 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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef _PULSAR_EXECUTOR_SERVICE_HEADER_
#define _PULSAR_EXECUTOR_SERVICE_HEADER_
#include <pulsar/defines.h>
#include <atomic>
#ifdef USE_ASIO
#include <asio/dispatch.hpp>
#include <asio/io_context.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/post.hpp>
#include <asio/ssl.hpp>
#else
#include <boost/asio/dispatch.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/ssl.hpp>
#endif
#include <chrono>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <utility>
#include "AsioTimer.h"
namespace pulsar {
typedef std::shared_ptr<ASIO::ip::tcp::socket> SocketPtr;
typedef std::shared_ptr<ASIO::ssl::stream<ASIO::ip::tcp::socket &> > TlsSocketPtr;
typedef std::shared_ptr<ASIO::ip::tcp::resolver> TcpResolverPtr;
class PULSAR_PUBLIC ExecutorService : public std::enable_shared_from_this<ExecutorService> {
public:
using IOService = ASIO::io_context;
using SharedPtr = std::shared_ptr<ExecutorService>;
static SharedPtr create();
~ExecutorService();
ExecutorService(const ExecutorService &) = delete;
ExecutorService &operator=(const ExecutorService &) = delete;
// throws std::runtime_error if failed
SocketPtr createSocket();
static TlsSocketPtr createTlsSocket(SocketPtr &socket, ASIO::ssl::context &ctx);
// throws std::runtime_error if failed
TcpResolverPtr createTcpResolver();
// throws std::runtime_error if failed
DeadlineTimerPtr createDeadlineTimer();
// Execute the task in the event loop thread asynchronously, i.e. the task will be put in the event loop
// queue and executed later.
template <typename T>
void postWork(T &&task) {
ASIO::post(io_context_, std::forward<T>(task));
}
// Different from `postWork`, if it's already in the event loop, execute the task immediately
template <typename T>
void dispatch(T &&task) {
ASIO::dispatch(io_context_, std::forward<T>(task));
}
// See TimeoutProcessor for the semantics of the parameter.
void close(long timeoutMs = 3000);
IOService &getIOService() { return io_context_; }
bool isClosed() const noexcept { return closed_; }
private:
/*
* io_context is our interface to os, io object schedule async ops on this object
*/
IOService io_context_;
std::atomic_bool closed_{false};
std::mutex mutex_;
std::condition_variable cond_;
bool ioServiceDone_{false};
ExecutorService();
void start();
void restart();
};
using ExecutorServicePtr = ExecutorService::SharedPtr;
class PULSAR_PUBLIC ExecutorServiceProvider {
public:
explicit ExecutorServiceProvider(int nthreads);
ExecutorServicePtr get() { return get(executorIdx_++); }
ExecutorServicePtr get(size_t index);
// See TimeoutProcessor for the semantics of the parameter.
void close(long timeoutMs = 3000);
private:
typedef std::vector<ExecutorServicePtr> ExecutorList;
ExecutorList executors_;
std::atomic_size_t executorIdx_;
std::mutex mutex_;
typedef std::unique_lock<std::mutex> Lock;
};
typedef std::shared_ptr<ExecutorServiceProvider> ExecutorServiceProviderPtr;
} // namespace pulsar
#endif //_PULSAR_EXECUTOR_SERVICE_HEADER_