--------------------- PatchSet 4126 Date: 2002/05/12 17:12:09 Author: serassio Branch: nt-2_5 Tag: (none) Log: Fixed some old remains on AWIN32 from Robert (Thread priority, Handles closing) and merged some multithread enhancement from 2.3 Andrey's work - Signal problem is still open Members: src/fs/awin32/aiops.c:1.1.62.1->1.1.62.2 src/fs/awin32/async_io.c:1.1.62.1->1.1.62.2 src/fs/awin32/store_asyncufs.h:1.1.62.3->1.1.62.4 src/fs/awin32/store_dir_aufs.c:1.1.62.4->1.1.62.5 Index: squid/src/fs/awin32/aiops.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/fs/awin32/Attic/aiops.c,v retrieving revision 1.1.62.1 retrieving revision 1.1.62.2 diff -u -r1.1.62.1 -r1.1.62.2 --- squid/src/fs/awin32/aiops.c 14 Mar 2002 20:23:47 -0000 1.1.62.1 +++ squid/src/fs/awin32/aiops.c 12 May 2002 17:12:09 -0000 1.1.62.2 @@ -1,5 +1,5 @@ /* - * $Id: aiops.c,v 1.1.62.1 2002/03/14 20:23:47 serassio Exp $ + * $Id: aiops.c,v 1.1.62.2 2002/05/12 17:12:09 serassio Exp $ * * DEBUG: section 43 AIOPS * AUTHOR: Stewart Forster @@ -90,8 +90,8 @@ } squidaio_request_t; typedef struct squidaio_request_queue_t { - HANDLE mutex; /* pthread_mutex_t mutex; */ - HANDLE cond; /*pthread_cond_t cond; */ /* See Event objects */ + HANDLE mutex; + HANDLE cond; /* See Event objects */ squidaio_request_t *volatile head; squidaio_request_t *volatile *volatile tailp; unsigned long requests; @@ -101,11 +101,12 @@ typedef struct squidaio_thread_t squidaio_thread_t; struct squidaio_thread_t { squidaio_thread_t *next; - HANDLE thread; /* pthread_t thread; */ + HANDLE thread; DWORD dwThreadId; /* thread ID */ squidaio_thread_status status; struct squidaio_request_t *current_req; unsigned long requests; + int volatile exit; }; int squidaio_cancel(squidaio_result_t *); @@ -170,9 +171,7 @@ NULL, &done_requests.head }; -/* static pthread_attr_t globattr; The attributes of a created thread */ -/*static struct sched_param globsched; */ -/* static pthread_t main_thread; */ + static HANDLE main_thread; static MemPool * @@ -253,42 +252,28 @@ if (squidaio_initialised) return; - if (!DuplicateHandle(GetCurrentProcess(), /* pseudo handle, don't close */ - GetCurrentThread(), /* pseudo handle to copy */ - GetCurrentProcess(), /* pseudo handle, don't close */ - &main_thread, - 0, /* required access */ - FALSE, /* child process's don't inherit the handle */ - DUPLICATE_SAME_ACCESS)) { + if (!DuplicateHandle(GetCurrentProcess(), /* pseudo handle, don't close */ + GetCurrentThread(), /* pseudo handle to copy */ + GetCurrentProcess(),/* pseudo handle, don't close */ + &main_thread, + 0, /* required access */ + FALSE, /* child process's don't inherit the handle */ + DUPLICATE_SAME_ACCESS)) { /* spit errors */ fatal("couldn't get current thread handle\n"); } - /* FIXME: where do we CloseHandle the main_thread ?*/ -#if 0 -/* FIXME set the current thread to priority 1 */ -#if HAVE_PTHREAD_SETSCHEDPARAM - pthread_setschedparam(main_thread, SCHED_OTHER, &globsched); -#endif -/* FIXME set new threads to priority 2 - globsched.sched_priority = 2; -#if HAVE_PTHREAD_ATTR_SETSCHEDPARAM - pthread_attr_setschedparam(&globattr, &globsched); -#endif -*/ -#endif - /* Initialize request queue */ - if ((request_queue.mutex=CreateMutex( NULL, /* no inheritance */ - FALSE, /* start unowned (as per mutex_init) */ - NULL /* no name */ ) - )==NULL) { + if ((request_queue.mutex = CreateMutex(NULL, /* no inheritance */ + FALSE, /* start unowned (as per mutex_init) */ + NULL) /* no name */ + ) == NULL) { fatal("failed to create mutex\n"); } - if ((request_queue.cond=CreateEvent( NULL, /* no inheritance */ - FALSE, /* auto signal reset - which I think is pthreads like ? */ - FALSE, /* start non signaled */ - NULL /* no name */ ) - )==NULL) { + if ((request_queue.cond = CreateEvent(NULL, /* no inheritance */ + FALSE, /* auto signal reset - which I think is pthreads like ? */ + FALSE, /* start non signaled */ + NULL) /* no name */ + ) == NULL) { fatal("failed to create condition event variable.\n"); } request_queue.head = NULL; @@ -297,17 +282,17 @@ request_queue.blocked = 0; /* Initialize done queue */ - if ((done_queue.mutex=CreateMutex( NULL, /* no inheritance */ - FALSE, /* start unowned (as per mutex_init) */ - NULL /* no name */ ) - )==NULL) { + if ((done_queue.mutex = CreateMutex(NULL, /* no inheritance */ + FALSE, /* start unowned (as per mutex_init) */ + NULL) /* no name */ + ) == NULL) { fatal("failed to create mutex\n"); } - if ((done_queue.cond=CreateEvent( NULL, /* no inheritance */ - TRUE, /* manually signaled - which I think is pthreads like ? */ - FALSE, /* start non signaled */ - NULL /* no name */ ) - )==NULL) { + if ((done_queue.cond = CreateEvent(NULL, /* no inheritance */ + TRUE, /* manually signaled - which I think is pthreads like ? */ + FALSE, /* start non signaled */ + NULL) /* no name */ + ) == NULL) { fatal("failed to create condition event variable.\n"); } done_queue.head = NULL; @@ -324,18 +309,19 @@ threadp->requests = 0; threadp->next = threads; threads = threadp; - if ((threadp->thread = CreateThread( - NULL, // no security attributes - 0, // use default stack size - squidaio_thread_loop, // thread function - threadp, // argument to thread function - 0, // use default creation flags - &(threadp->dwThreadId)) // returns the thread identifier - )==NULL) { + if ((threadp->thread = CreateThread(NULL, /* no security attributes */ + 0, /* use default stack size */ + squidaio_thread_loop, /* thread function */ + threadp, /* argument to thread function */ + 0, /* use default creation flags */ + &(threadp->dwThreadId)) /* returns the thread identifier */ + ) == NULL) { fprintf(stderr, "Thread creation failed\n"); threadp->status = _THREAD_FAILED; continue; } + /* Set the new thread priority above parent process */ + SetThreadPriority(threadp->thread,THREAD_PRIORITY_ABOVE_NORMAL); } /* Create request pool */ @@ -349,9 +335,40 @@ squidaio_initialised = 1; } +void +squidaio_shutdown(void) +{ + squidaio_thread_t *threadp; + int i; + HANDLE hthreads[NUMTHREADS]; + + if (!squidaio_initialised) + return; + + for (i = 0; i < NUMTHREADS; i++) { + threadp = &threads[i]; + threadp->exit = 1; + hthreads[i] = threadp->thread; + } + ReleaseMutex(request_queue.mutex); + ResetEvent(request_queue.cond); + ReleaseMutex(done_queue.mutex); + ResetEvent(done_queue.cond); + Sleep(0); + + CloseHandle(main_thread); + { + DWORD r = WaitForMultipleObjects(NUMTHREADS, hthreads, TRUE, INFINITE); + assert(r >= WAIT_OBJECT_0 && r < WAIT_OBJECT_0 + NUMTHREADS); + } + for (i = 0; i < NUMTHREADS; i++) { + CloseHandle(hthreads[i]); + } + squidaio_initialised = 0; +} static DWORD WINAPI -squidaio_thread_loop( LPVOID lpParam ) +squidaio_thread_loop(LPVOID lpParam) { squidaio_thread_t *threadp = lpParam; squidaio_request_t *request; @@ -363,13 +380,11 @@ /* * Does WIN32 have this problem? */ - /* * Make sure to ignore signals which may possibly get sent to * the parent squid thread. Causes havoc with mutex's and * condition waits otherwise */ - sigemptyset(&new); sigaddset(&new, SIGPIPE); sigaddset(&new, SIGCHLD); @@ -388,26 +403,23 @@ #endif /* 0 */ /* lock the thread info */ - if (WAIT_FAILED == WaitForSingleObject( - request_queue.mutex, // handle to object - INFINITE // time-out interval - )) { - /* wait failed.... what to do ? */ + if (WAIT_FAILED == WaitForSingleObject(request_queue.mutex, INFINITE)) { + fatal("Can't get ownership of mutex\n"); } /* duplicate the handle */ - if (!DuplicateHandle(GetCurrentProcess(), /* pseudo handle, don't close */ - request_queue.cond, /* handle to copy */ - GetCurrentProcess(), /* pseudo handle, don't close */ - &cond, - 0, /* required access */ - FALSE, /* child process's don't inherit the handle */ - DUPLICATE_SAME_ACCESS)) - exit(1); + if (!DuplicateHandle(GetCurrentProcess(), /* pseudo handle, don't close */ + request_queue.cond, /* handle to copy */ + GetCurrentProcess(), /* pseudo handle, don't close */ + &cond, + 0, /* required access */ + FALSE, /* child process's don't inherit the handle */ + DUPLICATE_SAME_ACCESS)) + fatal("Can't duplicate mutex handle\n"); if (!ReleaseMutex(request_queue.mutex)) { CloseHandle(cond); - exit(1); + fatal("Can't release mutex\n"); } - + Sleep(0); while (1) { DWORD rv; @@ -415,34 +427,30 @@ request = NULL; /* Get a request to process */ threadp->status = _THREAD_WAITING; - rv = WaitForSingleObject( - request_queue.mutex, // handle to object - INFINITE // time-out interval - ); + if (threadp->exit) { + CloseHandle(request_queue.mutex); + CloseHandle(cond); + return 0; + } + rv = WaitForSingleObject(request_queue.mutex, INFINITE); if (rv == WAIT_FAILED) { CloseHandle(cond); return 1; } - while (!request_queue.head) { if (!ReleaseMutex(request_queue.mutex)) { CloseHandle(cond); threadp->status = _THREAD_FAILED; return 1; } - rv = WaitForSingleObject( - cond, // handle to object - INFINITE // time-out interval - ); + Sleep(0); + rv = WaitForSingleObject(cond, INFINITE); if (rv == WAIT_FAILED) { CloseHandle(cond); return 1; } - rv = WaitForSingleObject( - request_queue.mutex, // handle to object - INFINITE // time-out interval - ); + rv = WaitForSingleObject(request_queue.mutex, INFINITE); if (rv == WAIT_FAILED) { CloseHandle(cond); return 1; @@ -457,6 +465,7 @@ CloseHandle(cond); return 1; } + Sleep(0); /* process the request */ threadp->status = _THREAD_BUSY; request->next = NULL; @@ -501,10 +510,7 @@ } threadp->status = _THREAD_DONE; /* put the request in the done queue */ - rv = WaitForSingleObject( - done_queue.mutex, // handle to object - INFINITE // time-out interval - ); + rv = WaitForSingleObject(done_queue.mutex, INFINITE); if (rv == WAIT_FAILED) { CloseHandle(cond); return 1; @@ -515,6 +521,7 @@ CloseHandle(cond); return 1; } + Sleep(0); threadp->requests++; } /* while forever */ CloseHandle(cond); @@ -538,7 +545,7 @@ if (!request_queue2.head) { /* If queue2 is empty, insert into queue 1 and try to "push" the queue If the "push" fails, queue it via queue2 */ - if (WaitForSingleObject(request_queue.mutex, 0)==WAIT_OBJECT_0) { + if (WaitForSingleObject(request_queue.mutex, 0) == WAIT_OBJECT_0) { /* Normal path */ *request_queue.tailp = request; request_queue.tailp = &request->next; @@ -548,6 +555,7 @@ /* unexpected error */ fatal("couldn't push queue\n"); } + Sleep(0); } else { /* Oops, the request queue is blocked, use request_queue2 */ *request_queue2.tailp = request; @@ -567,6 +575,7 @@ /* unexpected error */ fatal("couldn't push queue\n"); } + Sleep(0); request_queue2.head = NULL; request_queue2.tailp = &request_queue2.head; } else { @@ -916,6 +925,7 @@ if (!ReleaseMutex(request_queue.mutex)) { /* unexpected error */ } + Sleep(0); request_queue2.head = NULL; request_queue2.tailp = &request_queue2.head; } @@ -928,6 +938,7 @@ if (!ReleaseMutex(done_queue.mutex)) { /* unexpected error */ } + Sleep(0); *done_requests.tailp = requests; request_queue_len -= 1; while (requests->next) { Index: squid/src/fs/awin32/async_io.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/fs/awin32/Attic/async_io.c,v retrieving revision 1.1.62.1 retrieving revision 1.1.62.2 diff -u -r1.1.62.1 -r1.1.62.2 --- squid/src/fs/awin32/async_io.c 14 Mar 2002 20:23:47 -0000 1.1.62.1 +++ squid/src/fs/awin32/async_io.c 12 May 2002 17:12:09 -0000 1.1.62.2 @@ -1,6 +1,6 @@ /* - * $Id: async_io.c,v 1.1.62.1 2002/03/14 20:23:47 serassio Exp $ + * $Id: async_io.c,v 1.1.62.2 2002/05/12 17:12:09 serassio Exp $ * * DEBUG: section 32 Asynchronous Disk I/O * AUTHOR: Pete Bentley @@ -102,6 +102,9 @@ void aioDone(void) { + if (!initialised) + return; + squidaio_shutdown(); memPoolDestroy(squidaio_ctrl_pool); initialised = 0; } Index: squid/src/fs/awin32/store_asyncufs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/fs/awin32/Attic/store_asyncufs.h,v retrieving revision 1.1.62.3 retrieving revision 1.1.62.4 diff -u -r1.1.62.3 -r1.1.62.4 --- squid/src/fs/awin32/store_asyncufs.h 9 Apr 2002 19:04:43 -0000 1.1.62.3 +++ squid/src/fs/awin32/store_asyncufs.h 12 May 2002 17:12:09 -0000 1.1.62.4 @@ -49,6 +49,7 @@ int squidaio_operations_pending(void); int squidaio_sync(void); int squidaio_get_queue_len(void); +void squidaio_shutdown(void); void aioInit(void); void aioDone(void); Index: squid/src/fs/awin32/store_dir_aufs.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/fs/awin32/Attic/store_dir_aufs.c,v retrieving revision 1.1.62.4 retrieving revision 1.1.62.5 diff -u -r1.1.62.4 -r1.1.62.5 --- squid/src/fs/awin32/store_dir_aufs.c 7 Apr 2002 13:10:25 -0000 1.1.62.4 +++ squid/src/fs/awin32/store_dir_aufs.c 12 May 2002 17:12:09 -0000 1.1.62.5 @@ -1,6 +1,6 @@ /* - * $Id: store_dir_aufs.c,v 1.1.62.4 2002/04/07 13:10:25 serassio Exp $ + * $Id: store_dir_aufs.c,v 1.1.62.5 2002/05/12 17:12:09 serassio Exp $ * * DEBUG: section 47 Store Directory Routines * AUTHOR: Duane Wessels @@ -1722,10 +1722,10 @@ storefs->parsefunc = storeAufsDirParse; storefs->reconfigurefunc = storeAufsDirReconfigure; storefs->donefunc = storeAufsDirDone; - squidaio_state_pool = memPoolCreate("AUFS IO State data", sizeof(squidaiostate_t)); - aufs_qread_pool = memPoolCreate("AUFS Queued read data", + squidaio_state_pool = memPoolCreate("AWIN32 IO State data", sizeof(squidaiostate_t)); + aufs_qread_pool = memPoolCreate("AWIN32 Queued read data", sizeof(queued_read)); - aufs_qwrite_pool = memPoolCreate("AUFS Queued write data", + aufs_qwrite_pool = memPoolCreate("AWIN32 Queued write data", sizeof(queued_write)); asyncufs_initialised = 1;