mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-16 15:41:16 +01:00
call accept() in main thread rather than in thread proc to fix
long-standing bug where main thread kept finding socket to be readable and forking new threads to call accept() on it and then block forever.
This commit is contained in:
parent
e20c63f38c
commit
90d505abea
1 changed files with 24 additions and 9 deletions
|
@ -197,14 +197,22 @@ printStats( FILE* fil, const CrefMgrInfo* info, bool isLocal )
|
||||||
fprintf( fil, "</table>" );
|
fprintf( fil, "</table>" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class HttpInstance {
|
||||||
|
public:
|
||||||
|
HttpInstance( int sock, HttpState* state ) {
|
||||||
|
m_sock = sock;
|
||||||
|
m_state = state;
|
||||||
|
}
|
||||||
|
int m_sock;
|
||||||
|
HttpState* m_state;
|
||||||
|
};
|
||||||
|
|
||||||
static void*
|
static void*
|
||||||
http_thread_main( void* arg )
|
http_thread_main( void* arg )
|
||||||
{
|
{
|
||||||
HttpState* state = (HttpState*)arg;
|
HttpInstance* inst = (HttpInstance*)arg;
|
||||||
|
HttpState* state = inst->m_state;
|
||||||
sockaddr newaddr;
|
int sock = inst->m_sock;
|
||||||
socklen_t siz = sizeof(newaddr);
|
|
||||||
int sock = accept( state->ctrl_sock, &newaddr, &siz );
|
|
||||||
|
|
||||||
char buf[512];
|
char buf[512];
|
||||||
ssize_t totalRead = 0;
|
ssize_t totalRead = 0;
|
||||||
|
@ -275,6 +283,8 @@ http_thread_main( void* arg )
|
||||||
}
|
}
|
||||||
close( sock );
|
close( sock );
|
||||||
|
|
||||||
|
delete inst;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
} /* http_thread_main */
|
} /* http_thread_main */
|
||||||
|
|
||||||
|
@ -282,13 +292,18 @@ void
|
||||||
run_http_thread( HttpState* state )
|
run_http_thread( HttpState* state )
|
||||||
{
|
{
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
int result = pthread_create( &thread, NULL,
|
sockaddr newaddr;
|
||||||
http_thread_main, (void*)state );
|
socklen_t siz = sizeof(newaddr);
|
||||||
|
int sock = accept( state->ctrl_sock, &newaddr, &siz );
|
||||||
|
|
||||||
|
HttpInstance* inst = new HttpInstance( sock, state );
|
||||||
|
int result = pthread_create( &thread, NULL, http_thread_main,
|
||||||
|
(void*)inst );
|
||||||
if ( 0 == result ) {
|
if ( 0 == result ) {
|
||||||
pthread_detach( thread );
|
pthread_detach( thread );
|
||||||
} else {
|
} else {
|
||||||
/* logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__, */
|
logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__,
|
||||||
/* strerror(errno) ); */
|
strerror(errno) );
|
||||||
}
|
}
|
||||||
} /* run_http_thread */
|
} /* run_http_thread */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue