1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2008 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 #include "RWSection.h"
\r
22 CRWSection::CRWSection()
\r
24 m_nWaitingReaders = m_nWaitingWriters = m_nActive = 0;
\r
25 m_hReaders = CreateSemaphore(NULL, 0, MAXLONG, NULL);
\r
26 m_hWriters = CreateSemaphore(NULL, 0, MAXLONG, NULL);
\r
27 InitializeCriticalSection(&m_cs);
\r
30 CRWSection::~CRWSection()
\r
37 m_nWaitingReaders = m_nWaitingWriters = m_nActive = 0;
\r
38 DeleteCriticalSection(&m_cs);
\r
39 CloseHandle(m_hWriters);
\r
40 CloseHandle(m_hReaders);
\r
43 bool CRWSection::WaitToRead(DWORD waitTime)
\r
45 EnterCriticalSection(&m_cs);
\r
47 // Writers have priority
\r
48 BOOL fResourceWritePending = (m_nWaitingWriters || (m_nActive < 0));
\r
50 if (fResourceWritePending)
\r
52 m_nWaitingReaders++;
\r
59 LeaveCriticalSection(&m_cs);
\r
61 if (fResourceWritePending)
\r
63 // wait until writer is finished
\r
64 if (WaitForSingleObject(m_hReaders, waitTime) != WAIT_OBJECT_0)
\r
66 EnterCriticalSection(&m_cs);
\r
67 m_nWaitingReaders--;
\r
68 LeaveCriticalSection(&m_cs);
\r
75 bool CRWSection::WaitToWrite(DWORD waitTime)
\r
77 EnterCriticalSection(&m_cs);
\r
79 BOOL fResourceOwned = (m_nActive != 0);
\r
83 m_nWaitingWriters++;
\r
90 LeaveCriticalSection(&m_cs);
\r
94 if (WaitForSingleObject(m_hWriters, waitTime) != WAIT_OBJECT_0)
\r
96 EnterCriticalSection(&m_cs);
\r
97 m_nWaitingWriters--;
\r
98 LeaveCriticalSection(&m_cs);
\r
105 void CRWSection::Done()
\r
107 EnterCriticalSection(&m_cs);
\r
118 HANDLE hsem = NULL;
\r
121 if (m_nActive == 0)
\r
123 // Note: If there are always writers waiting, then
\r
124 // it's possible that a reader never gets access
\r
125 // (reader starvation)
\r
126 if (m_nWaitingWriters > 0)
\r
129 m_nWaitingWriters--;
\r
132 else if (m_nWaitingReaders > 0)
\r
134 m_nActive = m_nWaitingReaders;
\r
135 m_nWaitingReaders = 0;
\r
137 lCount = m_nActive;
\r
141 // no threads waiting, nothing to do
\r
145 LeaveCriticalSection(&m_cs);
\r
149 ReleaseSemaphore(hsem, lCount, NULL);
\r