Ignore:
Timestamp:
Jul 5, 2002, 7:58:26 PM (23 years ago)
Author:
sandervl
Message:

Keep reference count for overlapped IO objects to avoid premature destruction

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/overlappedio.cpp

    r8647 r8839  
    1 /* $Id: overlappedio.cpp,v 1.17 2002-06-11 16:36:06 sandervl Exp $ */
     1/* $Id: overlappedio.cpp,v 1.18 2002-07-05 17:58:26 sandervl Exp $ */
    22
    33/*
     
    2929                                         LPOVERLAPPED_HANDLER lpPollHandler,
    3030                                         BOOL fFullDuplex) :
    31                    hThreadRead(0), hThreadWrite(0), hThreadPoll(0)
     31                   hThreadRead(0), hThreadWrite(0), hThreadPoll(0), refCount(0)
    3232{
    3333    OverlappedIOError errcode = OutOfMemory;
     
    7575    threadparam->lpOverlappedObj = this;
    7676    hThreadRead  = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
     77    if(hThreadRead) {//thread uses this object; keep reference count to avoid premature destruction
     78        AddRef();
     79    }
    7780
    7881    if(lpWriteHandler && fFullDuplex) {
     
    8487        threadparam->lpOverlappedObj = this;
    8588        hThreadWrite = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
     89        if(hThreadWrite) {//thread uses this object; keep reference count to avoid premature destruction
     90            AddRef();
     91        }
    8692    }
    8793
     
    95101        hThreadPoll  = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
    96102        SetThreadPriority(hThreadPoll, THREAD_PRIORITY_TIME_CRITICAL);
     103        if(hThreadPoll) {//thread uses this object; keep reference count to avoid premature destruction
     104            AddRef();
     105        }
    97106    }
    98107
     
    146155
    147156    DeleteCriticalSection(&critsect);
     157}
     158//******************************************************************************
     159//******************************************************************************
     160DWORD OverlappedIOHandler::AddRef()
     161{
     162    return InterlockedIncrement(&refCount);
     163}
     164//******************************************************************************
     165//******************************************************************************
     166DWORD OverlappedIOHandler::Release(BOOL fSignalExit)
     167{
     168    if(fSignalExit) {
     169        ::SetEvent(hEventExit);
     170    }
     171    if(InterlockedDecrement(&refCount) == 0) {
     172        dprintf(("OverlappedIOHandler::Release -> delete now"));
     173        delete this;
     174        return 0;
     175    }
     176    return refCount;
    148177}
    149178//******************************************************************************
     
    314343        } //while(TRUE)
    315344    }
     345    Release();  //decrease reference count
    316346    return 0;
    317347}
Note: See TracChangeset for help on using the changeset viewer.