Skip to content

Commit 5784107

Browse files
committed
Avoid sleep on service thread sync
1 parent 7320094 commit 5784107

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

src/3proxy.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,14 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int
521521
#ifndef NORADIUS
522522
pthread_mutex_init(&rad_mutex, NULL);
523523
#endif
524+
#ifdef _WIN32
525+
if(!CreatePipe(&conf.threadinit[0], &conf.threadinit[1], NULL, 1)){
526+
#else
527+
if(pipe(conf.threadinit)) {
528+
#endif
529+
fprintf(stderr, "CreatePipe failed\n");
530+
return 1;
531+
};
524532

525533
freeconf(&conf);
526534
res = readconfig(fp);

src/common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ char *rotations[] = {
9393

9494

9595
struct extparam conf = {
96+
{0, 0},
9697
{1, 5, 30, 60, 180, 1800, 15, 60, 15, 5, 0, 0},
9798
NULL,
9899
NULL,
@@ -101,7 +102,7 @@ struct extparam conf = {
101102
NULL,
102103
NULL,
103104
0,
104-
0, -1, 0, 0, 0, 0,
105+
-1, 0, 0, 0, 0,
105106
0, 500, 0, 0, 0, 0, 0, 0, 2,
106107
0, 0, 0,
107108
6, 600,

src/conf.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ int start_proxy_thread(struct child * chp){
150150
#ifdef _WIN32
151151
HANDLE h;
152152
#endif
153+
char r[1];
153154

154-
conf.threadinit = 1;
155155
#ifdef _WIN32
156156
#ifndef _WINCE
157157
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){
166166
pthread_create(&thread, &pa, startsrv, (void *)chp);
167167
pthread_attr_destroy(&pa);
168168
#endif
169-
while(conf.threadinit)usleep(SLEEPTIME);
169+
#ifdef _WIN32
170+
ReadFile(conf.threadinit[0], r, 1, NULL, NULL);
171+
#else
172+
while(read(conf.threadinit[0], r, 1) !=1) if(errno != EINTR) {
173+
fprintf(stderr, "pipe failed\n");
174+
return 40;
175+
}
176+
#endif
170177
if(haveerror) {
171178
fprintf(stderr, "Service not started on line: %d%s\n", linenum, haveerror == 2? ": insufficient memory":"");
172179
return(40);

src/proxymain.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ void * threadfunc (void *p) {
115115
}
116116
#undef param
117117

118+
int pushthreadinit(){
119+
return
120+
#ifdef _WIN32
121+
WriteFile(conf.threadinit[1], "1", 1, NULL, NULL);
122+
#else
123+
write(conf.threadinit[1], "1", 1);
124+
#endif
125+
}
126+
118127

119128
struct socketoptions sockopts[] = {
120129
#ifdef TCP_NODELAY
@@ -526,7 +535,7 @@ int MODULEMAINFUNC (int argc, char** argv){
526535
if (error || i!=argc) {
527536
#ifndef STDMAIN
528537
haveerror = 1;
529-
conf.threadinit = 0;
538+
pushthreadinit();
530539
#endif
531540
fprintf(stderr, "%s of %s\n"
532541
"Usage: %s options\n"
@@ -558,7 +567,7 @@ int MODULEMAINFUNC (int argc, char** argv){
558567
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) {
559568
#ifndef STDMAIN
560569
haveerror = 1;
561-
conf.threadinit = 0;
570+
pushthreadinit();
562571
#endif
563572
fprintf(stderr, "%s of %s\n"
564573
"Usage: %s options"
@@ -624,7 +633,7 @@ int MODULEMAINFUNC (int argc, char** argv){
624633
#ifndef STDMAIN
625634

626635
copyfilter(conf.filters, &srv);
627-
conf.threadinit = 0;
636+
pushthreadinit();
628637

629638

630639
#endif

src/structures.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,11 @@ struct filemon {
632632

633633

634634
struct extparam {
635+
#ifdef _WIN32
636+
HANDLE threadinit[2];
637+
#else
638+
int threadinit[2];
639+
#endif
635640
int timeouts[12];
636641
struct ace * acl;
637642
char * conffile;
@@ -640,7 +645,7 @@ struct extparam {
640645
struct trafcount * trafcounter;
641646
struct srvparam *services;
642647
int stacksize,
643-
threadinit, counterd, haveerror, rotate, paused, archiverc,
648+
counterd, haveerror, rotate, paused, archiverc,
644649
demon, maxchild, backlog, needreload, timetoexit, version, noforce, bandlimver, parentretries;
645650
int authcachetype, authcachetime;
646651
int filtermaxsize;

0 commit comments

Comments
 (0)