From a807827db87b811d1a14dfdc06b53e9a8a466f1e Mon Sep 17 00:00:00 2001 From: mingodad Date: Thu, 22 Jun 2023 14:48:14 +0200 Subject: [PATCH] Add preprocessor guards to allow build without threads/threadpool. --- src/lalr/GrammarGenerator.cpp | 16 +++++++++++++++- src/lalr/GrammarGenerator.hpp | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lalr/GrammarGenerator.cpp b/src/lalr/GrammarGenerator.cpp index b730ade..67c10f1 100644 --- a/src/lalr/GrammarGenerator.cpp +++ b/src/lalr/GrammarGenerator.cpp @@ -21,7 +21,9 @@ #include "ParserStateMachine.hpp" #include "RegexGenerator.hpp" #include "RegexCompiler.hpp" +#ifndef LALR_NO_THREADS #include "ThreadPool.hpp" +#endif #include "ErrorPolicy.hpp" #include "ErrorCode.hpp" #include "assert.hpp" @@ -49,7 +51,9 @@ using namespace lalr; */ GrammarGenerator::GrammarGenerator() : error_policy_( nullptr ) +#ifndef LALR_NO_THREADS , thread_pool_( nullptr ) +#endif , identifier_() , actions_() , productions_() @@ -64,14 +68,18 @@ GrammarGenerator::GrammarGenerator() , start_state_( nullptr ) , errors_( 0 ) { +#ifndef LALR_NO_THREADS thread_pool_ = new ThreadPool; thread_pool_->start( 8 ); +#endif } GrammarGenerator::~GrammarGenerator() { +#ifndef LALR_NO_THREADS delete thread_pool_; thread_pool_ = nullptr; +#endif } const std::vector>& GrammarGenerator::actions() const @@ -756,7 +764,9 @@ void GrammarGenerator::generate_goto_items() { for ( const shared_ptr& state : states_ ) { +#ifndef LALR_NO_THREADS thread_pool_->push_job( [this, state]() +#endif { for ( GrammarTransition* transition : state->transitions() ) { @@ -799,9 +809,13 @@ void GrammarGenerator::generate_goto_items() } } } +#ifndef LALR_NO_THREADS ); +#endif } - thread_pool_->wait_idle(); +#ifndef LALR_NO_THREADS + thread_pool_->wait_idle(); +#endif } /** diff --git a/src/lalr/GrammarGenerator.hpp b/src/lalr/GrammarGenerator.hpp index eb4112b..c03e58d 100644 --- a/src/lalr/GrammarGenerator.hpp +++ b/src/lalr/GrammarGenerator.hpp @@ -15,7 +15,9 @@ namespace lalr { class ErrorPolicy; +#ifndef LALR_NO_THREADS class ThreadPool; +#endif class LexerStateMachine; class GrammarCompiler; class GrammarAction; @@ -34,7 +36,9 @@ class Grammar; class GrammarGenerator { ErrorPolicy* error_policy_; ///< The event sink to report errors to and print with or null to ignore errors and prints. +#ifndef LALR_NO_THREADS ThreadPool* thread_pool_; ///< The pool of threads to use to generate the parser. +#endif std::string identifier_; ///< The identifier of the parser. std::vector> actions_; ///< The actions in the parser. std::vector> productions_; ///< The productions in the parser.