Re: Linus on Linux, Apache and Threads

Olaf Titz (olaf@bigred.inka.de)
Sat, 24 Apr 1999 13:02:15 +0200


> how does the event waiter thread determine whether there is a
> suitable/idle worker thread to awaken?

like this?

struct Q {
struct Q *next;
pthread_t worker;
int fd;
pthread_cond_t sleep;
} *head, *tail;
int Qlen;
pthread_cond_t Qlow;
pthread_mutex_t QLOCK;

void workerthread() {
struct Q *this;
while (1) {
pthread_cond_wait(this->sleep);
/* work on fd here... */
this->next=NULL;
pthread_mutex_lock(QLOCK);
tail->next=this;
tail=this;
++Qlen;
pthread_mutex_unlock(QLOCK);
}
}

void qwatcherthread() {
struct Q *new;
while (1) {
pthread_cond_wait(Qlow);
new=create_new_worker();
new->next=NULL;
pthread_mutex_lock(QLOCK);
tail->next=new;
tail=new;
++Qlen;
pthread_mutex_unlock(QLOCK);
}
}

void acceptor() {
struct Q *work;
while (1) {
if (Qlen<min_workers)
pthread_cond_signal(Qlow);
newfd=accept(...);
pthread_mutex_lock(QLOCK);
if (head) {
--Qlen;
work=head;
head=head->next;
} else {
work=create_new_worker();
}
pthread_mutex_unlock(QLOCK);
work->fd=newfd;
pthtread_cond_signal(work->sleep);
}
}

(no, I don't claim this code is correct, but just to illustrate how it
could be done :-)

Olaf

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/