X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2Fwin32%2FIOManager.c;h=c9a759fffbce2655f971f220272fb409ed7b4a06;hb=7d9eb2e45b4a9ff4cb053b1ec37602be88528b62;hp=a67c3504c1788d738be06101c6ad582a9f66141c;hpb=0065d5ab628975892cea1ec7303f968c3338cbe1;p=ghc-hetmet.git diff --git a/rts/win32/IOManager.c b/rts/win32/IOManager.c index a67c350..c9a759f 100644 --- a/rts/win32/IOManager.c +++ b/rts/win32/IOManager.c @@ -4,6 +4,9 @@ * * (c) sof, 2002-2003. */ + +#if !defined(THREADED_RTS) + #include "Rts.h" #include "IOManager.h" #include "WorkQueue.h" @@ -13,6 +16,7 @@ #include #include #include +#include /* * Internal state maintained by the IO manager. @@ -80,6 +84,9 @@ IOWorkerProc(PVOID param) if (rc == WAIT_OBJECT_0) { // we received the exit event + EnterCriticalSection(&iom->manLock); + ioMan->numWorkers--; + LeaveCriticalSection(&iom->manLock); return 0; } @@ -175,7 +182,15 @@ IOWorkerProc(PVOID param) len = write(work->workData.ioData.fd, work->workData.ioData.buf, work->workData.ioData.len); - if (len == -1) { errCode = errno; } + if (len == -1) { + errCode = errno; + // write() gets errno wrong for + // ERROR_NO_DATA, we have to fix it here: + if (errCode == EINVAL && + GetLastError() == ERROR_NO_DATA) { + errCode = EPIPE; + } + } } complData = work->workData.ioData.buf; fd = work->workData.ioData.fd; @@ -184,7 +199,7 @@ IOWorkerProc(PVOID param) * * Note: Sleep() is in milliseconds, not micros. */ - Sleep(work->workData.delayData.msecs / 1000); + Sleep((work->workData.delayData.msecs + 999) / 1000); len = work->workData.delayData.msecs; complData = NULL; fd = 0; @@ -217,10 +232,16 @@ IOWorkerProc(PVOID param) free(work); } else { fprintf(stderr, "unable to fetch work; fatal.\n"); fflush(stderr); + EnterCriticalSection(&iom->manLock); + ioMan->numWorkers--; + LeaveCriticalSection(&iom->manLock); return 1; } } else { fprintf(stderr, "waiting failed (%lu); fatal.\n", rc); fflush(stderr); + EnterCriticalSection(&iom->manLock); + ioMan->numWorkers--; + LeaveCriticalSection(&iom->manLock); return 1; } } @@ -331,14 +352,12 @@ depositWorkItem( unsigned int reqID, if ( (ioMan->workersIdle < ioMan->queueSize) ) { /* No, go ahead and create another. */ ioMan->numWorkers++; - LeaveCriticalSection(&ioMan->manLock); - NewIOWorkerThread(ioMan); - } else { - LeaveCriticalSection(&ioMan->manLock); + if (!NewIOWorkerThread(ioMan)) { + ioMan->numWorkers--; + } } - } else { - LeaveCriticalSection(&ioMan->manLock); } + LeaveCriticalSection(&ioMan->manLock); if (SubmitWork(ioMan->workQueue,wItem)) { /* Note: the work item has potentially been consumed by a worker thread @@ -433,14 +452,29 @@ AddProcRequest ( void* proc, return depositWorkItem(reqID, wItem); } -void ShutdownIOManager ( void ) +void ShutdownIOManager ( rtsBool wait_threads ) { - SetEvent(ioMan->hExitEvent); - // ToDo: we can't free this now, because the worker thread(s) - // haven't necessarily finished with it yet. Perhaps it should - // have a reference count or something. - // free(ioMan); - // ioMan = NULL; + int num; + + SetEvent(ioMan->hExitEvent); + + if (wait_threads) { + /* Wait for all worker threads to die. */ + for (;;) { + EnterCriticalSection(&ioMan->manLock); + num = ioMan->numWorkers; + LeaveCriticalSection(&ioMan->manLock); + if (num == 0) + break; + Sleep(10); + } + FreeWorkQueue(ioMan->workQueue); + CloseHandle(ioMan->hExitEvent); + DeleteCriticalSection(&ioMan->active_work_lock); + DeleteCriticalSection(&ioMan->manLock); + free(ioMan); + ioMan = NULL; + } } /* Keep track of WorkItems currently being serviced. */ @@ -508,3 +542,5 @@ abandonWorkRequest ( int reqID ) */ LeaveCriticalSection(&ioMan->active_work_lock); } + +#endif