Removed the close(connfd) call since the socket has already been closed
from within the handle_connection() function. Added tests to make sure pthread_create() succeeds. Added defined tests for pthread_cancel() since it's not available on all platforms.
This commit is contained in:
parent
44bbdb2623
commit
42098699db
41
src/thread.c
41
src/thread.c
@ -1,4 +1,4 @@
|
|||||||
/* $Id: thread.c,v 1.23 2002-04-09 00:37:43 rjkaes Exp $
|
/* $Id: thread.c,v 1.24 2002-04-17 20:54:26 rjkaes Exp $
|
||||||
*
|
*
|
||||||
* Handles the creation/destruction of the various threads required for
|
* Handles the creation/destruction of the various threads required for
|
||||||
* processing incoming connections.
|
* processing incoming connections.
|
||||||
@ -118,8 +118,10 @@ thread_main(void *arg)
|
|||||||
socklen_t clilen;
|
socklen_t clilen;
|
||||||
struct thread_s *ptr;
|
struct thread_s *ptr;
|
||||||
|
|
||||||
|
#ifdef HAVE_PTHREAD_CANCEL
|
||||||
/* Set the cancelation type to immediate. */
|
/* Set the cancelation type to immediate. */
|
||||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
||||||
|
#endif
|
||||||
|
|
||||||
ptr = (struct thread_s *) arg;
|
ptr = (struct thread_s *) arg;
|
||||||
|
|
||||||
@ -164,7 +166,6 @@ thread_main(void *arg)
|
|||||||
SERVER_DEC();
|
SERVER_DEC();
|
||||||
|
|
||||||
handle_connection(connfd);
|
handle_connection(connfd);
|
||||||
close(connfd);
|
|
||||||
|
|
||||||
if (thread_config.maxrequestsperchild != 0) {
|
if (thread_config.maxrequestsperchild != 0) {
|
||||||
ptr->connects++;
|
ptr->connects++;
|
||||||
@ -216,6 +217,7 @@ short int
|
|||||||
thread_pool_create(void)
|
thread_pool_create(void)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
int pthread_ret;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize thread_attr to contain a non-default stack size
|
* Initialize thread_attr to contain a non-default stack size
|
||||||
@ -252,9 +254,15 @@ thread_pool_create(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < thread_config.startservers; i++) {
|
for (i = 0; i < thread_config.startservers; i++) {
|
||||||
thread_ptr[i].status = T_WAITING;
|
pthread_ret = pthread_create(&thread_ptr[i].tid, &thread_attr,
|
||||||
pthread_create(&thread_ptr[i].tid, &thread_attr, &thread_main,
|
&thread_main, &thread_ptr[i]);
|
||||||
&thread_ptr[i]);
|
if (pthread_ret < 0) {
|
||||||
|
log_message(LOG_WARNING, "Could not create thread number %d of %d: %s",
|
||||||
|
i, thread_config.startservers, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
thread_ptr[i].status = T_WAITING;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
servers_waiting = thread_config.startservers;
|
servers_waiting = thread_config.startservers;
|
||||||
|
|
||||||
@ -274,6 +282,7 @@ void
|
|||||||
thread_main_loop(void)
|
thread_main_loop(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int pthread_ret;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (config.quit)
|
if (config.quit)
|
||||||
@ -286,10 +295,17 @@ thread_main_loop(void)
|
|||||||
|
|
||||||
for (i = 0; i < thread_config.maxclients; i++) {
|
for (i = 0; i < thread_config.maxclients; i++) {
|
||||||
if (thread_ptr[i].status == T_EMPTY) {
|
if (thread_ptr[i].status == T_EMPTY) {
|
||||||
pthread_create(&thread_ptr[i].tid,
|
pthread_ret = pthread_create(&thread_ptr[i].tid,
|
||||||
&thread_attr,
|
&thread_attr,
|
||||||
&thread_main,
|
&thread_main,
|
||||||
&thread_ptr[i]);
|
&thread_ptr[i]);
|
||||||
|
if (pthread_ret < 0) {
|
||||||
|
log_message(LOG_NOTICE,
|
||||||
|
"Could not create thread: %s",
|
||||||
|
strerror(errno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
thread_ptr[i].status = T_WAITING;
|
thread_ptr[i].status = T_WAITING;
|
||||||
thread_ptr[i].connects = 0;
|
thread_ptr[i].connects = 0;
|
||||||
|
|
||||||
@ -311,6 +327,7 @@ thread_main_loop(void)
|
|||||||
/*
|
/*
|
||||||
* Go through all the non-empty threads and cancel them.
|
* Go through all the non-empty threads and cancel them.
|
||||||
*/
|
*/
|
||||||
|
#ifdef HAVE_PTHREAD_CANCEL
|
||||||
void
|
void
|
||||||
thread_kill_threads(void)
|
thread_kill_threads(void)
|
||||||
{
|
{
|
||||||
@ -321,6 +338,12 @@ thread_kill_threads(void)
|
|||||||
pthread_cancel(thread_ptr[i].tid);
|
pthread_cancel(thread_ptr[i].tid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
void
|
||||||
|
thread_kill_threads(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int
|
int
|
||||||
thread_listening_sock(uint16_t port)
|
thread_listening_sock(uint16_t port)
|
||||||
|
Loading…
Reference in New Issue
Block a user