@@ -30,6 +30,38 @@ using namespace std::chrono_literals;
3030#include " ghc/fs_std.hpp"
3131#include " linenoise.hpp"
3232
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+
3365#if not(defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY)
3466#define _DEFER (code, line ) std::shared_ptr<void > _defer_##line(nullptr , [&](auto ) { \
3567 code; \
@@ -699,7 +731,7 @@ int main(int narg, const char** args) {
699731 }
700732 std::list<std::future<std::string>> results;
701733 for (const auto & file : files) {
702- auto task = std:: async( std::launch::async, [=]() {
734+ auto task = async< std::string>( [=]() {
703735#ifndef YUE_COMPILER_ONLY
704736 return compileFile (fs::absolute (file.first ), config, fullWorkPath, fullTargetPath, minify, rewrite);
705737#else
@@ -737,7 +769,7 @@ int main(int narg, const char** args) {
737769#endif // YUE_NO_WATCHER
738770 std::list<std::future<std::tuple<int , std::string, std::string>>> results;
739771 for (const auto & file : files) {
740- auto task = std::async ( std::launch::async, [=]() {
772+ auto task = async< std::tuple< int , std::string, std::string>>( [=]() {
741773 std::ifstream input (file.first , std::ios::in);
742774 if (input) {
743775 std::string s (
0 commit comments