Renamed the LOCKing macros and added assert debugging code. Also, moved

the mutex initialization into a function call so that I can use an
error checking mutex once I figure out how to get it to work on my
computer.
This commit is contained in:
Robert James Kaes 2002-04-22 19:41:17 +00:00
parent c648ed1bc5
commit c5b9ec889e
2 changed files with 46 additions and 23 deletions

View File

@ -1,5 +1,11 @@
2002-04-22 Robert James Kaes <rjkaes@flarenet.com> 2002-04-22 Robert James Kaes <rjkaes@flarenet.com>
* src/thread.c: Renamed the LOCKing macros and added debugging
asserts to them. Also, moved the mutex initialization into the
thread_pool_create() function since I would like to use an error
checking mutex, but my machine doesn't seem to work with it. I
left the code there in case I can get it to work later.
* src/log.c (send_stored_logs): Added this function since the * src/log.c (send_stored_logs): Added this function since the
log_message() function will now stored the messages if the config log_message() function will now stored the messages if the config
file has not been processed yet. This function is called from file has not been processed yet. This function is called from

View File

@ -1,4 +1,4 @@
/* $Id: thread.c,v 1.25 2002-04-18 16:57:06 rjkaes Exp $ /* $Id: thread.c,v 1.26 2002-04-22 19:41:17 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.
@ -47,7 +47,16 @@ struct thread_s {
* created when the program is started. * created when the program is started.
*/ */
static struct thread_s *thread_ptr; static struct thread_s *thread_ptr;
static pthread_mutex_t mlock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mlock;
#define ACCEPT_LOCK() do { \
int accept_lock_ret = pthread_mutex_lock(&mlock); \
assert(accept_lock_ret == 0); \
} while (0)
#define ACCEPT_UNLOCK() do { \
int accept_lock_ret = pthread_mutex_unlock(&mlock); \
assert(accept_lock_ret == 0); \
} while (0)
/* Used to override the default statck size. */ /* Used to override the default statck size. */
static pthread_attr_t thread_attr; static pthread_attr_t thread_attr;
@ -58,23 +67,29 @@ static struct thread_config_s {
} thread_config; } thread_config;
static unsigned int servers_waiting; /* servers waiting for a connection */ static unsigned int servers_waiting; /* servers waiting for a connection */
static pthread_mutex_t servers_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t servers_mutex;
#define SERVER_LOCK() pthread_mutex_lock(&servers_mutex) #define SERVER_COUNT_LOCK() do { \
#define SERVER_UNLOCK() pthread_mutex_unlock(&servers_mutex) int servers_mutex_ret = pthread_mutex_lock(&servers_mutex); \
assert(servers_mutex_ret == 0); \
} while (0)
#define SERVER_COUNT_UNLOCK() do { \
int servers_mutex_ret = pthread_mutex_unlock(&servers_mutex); \
assert(servers_mutex_ret == 0); \
} while (0)
#define SERVER_INC() do { \ #define SERVER_INC() do { \
SERVER_LOCK(); \ SERVER_COUNT_LOCK(); \
DEBUG2("INC: servers_waiting: %u", servers_waiting); \ DEBUG2("INC: servers_waiting: %u", servers_waiting); \
servers_waiting++; \ ++servers_waiting; \
SERVER_UNLOCK(); \ SERVER_COUNT_UNLOCK(); \
} while (0) } while (0)
#define SERVER_DEC() do { \ #define SERVER_DEC() do { \
SERVER_LOCK(); \ SERVER_COUNT_LOCK(); \
servers_waiting--; \ --servers_waiting; \
DEBUG2("DEC: servers_waiting: %u", servers_waiting); \ DEBUG2("DEC: servers_waiting: %u", servers_waiting); \
SERVER_UNLOCK(); \ SERVER_COUNT_UNLOCK(); \
} while (0) } while (0)
/* /*
@ -132,18 +147,15 @@ thread_main(void *arg)
while (!config.quit) { while (!config.quit) {
clilen = addrlen; clilen = addrlen;
pthread_mutex_lock(&mlock);
/* /*
* Check to see if the program is shutting down. * Check to see if the program is shutting down.
*/ */
if (config.quit) { if (config.quit)
pthread_mutex_unlock(&mlock);
break; break;
}
ACCEPT_LOCK();
connfd = accept(listenfd, cliaddr, &clilen); connfd = accept(listenfd, cliaddr, &clilen);
pthread_mutex_unlock(&mlock); ACCEPT_UNLOCK();
/* /*
* Make sure no error occurred... * Make sure no error occurred...
@ -184,13 +196,13 @@ thread_main(void *arg)
} }
} }
SERVER_LOCK(); SERVER_COUNT_LOCK();
if (servers_waiting >= thread_config.maxspareservers) { if (servers_waiting >= thread_config.maxspareservers) {
/* /*
* There are too many spare threads, kill ourself * There are too many spare threads, kill ourself
* off. * off.
*/ */
SERVER_UNLOCK(); SERVER_COUNT_UNLOCK();
log_message(LOG_NOTICE, log_message(LOG_NOTICE,
"Waiting servers exceeds MaxSpareServers. Killing thread."); "Waiting servers exceeds MaxSpareServers. Killing thread.");
@ -199,7 +211,7 @@ thread_main(void *arg)
break; break;
} }
SERVER_UNLOCK(); SERVER_COUNT_UNLOCK();
ptr->status = T_WAITING; ptr->status = T_WAITING;
@ -218,6 +230,7 @@ thread_pool_create(void)
{ {
unsigned int i; unsigned int i;
int pthread_ret; int pthread_ret;
// pthread_mutexattr_t mutexattr;
/* /*
* Initialize thread_attr to contain a non-default stack size * Initialize thread_attr to contain a non-default stack size
@ -229,6 +242,10 @@ thread_pool_create(void)
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&thread_attr, THREAD_STACK_SIZE); pthread_attr_setstacksize(&thread_attr, THREAD_STACK_SIZE);
// pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&mlock, NULL);
pthread_mutex_init(&servers_mutex, NULL);
if (thread_config.maxclients == 0) { if (thread_config.maxclients == 0) {
log_message(LOG_ERR, log_message(LOG_ERR,
"thread_pool_create: \"MaxClients\" must be greater than zero."); "thread_pool_create: \"MaxClients\" must be greater than zero.");
@ -289,9 +306,9 @@ thread_main_loop(void)
return; return;
/* If there are not enough spare servers, create more */ /* If there are not enough spare servers, create more */
SERVER_LOCK(); SERVER_COUNT_LOCK();
if (servers_waiting < thread_config.minspareservers) { if (servers_waiting < thread_config.minspareservers) {
SERVER_UNLOCK(); SERVER_COUNT_UNLOCK();
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) {
@ -318,7 +335,7 @@ thread_main_loop(void)
} }
} }
} }
SERVER_UNLOCK(); SERVER_COUNT_UNLOCK();
sleep(5); sleep(5);