Loading config.h.in +1 −0 Changes for config.h.in: 1 added line, 0 removed lines. Original line number Diff line number Diff line #define ${CMAKE_SYSTEM_NAME} #define ARCH_${CMAKE_SYSTEM_PROCESSOR} #define THREAD_STACK_SIZE (4 * 1024 * 1024) src/event/epoll.cpp +62 −14 Changes for src/event/epoll.cpp: 62 added lines, 14 removed lines. Original line number Diff line number Diff line Loading @@ -425,6 +425,29 @@ namespace netplus { } }; struct ThreadHelperArgs { EventWorkerArgs* eargs; ULONG_PTR args; int tid; }; void* worker_wrapper(void* data) { ThreadHelperArgs* helper_args = reinterpret_cast<ThreadHelperArgs*>(data); try { // Call the original worker function EventWorker(helper_args->tid, helper_args->args, helper_args->eargs); } catch (NetException& e) { std::cerr << "Thread " << helper_args->tid << " exited with exception: " << e.what() << std::endl; } catch (...) { std::cerr << "Thread " << helper_args->tid << " exited with unknown exception." << std::endl; } // Clean up allocated memory delete helper_args; return nullptr; } void eventapi::RequestEvent(con &curcon,const int tid,ULONG_PTR args){ //dummy }; Loading Loading @@ -484,7 +507,6 @@ namespace netplus { NetException exception; signal(SIGINT, signalHandler); signal(SIGPIPE, SIG_IGN); MAINWORKERLOOP: Loading @@ -495,9 +517,7 @@ MAINWORKERLOOP: throw exception; } struct epoll_event setevent ={ 0 }; struct epoll_event setevent = {0}; setevent.events = EPOLLIN; setevent.data.ptr = nullptr; Loading @@ -507,29 +527,57 @@ MAINWORKERLOOP: throw exception; } EventWorkerArgs eargs; eargs.ssocket=_ServerSocket; eargs.pollfd=_pollFD; eargs.timeout=_Timeout; eargs.event=this; std::vector <std::thread> threadpool; // --- FIX START: Set Thread Attributes for Stack Size --- pthread_attr_t attr; if (pthread_attr_init(&attr) != 0) { exception[NetException::Critical] << "runEventloop: Failed to initialize pthread attributes: " << strerror(errno); throw exception; } if (pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0) { exception[NetException::Critical] << "runEventloop: Failed to set pthread stack size: " << strerror(errno); pthread_attr_destroy(&attr); throw exception; } // --- FIX END --- // Use std::vector<pthread_t> to store thread handles std::vector <pthread_t> threadpool_handles; threadpool_handles.reserve(threads); for (int i = 0; i < threads; i++) { try { threadpool.emplace_back( std::thread([&eargs,args,i]{ EventWorker(i,args,&eargs); })); } catch (NetException& e) { throw e; // Allocate memory for thread arguments on the heap ThreadHelperArgs *helper_args = new ThreadHelperArgs{&eargs, args, i}; pthread_t thread_id; // Use pthread_create with the custom attributes and C-style wrapper int ret = pthread_create(&thread_id, &attr, worker_wrapper, helper_args); if (ret != 0) { delete helper_args; // Cleanup allocated arguments char errstr[255]; strerror_r(ret, errstr, 255); exception[NetException::Critical] << "runEventloop: Failed to create thread: " << errstr; pthread_attr_destroy(&attr); throw exception; } threadpool_handles.push_back(thread_id); } for(auto i = threadpool.begin(); i!=threadpool.end(); ++i){ i->join(); // Wait for all threads to finish using pthread_join for(auto i = threadpool_handles.begin(); i!=threadpool_handles.end(); ++i){ pthread_join(*i, nullptr); } // Cleanup thread attributes pthread_attr_destroy(&attr); close(_pollFD); if (event::_Restart) { Loading Loading
config.h.in +1 −0 Changes for config.h.in: 1 added line, 0 removed lines. Original line number Diff line number Diff line #define ${CMAKE_SYSTEM_NAME} #define ARCH_${CMAKE_SYSTEM_PROCESSOR} #define THREAD_STACK_SIZE (4 * 1024 * 1024)
src/event/epoll.cpp +62 −14 Changes for src/event/epoll.cpp: 62 added lines, 14 removed lines. Original line number Diff line number Diff line Loading @@ -425,6 +425,29 @@ namespace netplus { } }; struct ThreadHelperArgs { EventWorkerArgs* eargs; ULONG_PTR args; int tid; }; void* worker_wrapper(void* data) { ThreadHelperArgs* helper_args = reinterpret_cast<ThreadHelperArgs*>(data); try { // Call the original worker function EventWorker(helper_args->tid, helper_args->args, helper_args->eargs); } catch (NetException& e) { std::cerr << "Thread " << helper_args->tid << " exited with exception: " << e.what() << std::endl; } catch (...) { std::cerr << "Thread " << helper_args->tid << " exited with unknown exception." << std::endl; } // Clean up allocated memory delete helper_args; return nullptr; } void eventapi::RequestEvent(con &curcon,const int tid,ULONG_PTR args){ //dummy }; Loading Loading @@ -484,7 +507,6 @@ namespace netplus { NetException exception; signal(SIGINT, signalHandler); signal(SIGPIPE, SIG_IGN); MAINWORKERLOOP: Loading @@ -495,9 +517,7 @@ MAINWORKERLOOP: throw exception; } struct epoll_event setevent ={ 0 }; struct epoll_event setevent = {0}; setevent.events = EPOLLIN; setevent.data.ptr = nullptr; Loading @@ -507,29 +527,57 @@ MAINWORKERLOOP: throw exception; } EventWorkerArgs eargs; eargs.ssocket=_ServerSocket; eargs.pollfd=_pollFD; eargs.timeout=_Timeout; eargs.event=this; std::vector <std::thread> threadpool; // --- FIX START: Set Thread Attributes for Stack Size --- pthread_attr_t attr; if (pthread_attr_init(&attr) != 0) { exception[NetException::Critical] << "runEventloop: Failed to initialize pthread attributes: " << strerror(errno); throw exception; } if (pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0) { exception[NetException::Critical] << "runEventloop: Failed to set pthread stack size: " << strerror(errno); pthread_attr_destroy(&attr); throw exception; } // --- FIX END --- // Use std::vector<pthread_t> to store thread handles std::vector <pthread_t> threadpool_handles; threadpool_handles.reserve(threads); for (int i = 0; i < threads; i++) { try { threadpool.emplace_back( std::thread([&eargs,args,i]{ EventWorker(i,args,&eargs); })); } catch (NetException& e) { throw e; // Allocate memory for thread arguments on the heap ThreadHelperArgs *helper_args = new ThreadHelperArgs{&eargs, args, i}; pthread_t thread_id; // Use pthread_create with the custom attributes and C-style wrapper int ret = pthread_create(&thread_id, &attr, worker_wrapper, helper_args); if (ret != 0) { delete helper_args; // Cleanup allocated arguments char errstr[255]; strerror_r(ret, errstr, 255); exception[NetException::Critical] << "runEventloop: Failed to create thread: " << errstr; pthread_attr_destroy(&attr); throw exception; } threadpool_handles.push_back(thread_id); } for(auto i = threadpool.begin(); i!=threadpool.end(); ++i){ i->join(); // Wait for all threads to finish using pthread_join for(auto i = threadpool_handles.begin(); i!=threadpool_handles.end(); ++i){ pthread_join(*i, nullptr); } // Cleanup thread attributes pthread_attr_destroy(&attr); close(_pollFD); if (event::_Restart) { Loading