mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-15 15:41:24 +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>" );
|
||||
}
|
||||
|
||||
class HttpInstance {
|
||||
public:
|
||||
HttpInstance( int sock, HttpState* state ) {
|
||||
m_sock = sock;
|
||||
m_state = state;
|
||||
}
|
||||
int m_sock;
|
||||
HttpState* m_state;
|
||||
};
|
||||
|
||||
static void*
|
||||
http_thread_main( void* arg )
|
||||
{
|
||||
HttpState* state = (HttpState*)arg;
|
||||
|
||||
sockaddr newaddr;
|
||||
socklen_t siz = sizeof(newaddr);
|
||||
int sock = accept( state->ctrl_sock, &newaddr, &siz );
|
||||
HttpInstance* inst = (HttpInstance*)arg;
|
||||
HttpState* state = inst->m_state;
|
||||
int sock = inst->m_sock;
|
||||
|
||||
char buf[512];
|
||||
ssize_t totalRead = 0;
|
||||
|
@ -275,6 +283,8 @@ http_thread_main( void* arg )
|
|||
}
|
||||
close( sock );
|
||||
|
||||
delete inst;
|
||||
|
||||
return NULL;
|
||||
} /* http_thread_main */
|
||||
|
||||
|
@ -282,13 +292,18 @@ void
|
|||
run_http_thread( HttpState* state )
|
||||
{
|
||||
pthread_t thread;
|
||||
int result = pthread_create( &thread, NULL,
|
||||
http_thread_main, (void*)state );
|
||||
sockaddr newaddr;
|
||||
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 ) {
|
||||
pthread_detach( thread );
|
||||
} else {
|
||||
/* logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__, */
|
||||
/* strerror(errno) ); */
|
||||
logf( XW_LOGERROR, "%s: pthread_create failed: %s", __func__,
|
||||
strerror(errno) );
|
||||
}
|
||||
} /* run_http_thread */
|
||||
|
||||
|
|
Loading…
Reference in a new issue