All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RasterSynchronizer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/raster/RasterSynchronizer.cpp
22 
23  \brief An access synchronizer to be used in RasterSynchronizer raster instances
24 */
25 
26 // TerraLib
27 #include "RasterSynchronizer.h"
28 #include "Band.h"
29 #include "BandProperty.h"
30 #include "Exception.h"
31 #include "../common/Translator.h"
32 
33 #include <climits>
34 
36  const te::common::AccessPolicy policy )
37 : m_policy( policy ), m_raster( raster )
38 {
39  m_blocksUseCounters.resize( raster.getNumberOfBands() );
40 
41  for( unsigned int bandIdx = 0; bandIdx < m_blocksUseCounters.size() ; ++bandIdx )
42  {
43  m_blocksUseCounters[ bandIdx ].resize( raster.getBand( bandIdx )->getProperty()->m_nblocksy );
44 
45  for( unsigned int blockYIdx = 0 ; blockYIdx < m_blocksUseCounters[ bandIdx ].size() ;
46  ++blockYIdx )
47  {
48  m_blocksUseCounters[ bandIdx ][ blockYIdx ].resize( raster.getBand( bandIdx )->getProperty()->m_nblocksx, 0 );
49  }
50  }
51 }
52 
54 {
55 }
56 
57 bool te::rst::RasterSynchronizer::acquireBlock( const unsigned int bandIdx,
58  const unsigned int blockXIndex, const unsigned int blockYIndex,
59  void* blkDataPtr )
60 {
61  m_mutex.lock();
62 
63  if( bandIdx >= m_blocksUseCounters.size() )
64  {
65  m_mutex.unlock();
66  throw Exception(TE_TR("Inalid band index") );
67  }
68  if( blockYIndex >= m_blocksUseCounters[ bandIdx ].size() )
69  {
70  m_mutex.unlock();
71  throw Exception(TE_TR("Inalid block Y index") );
72  }
73  if( blockXIndex >= m_blocksUseCounters[ bandIdx ][ blockYIndex ].size() )
74  {
75  m_mutex.unlock();
76  throw Exception(TE_TR("Inalid block X index") );
77  }
78 
79  if( m_policy & te::common::WAccess )
80  {
81  // Wait the block to be avaliable
82 
83  while( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] )
84  {
85  m_condVar.wait( m_mutex );
86  }
87 
88  assert( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] == 0 );
89 
90  m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] = 1;
91 
92  m_raster.getBand( bandIdx )->read( blockXIndex, blockYIndex, blkDataPtr );
93 
94  m_mutex.unlock();
95 
96  return true;
97  }
98  else
99  {
100  ++( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] );
101 
102  m_raster.getBand( bandIdx )->read( blockXIndex, blockYIndex, blkDataPtr );
103 
104  m_mutex.unlock();
105 
106  return true;
107  }
108 }
109 
110 bool te::rst::RasterSynchronizer::releaseBlock( const unsigned int bandIdx,
111  const unsigned int blockXIndex, const unsigned int blockYIndex,
112  void* blkDataPtr )
113 {
114  m_mutex.lock();
115 
116  if( bandIdx >= m_blocksUseCounters.size() )
117  {
118  m_mutex.unlock();
119  throw Exception(TE_TR("Inalid band index") );
120  }
121  if( blockYIndex >= m_blocksUseCounters[ bandIdx ].size() )
122  {
123  m_mutex.unlock();
124  throw Exception(TE_TR("Inalid block Y index") );
125  }
126  if( blockXIndex >= m_blocksUseCounters[ bandIdx ][ blockYIndex ].size() )
127  {
128  m_mutex.unlock();
129  throw Exception(TE_TR("Inalid block X index") );
130  }
131 
132  if( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] )
133  {
134  if( ( m_policy & te::common::WAccess ) &&
135  ( m_raster.getAccessPolicy() & te::common::WAccess ) )
136  {
137  m_raster.getBand( bandIdx )->write( blockXIndex, blockYIndex, blkDataPtr );
138  }
139 
140  --( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] );
141  }
142 
143  m_mutex.unlock();
144 
145  m_condVar.notify_all();
146 
147  return true;
148 }
149 
It describes one band (or dimension) of a raster.
bool acquireBlock(const unsigned int bandIdx, const unsigned int blockXIndex, const unsigned int blockYIndex, void *blkDataPtr)
Acquire a raster data block.
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
An exception class for the Raster module.
RasterSynchronizer(Raster &raster, const te::common::AccessPolicy policy)
Constructor.
An abstract class for raster data strucutures.
Definition: Raster.h:71
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:370
An access synchronizer to be used in SynchronizedRaster raster instances.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
It gives access to values in one band (dimension) of a raster.
BlocksUseCounterT m_blocksUseCounters
blocks use counter.
bool releaseBlock(const unsigned int bandIdx, const unsigned int blockXIndex, const unsigned int blockYIndex, void *blkDataPtr)
Release a raster data block.