From 57841074b922cb48260b094721bcc690a5c5cd27 Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Wed, 16 Apr 2025 21:29:48 +0300 Subject: [PATCH] Avoid sleep on service thread sync --- src/3proxy.c | 8 ++++++++ src/common.c | 3 ++- src/conf.c | 11 +++++++++-- src/proxymain.c | 15 ++++++++++++--- src/structures.h | 7 ++++++- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/3proxy.c b/src/3proxy.c index 9082f50..70b1bbb 100644 --- a/src/3proxy.c +++ b/src/3proxy.c @@ -521,6 +521,14 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int #ifndef NORADIUS pthread_mutex_init(&rad_mutex, NULL); #endif +#ifdef _WIN32 + if(!CreatePipe(&conf.threadinit[0], &conf.threadinit[1], NULL, 1)){ +#else + if(pipe(conf.threadinit)) { +#endif + fprintf(stderr, "CreatePipe failed\n"); + return 1; + }; freeconf(&conf); res = readconfig(fp); diff --git a/src/common.c b/src/common.c index ec25eb6..abffdf2 100644 --- a/src/common.c +++ b/src/common.c @@ -93,6 +93,7 @@ char *rotations[] = { struct extparam conf = { + {0, 0}, {1, 5, 30, 60, 180, 1800, 15, 60, 15, 5, 0, 0}, NULL, NULL, @@ -101,7 +102,7 @@ struct extparam conf = { NULL, NULL, 0, - 0, -1, 0, 0, 0, 0, + -1, 0, 0, 0, 0, 0, 500, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 600, diff --git a/src/conf.c b/src/conf.c index 2a39aad..39219ac 100644 --- a/src/conf.c +++ b/src/conf.c @@ -150,8 +150,8 @@ int start_proxy_thread(struct child * chp){ #ifdef _WIN32 HANDLE h; #endif + char r[1]; - conf.threadinit = 1; #ifdef _WIN32 #ifndef _WINCE h = (HANDLE)_beginthreadex((LPSECURITY_ATTRIBUTES )NULL, 16384+conf.stacksize, (void *)startsrv, (void *) chp, (DWORD)0, &thread); @@ -166,7 +166,14 @@ int start_proxy_thread(struct child * chp){ pthread_create(&thread, &pa, startsrv, (void *)chp); pthread_attr_destroy(&pa); #endif - while(conf.threadinit)usleep(SLEEPTIME); +#ifdef _WIN32 + ReadFile(conf.threadinit[0], r, 1, NULL, NULL); +#else + while(read(conf.threadinit[0], r, 1) !=1) if(errno != EINTR) { + fprintf(stderr, "pipe failed\n"); + return 40; + } +#endif if(haveerror) { fprintf(stderr, "Service not started on line: %d%s\n", linenum, haveerror == 2? ": insufficient memory":""); return(40); diff --git a/src/proxymain.c b/src/proxymain.c index 09828b9..2e11e97 100644 --- a/src/proxymain.c +++ b/src/proxymain.c @@ -115,6 +115,15 @@ void * threadfunc (void *p) { } #undef param +int pushthreadinit(){ + return +#ifdef _WIN32 + WriteFile(conf.threadinit[1], "1", 1, NULL, NULL); +#else + write(conf.threadinit[1], "1", 1); +#endif +} + struct socketoptions sockopts[] = { #ifdef TCP_NODELAY @@ -526,7 +535,7 @@ int MODULEMAINFUNC (int argc, char** argv){ if (error || i!=argc) { #ifndef STDMAIN haveerror = 1; - conf.threadinit = 0; + pushthreadinit(); #endif fprintf(stderr, "%s of %s\n" "Usage: %s options\n" @@ -558,7 +567,7 @@ int MODULEMAINFUNC (int argc, char** argv){ if (error || argc != i+3 || *argv[i]=='-'|| (*SAPORT(&srv.intsa) = htons((unsigned short)atoi(argv[i])))==0 || (srv.targetport = htons((unsigned short)atoi(argv[i+2])))==0) { #ifndef STDMAIN haveerror = 1; - conf.threadinit = 0; + pushthreadinit(); #endif fprintf(stderr, "%s of %s\n" "Usage: %s options" @@ -624,7 +633,7 @@ int MODULEMAINFUNC (int argc, char** argv){ #ifndef STDMAIN copyfilter(conf.filters, &srv); - conf.threadinit = 0; + pushthreadinit(); #endif diff --git a/src/structures.h b/src/structures.h index 6f175f7..786d301 100644 --- a/src/structures.h +++ b/src/structures.h @@ -632,6 +632,11 @@ struct filemon { struct extparam { +#ifdef _WIN32 + HANDLE threadinit[2]; +#else + int threadinit[2]; +#endif int timeouts[12]; struct ace * acl; char * conffile; @@ -640,7 +645,7 @@ struct extparam { struct trafcount * trafcounter; struct srvparam *services; int stacksize, - threadinit, counterd, haveerror, rotate, paused, archiverc, + counterd, haveerror, rotate, paused, archiverc, demon, maxchild, backlog, needreload, timetoexit, version, noforce, bandlimver, parentretries; int authcachetype, authcachetime; int filtermaxsize;