diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/yue.cpp | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/yue.cpp b/src/yue.cpp index fc57767..2c3a469 100644 --- a/src/yue.cpp +++ b/src/yue.cpp | |||
| @@ -30,6 +30,38 @@ using namespace std::chrono_literals; | |||
| 30 | #include "ghc/fs_std.hpp" | 30 | #include "ghc/fs_std.hpp" |
| 31 | #include "linenoise.hpp" | 31 | #include "linenoise.hpp" |
| 32 | 32 | ||
| 33 | #if __has_include(<pthread.h>) | ||
| 34 | #include <pthread.h> | ||
| 35 | template<class R> | ||
| 36 | std::future<R> async(const std::function<R()>& f) { | ||
| 37 | using Fn = std::packaged_task<R()>; | ||
| 38 | auto task = new Fn(f); | ||
| 39 | std::future<R> fut = task->get_future(); | ||
| 40 | |||
| 41 | pthread_attr_t attr; | ||
| 42 | pthread_attr_init(&attr); | ||
| 43 | pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); | ||
| 44 | |||
| 45 | pthread_t th; | ||
| 46 | pthread_create(&th, &attr, | ||
| 47 | [](void* p)->void* { | ||
| 48 | std::unique_ptr<Fn> fn(static_cast<Fn*>(p)); | ||
| 49 | (*fn)(); | ||
| 50 | return nullptr; | ||
| 51 | }, | ||
| 52 | task); | ||
| 53 | pthread_attr_destroy(&attr); | ||
| 54 | pthread_detach(th); | ||
| 55 | return fut; | ||
| 56 | } | ||
| 57 | #else | ||
| 58 | template<class R> | ||
| 59 | std::future<R> async(const std::function<R()>& f) { | ||
| 60 | // fallback: ignore stack size | ||
| 61 | return std::async(std::launch::async, f); | ||
| 62 | } | ||
| 63 | #endif | ||
| 64 | |||
| 33 | #if not(defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY) | 65 | #if not(defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY) |
| 34 | #define _DEFER(code, line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto) { \ | 66 | #define _DEFER(code, line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto) { \ |
| 35 | code; \ | 67 | code; \ |
| @@ -699,7 +731,7 @@ int main(int narg, const char** args) { | |||
| 699 | } | 731 | } |
| 700 | std::list<std::future<std::string>> results; | 732 | std::list<std::future<std::string>> results; |
| 701 | for (const auto& file : files) { | 733 | for (const auto& file : files) { |
| 702 | auto task = std::async(std::launch::async, [=]() { | 734 | auto task = async<std::string>([=]() { |
| 703 | #ifndef YUE_COMPILER_ONLY | 735 | #ifndef YUE_COMPILER_ONLY |
| 704 | return compileFile(fs::absolute(file.first), config, fullWorkPath, fullTargetPath, minify, rewrite); | 736 | return compileFile(fs::absolute(file.first), config, fullWorkPath, fullTargetPath, minify, rewrite); |
| 705 | #else | 737 | #else |
| @@ -737,7 +769,7 @@ int main(int narg, const char** args) { | |||
| 737 | #endif // YUE_NO_WATCHER | 769 | #endif // YUE_NO_WATCHER |
| 738 | std::list<std::future<std::tuple<int, std::string, std::string>>> results; | 770 | std::list<std::future<std::tuple<int, std::string, std::string>>> results; |
| 739 | for (const auto& file : files) { | 771 | for (const auto& file : files) { |
| 740 | auto task = std::async(std::launch::async, [=]() { | 772 | auto task = async<std::tuple<int, std::string, std::string>>([=]() { |
| 741 | std::ifstream input(file.first, std::ios::in); | 773 | std::ifstream input(file.first, std::ios::in); |
| 742 | if (input) { | 774 | if (input) { |
| 743 | std::string s( | 775 | std::string s( |
