8 #ifndef INCLUDED_ORCUS_DETAIL_THREAD_PARSER_TOKEN_BUFFER_HPP 9 #define INCLUDED_ORCUS_DETAIL_THREAD_PARSER_TOKEN_BUFFER_HPP 11 #include "orcus/exception.hpp" 14 #include <condition_variable> 16 namespace orcus {
namespace detail {
namespace thread {
22 template<
typename _TokensT>
25 enum class state_type { parsing_progress, parsing_ended, parsing_aborted };
27 typedef _TokensT tokens_type;
29 mutable std::mutex m_mtx_tokens;
30 std::condition_variable m_cv_tokens_empty;
31 std::condition_variable m_cv_tokens_ready;
35 size_t m_token_size_threshold;
36 const size_t m_max_token_size;
40 bool tokens_empty()
const 42 std::lock_guard<std::mutex> lock(m_mtx_tokens);
43 return m_tokens.empty();
52 void wait_until_tokens_empty()
54 std::unique_lock<std::mutex> lock(m_mtx_tokens);
55 while (!m_tokens.empty() && m_state == state_type::parsing_progress)
56 m_cv_tokens_empty.wait(lock);
58 if (m_state == state_type::parsing_aborted)
65 m_token_size_threshold(std::max<size_t>(min_token_size, 1)),
66 m_max_token_size(max_token_size),
67 m_state(state_type::parsing_progress)
69 if (m_token_size_threshold > m_max_token_size)
71 "initial token size threshold is already larger than the max token size.");
84 if (parser_tokens.size() < m_token_size_threshold)
90 if (m_token_size_threshold < (m_max_token_size/2))
93 m_token_size_threshold *= 2;
99 wait_until_tokens_empty();
102 std::unique_lock<std::mutex> lock(m_mtx_tokens);
103 m_tokens.swap(parser_tokens);
105 m_cv_tokens_ready.notify_one();
119 wait_until_tokens_empty();
122 std::lock_guard<std::mutex> lock(m_mtx_tokens);
123 m_tokens.swap(parser_tokens);
124 m_state = state_type::parsing_ended;
126 m_cv_tokens_ready.notify_one();
132 std::lock_guard<std::mutex> lock(m_mtx_tokens);
134 m_state = state_type::parsing_aborted;
136 m_cv_tokens_empty.notify_one();
154 std::unique_lock<std::mutex> lock(m_mtx_tokens);
155 while (m_tokens.empty() && m_state == state_type::parsing_progress)
156 m_cv_tokens_ready.wait(lock);
160 state_type parsing_progress = m_state;
164 m_cv_tokens_empty.notify_one();
166 return parsing_progress == state_type::parsing_progress;
177 if (m_state == state_type::parsing_progress)
180 return m_token_size_threshold;
Definition: parser_token_buffer.hpp:23
void check_and_notify(tokens_type &parser_tokens)
Definition: parser_token_buffer.hpp:82
size_t token_size_threshold() const
Definition: parser_token_buffer.hpp:175
Definition: tokens.hpp:21
Definition: exception.hpp:33
Definition: exception.hpp:84
Definition: base64.hpp:15
void notify_and_finish(tokens_type &parser_tokens)
Definition: parser_token_buffer.hpp:116
bool next_tokens(tokens_type &tokens)
Definition: parser_token_buffer.hpp:149