From 6cce23349147a2b8d4310d1ea9cb9e46da84c7e3 Mon Sep 17 00:00:00 2001 From: robertc <> Date: Thu, 9 Jan 2003 19:27:14 +0000 Subject: [PATCH] extract async completion notification --- src/comm.cc | 45 +++++++++++++++++++++++++++++++++++++++++++- src/comm.h | 26 +++++++++++++++++++++++++ src/fs/aufs/aiops.cc | 36 ++++------------------------------- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/src/comm.cc b/src/comm.cc index 51e6880e1f..acbbc80d00 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -1,6 +1,6 @@ /* - * $Id: comm.cc,v 1.360 2002/12/06 23:19:15 hno Exp $ + * $Id: comm.cc,v 1.361 2003/01/09 12:27:14 robertc Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -1885,3 +1885,46 @@ comm_accept(int fd, IOACB *handler, void *handler_data) commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0); #endif } + +void CommIO::Initialise() +{ + /* Initialize done pipe signal */ + int DonePipe[2]; + pipe(DonePipe); + DoneFD = DonePipe[1]; + DoneReadFD = DonePipe[0]; + fd_open(DonePipe[0], FD_PIPE, "async-io completetion event: main"); + fd_open(DonePipe[1], FD_PIPE, "async-io completetion event: threads"); + commSetNonBlocking(DonePipe[0]); + commSetNonBlocking(DonePipe[1]); + commSetSelect(DonePipe[0], COMM_SELECT_READ, NULLFDHandler, NULL, 0); + Initialised = true; +} + +bool CommIO::Initialised = false; +bool CommIO::DoneSignalled = false; +int CommIO::DoneFD = -1; +int CommIO::DoneReadFD = -1; + +void +CommIO::FlushPipe() +{ + char buf[256]; + read(DoneReadFD, buf, sizeof(buf)); +} + +void +CommIO::NULLFDHandler(int fd, void *data) +{ + FlushPipe(); + commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0); +} + +void +CommIO::ResetNotifications() +{ + if (DoneSignalled) { + FlushPipe(); + DoneSignalled = false; + } +} diff --git a/src/comm.h b/src/comm.h index 7808d354c4..6ce1bfbf69 100644 --- a/src/comm.h +++ b/src/comm.h @@ -20,4 +20,30 @@ extern void comm_accept_setcheckperiod(int fd, int mdelay); extern void comm_write(int s, const char *buf, size_t len, IOWCB *callback, void *callback_data); +/* Where should this belong? */ +class CommIO { +public: + static inline void NotifyIOCompleted(); + static void ResetNotifications(); + static void Initialise(); +private: + static void NULLFDHandler(int, void *); + static void FlushPipe(); + static bool Initialised; + static bool DoneSignalled; + static int DoneFD; + static int DoneReadFD; +}; + +/* Inline code. TODO: make structued approach to inlining */ +void +CommIO::NotifyIOCompleted() { + if (!Initialised) + Initialise(); + if (!DoneSignalled) { + DoneSignalled = true; + write(DoneFD, "!", 1); + } +}; + #endif diff --git a/src/fs/aufs/aiops.cc b/src/fs/aufs/aiops.cc index 6dfda73166..d397b5ffa3 100644 --- a/src/fs/aufs/aiops.cc +++ b/src/fs/aufs/aiops.cc @@ -1,5 +1,5 @@ /* - * $Id: aiops.cc,v 1.20 2003/01/09 11:45:49 hno Exp $ + * $Id: aiops.cc,v 1.21 2003/01/09 12:27:18 robertc Exp $ * * DEBUG: section 43 AIOPS * AUTHOR: Stewart Forster @@ -46,6 +46,7 @@ #if HAVE_SCHED_H #include #endif +#include "comm.h" #define RIDICULOUS_LENGTH 4096 @@ -146,9 +147,6 @@ static struct { NULL, &done_requests.head }; -static int done_fd = 0; -static int done_fd_read = 0; -static int done_signalled = 0; static pthread_attr_t globattr; #if HAVE_SCHED_H static struct sched_param globsched; @@ -224,19 +222,10 @@ squidaio_xstrfree(char *str) xfree(str); } -static void -squidaio_fdhandler(int fd, void *data) -{ - char junk[256]; - read(done_fd_read, junk, sizeof(junk)); - commSetSelect(fd, COMM_SELECT_READ, squidaio_fdhandler, NULL, 0); -} - static void squidaio_init(void) { int i; - int done_pipe[2]; squidaio_thread_t *threadp; if (squidaio_initialised) @@ -280,16 +269,6 @@ squidaio_init(void) done_queue.requests = 0; done_queue.blocked = 0; - /* Initialize done pipe signal */ - pipe(done_pipe); - done_fd = done_pipe[1]; - done_fd_read = done_pipe[0]; - fd_open(done_pipe[0], FD_PIPE, "async-io completetion event: main"); - fd_open(done_pipe[1], FD_PIPE, "async-io completetion event: threads"); - commSetNonBlocking(done_pipe[0]); - commSetNonBlocking(done_pipe[1]); - commSetSelect(done_pipe[0], COMM_SELECT_READ, squidaio_fdhandler, NULL, 0); - /* Create threads and get them to sit in their wait loop */ squidaio_thread_pool = memPoolCreate("aio_thread", sizeof(squidaio_thread_t)); assert(NUMTHREADS); @@ -411,10 +390,7 @@ squidaio_thread_loop(void *ptr) *done_queue.tailp = request; done_queue.tailp = &request->next; pthread_mutex_unlock(&done_queue.mutex); - if (!done_signalled) { - done_signalled = 1; - write(done_fd, "!", 1); - } + CommIO::NotifyIOCompleted(); threadp->requests++; } /* while forever */ return NULL; @@ -829,11 +805,7 @@ squidaio_poll_done(void) AIO_REPOLL: request = done_requests.head; if (request == NULL && !polled) { - if (done_signalled) { - char junk[256]; - read(done_fd_read, junk, sizeof(junk)); - done_signalled = 0; - } + CommIO::ResetNotifications(); squidaio_poll_queues(); polled = 1; request = done_requests.head; -- 2.47.3