Clone
ixen <ixen@copyhandler.com>
committed
on 24 Nov 16
Various cleanups and improvements in the multithreaded copying code (CH-307).
ParallelizeReaderWriter + 3 more
src/libchcore/TBufferList.cpp (deleted)
1   // ============================================================================
2   //  Copyright (C) 2001-2016 by Jozef Starosczyk
3   //  ixen@copyhandler.com
4   //
5   //  This program is free software; you can redistribute it and/or modify
6   //  it under the terms of the GNU Library General Public License
7   //  (version 2) as published by the Free Software Foundation;
8   //
9   //  This program is distributed in the hope that it will be useful,
10   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   //  GNU General Public License for more details.
13   //
14   //  You should have received a copy of the GNU Library General Public
15   //  License along with this program; if not, write to the
16   //  Free Software Foundation, Inc.,
17   //  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   // ============================================================================
19   #include "stdafx.h"
20   #include "TBufferList.h"
21   #include "TCoreException.h"
22   #include <boost/thread/locks.hpp>
23  
24   namespace chcore
25   {
26           TBufferList::TBufferList() :
27                   m_eventAllBuffersAccountedFor(true, true)
28           {
29           }
30  
31           void TBufferList::Push(TOverlappedDataBuffer* pBuffer)
32           {
33                   if(!pBuffer)
34                           throw TCoreException(eErr_InvalidArgument, L"pBuffer", LOCATION);
35  
36                   {
37                           boost::unique_lock<boost::shared_mutex> lock(m_mutex);
38  
39                           m_listBuffers.push_front(pBuffer);
40                           UpdateEvent();
41                   }
42  
43                   m_notifier();
44           }
45  
46           TOverlappedDataBuffer* TBufferList::Pop()
47           {
48                   TOverlappedDataBuffer* pBuffer = nullptr;
49  
50                   {
51                           boost::unique_lock<boost::shared_mutex> lock(m_mutex);
52  
53                           if(m_listBuffers.empty())
54                                   return nullptr;
55  
56                           pBuffer = m_listBuffers.front();
57                           m_listBuffers.pop_front();
58  
59                           UpdateEvent();
60                   }
61  
62                   m_notifier();
63  
64                   return pBuffer;
65           }
66  
67           void TBufferList::Clear()
68           {
69                   bool bRemoved = false;
70                   {
71                           boost::unique_lock<boost::shared_mutex> lock(m_mutex);
72  
73                           bRemoved = !m_listBuffers.empty();
74                           m_listBuffers.clear();
75  
76                           if(bRemoved)
77                                   UpdateEvent();
78                   }
79  
80                   if(bRemoved)
81                           m_notifier();
82           }
83  
84           size_t TBufferList::GetCount() const
85           {
86                   boost::shared_lock<boost::shared_mutex> lock(m_mutex);
87                   return m_listBuffers.size();
88           }
89  
90           bool TBufferList::IsEmpty() const
91           {
92                   boost::shared_lock<boost::shared_mutex> lock(m_mutex);
93                   return m_listBuffers.empty();
94           }
95  
96           void TBufferList::SetExpectedBuffersCount(size_t stExpectedBuffers)
97           {
98                   boost::shared_lock<boost::shared_mutex> lock(m_mutex);
99                   m_stExpectedBuffers = stExpectedBuffers;
100                   UpdateEvent();
101           }
102  
103           HANDLE TBufferList::GetAllBuffersAccountedForEvent() const
104           {
105                   return m_eventAllBuffersAccountedFor.Handle();
106           }
107  
108           boost::signals2::signal<void()>& TBufferList::GetNotifier()
109           {
110                   return m_notifier;
111           }
112  
113           void TBufferList::UpdateEvent()
114           {
115                   m_eventAllBuffersAccountedFor.SetEvent(m_listBuffers.size() == m_stExpectedBuffers);
116           }
117   }