HashList *HttpReqTypes = NULL;
HashList *HttpHeaderHandler = NULL;
extern HashList *HandlerHash;
-int num_threads = 1; /* Number of worker threads. Start at 1 because the parent counts. */
+
+/* the following two values start at 1 because the initial parent thread counts as one. */
+int num_threads_existing = 1; /* Number of worker threads which exist. */
+int num_threads_executing = 1; /* Number of worker threads currently executing. */
void DestroyHttpHeaderHandler(void *V)
{
{
wcsession *sptr, *ss;
wcsession *sessions_to_kill = NULL;
- int num_sessions = 0;
/*
* Lock the session list, moving any candidates for euthanasia into
* a separate list.
*/
pthread_mutex_lock(&SessionListMutex);
- num_sessions = 0;
for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
- ++num_sessions;
/* Kill idle sessions */
if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
session_destroy_modules(&sessions_to_kill);
sessions_to_kill = sptr;
- --num_sessions;
}
/*
- * If there are more sessions than threads, then we should spawn
- * more threads ... up to a predefined maximum.
+ * Check the size of our thread pool. If all threads are executing, spawn another.
*/
- while ( (num_sessions > num_threads) && (num_threads <= MAX_WORKER_THREADS) ) {
+ begin_critical_section(S_SPAWNER);
+ while (
+ (num_threads_executing >= num_threads_existing)
+ && (num_threads_existing <= MAX_WORKER_THREADS)
+ ) {
spawn_another_worker_thread();
- lprintf(3, "There are %d sessions and %d threads active.\n",
- num_sessions, num_threads);
}
+ end_critical_section(S_SPAWNER);
}
ssock = -1;
errno = EAGAIN;
do {
+ --num_threads_executing;
ssock = accept(msock, NULL, 0);
+ ++num_threads_executing;
lprintf(9, "Thread %u woke up, accept() returned %d %s\n",
- ((pthread_self() % 6) + 1),
pthread_self(),
ssock,
((ssock >= 0) ? "" : strerror(errno))
{/* ok, we're going down. */
int shutdown = 0;
- /* the first to come here will have to do the cleanup.
- * make shure its realy just one.
+ /* The first thread to get here will have to do the cleanup.
+ * Make sure it's really just one.
*/
begin_critical_section(S_SHUTDOWN);
if (msock == -1)
}
if (ssock < 0 ) continue;
+ /* Now do something. */
if (msock < 0) {
if (ssock > 0) close (ssock);
- lprintf(2, "inbetween.");
+ lprintf(2, "in between.");
pthread_exit(NULL);
- } else { /* Got it? do some real work! */
+ } else {
+ /* Got it? do some real work! */
/* Set the SO_REUSEADDR socket option */
i = 1;
- setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
- &i, sizeof(i));
+ setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
/* If we are an HTTPS server, go crypto now. */
#ifdef HAVE_OPENSSL
#endif
/* ...and close the socket. */
- if (Hdr.http_sock > 0)
+ if (Hdr.http_sock > 0) {
lingering_close(ssock);
+ }
http_detach_modules(&Hdr);
}
pthread_attr_t attr; /* Thread attributes */
int ret;
- lprintf(3, "Creating a new thread. Pool size is now %d\n", ++num_threads);
+ lprintf(3, "Creating a new thread.\n");
+
+ ++num_threads_existing;
+ ++num_threads_executing;
/* set attributes for the new thread */
pthread_attr_init(&attr);
#define DEFAULT_PORT "504"
#define TARGET "webcit01" /* Target for inline URL's */
#define HOUSEKEEPING 15 /* Housekeeping frequency */
-#define MIN_WORKER_THREADS 5
#define MAX_WORKER_THREADS 250
#define LISTEN_QUEUE_LENGTH 100 /* listen() backlog queue */
enum {
S_SHUTDOWN,
+ S_SPAWNER,
MAX_SEMAPHORES
};
extern int setup_wizard;
extern char wizard_filename[];
extern int follow_xff;
-extern int num_threads;
+extern int num_threads_existing;
+extern int num_threads_executing;
void InitialiseSemaphores(void);
void begin_critical_section(int which_one);
size_t basesize = 2; /* how big should strbufs be on creation? */
pthread_t SessThread; /* Thread descriptor */
pthread_attr_t attr; /* Thread attributes */
- int a, i; /* General-purpose variables */
+ int a; /* General-purpose variable */
char tracefile[PATH_MAX];
char ip_addr[256]="0.0.0.0";
int relh=0;
#endif
drop_root(UID);
- /* Start a few initial worker threads */
- for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
- spawn_another_worker_thread();
- }
-
- /* now the original thread becomes another worker */
+ /* Become a worker thread. More worker threads will be spawned as they are needed. */
worker_entry();
ShutDownLibCitadel ();
return 0;