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 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_raster( raster )
38 {
39  if( ( raster.getAccessPolicy() & te::common::WAccess ) &&
40  ( policy & te::common::WAccess ) )
41  {
42  m_policy = raster.getAccessPolicy();
43  }
44  else
45  {
46  if( raster.getAccessPolicy() & te::common::RAccess )
47  {
49  }
50  else
51  {
53  }
54  }
55 
56  m_blocksUseCounters.resize( raster.getNumberOfBands() );
57 
58  for( unsigned int bandIdx = 0; bandIdx < m_blocksUseCounters.size() ; ++bandIdx )
59  {
60  m_blocksUseCounters[ bandIdx ].resize( raster.getBand( bandIdx )->getProperty()->m_nblocksy );
61 
62  for( unsigned int blockYIdx = 0 ; blockYIdx < m_blocksUseCounters[ bandIdx ].size() ;
63  ++blockYIdx )
64  {
65  m_blocksUseCounters[ bandIdx ][ blockYIdx ].resize( raster.getBand( bandIdx )->getProperty()->m_nblocksx, 0 );
66  }
67  }
68 }
69 
71 {
72 }
73 
74 bool te::rst::RasterSynchronizer::acquireBlock( const unsigned int bandIdx,
75  const unsigned int blockXIndex, const unsigned int blockYIndex,
76  void* blkDataPtr )
77 {
78  m_mutex.lock();
79 
80  if( bandIdx >= m_blocksUseCounters.size() )
81  {
82  m_mutex.unlock();
83  throw Exception(TE_TR("Inalid band index") );
84  }
85  if( blockYIndex >= m_blocksUseCounters[ bandIdx ].size() )
86  {
87  m_mutex.unlock();
88  throw Exception(TE_TR("Inalid block Y index") );
89  }
90  if( blockXIndex >= m_blocksUseCounters[ bandIdx ][ blockYIndex ].size() )
91  {
92  m_mutex.unlock();
93  throw Exception(TE_TR("Inalid block X index") );
94  }
95 
96  if( m_policy & te::common::WAccess )
97  {
98  // Wait the block to be avaliable
99 
100  while( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] )
101  {
102  m_condVar.wait( m_mutex );
103  }
104 
105  assert( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] == 0 );
106 
107  m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] = 1;
108 
109  m_raster.getBand( bandIdx )->read( blockXIndex, blockYIndex, blkDataPtr );
110 
111  m_mutex.unlock();
112 
113  return true;
114  }
115  else
116  {
117  ++( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] );
118 
119  m_raster.getBand( bandIdx )->read( blockXIndex, blockYIndex, blkDataPtr );
120 
121  m_mutex.unlock();
122 
123  return true;
124  }
125 }
126 
127 bool te::rst::RasterSynchronizer::releaseBlock( const unsigned int bandIdx,
128  const unsigned int blockXIndex, const unsigned int blockYIndex,
129  void* blkDataPtr )
130 {
131  m_mutex.lock();
132 
133  if( bandIdx >= m_blocksUseCounters.size() )
134  {
135  m_mutex.unlock();
136  throw Exception(TE_TR("Inalid band index") );
137  }
138  if( blockYIndex >= m_blocksUseCounters[ bandIdx ].size() )
139  {
140  m_mutex.unlock();
141  throw Exception(TE_TR("Inalid block Y index") );
142  }
143  if( blockXIndex >= m_blocksUseCounters[ bandIdx ][ blockYIndex ].size() )
144  {
145  m_mutex.unlock();
146  throw Exception(TE_TR("Inalid block X index") );
147  }
148 
149  if( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] )
150  {
151  if( ( m_policy & te::common::WAccess ) &&
152  ( m_raster.getAccessPolicy() & te::common::WAccess ) )
153  {
154  m_raster.getBand( bandIdx )->write( blockXIndex, blockYIndex, blkDataPtr );
155  }
156 
157  --( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] );
158  }
159 
160  m_mutex.unlock();
161 
162  m_condVar.notify_all();
163 
164  return true;
165 }
166 
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:347
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
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:428
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.
te::common::AccessPolicy m_policy
The access policy used on the given input 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.