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 "../core/translator/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 bool te::rst::RasterSynchronizer::acquireBlock( const unsigned int bandIdx,
73  const unsigned int blockXIndex, const unsigned int blockYIndex,
74  void* blkDataPtr )
75 {
76  m_mutex.lock();
77 
78  if( bandIdx >= m_blocksUseCounters.size() )
79  {
80  m_mutex.unlock();
81  throw Exception(TE_TR("Inalid band index") );
82  }
83  if( blockYIndex >= m_blocksUseCounters[ bandIdx ].size() )
84  {
85  m_mutex.unlock();
86  throw Exception(TE_TR("Inalid block Y index") );
87  }
88  if( blockXIndex >= m_blocksUseCounters[ bandIdx ][ blockYIndex ].size() )
89  {
90  m_mutex.unlock();
91  throw Exception(TE_TR("Inalid block X index") );
92  }
93 
95  {
96  // Wait the block to be avaliable
97 
98  while( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] )
99  {
100  m_condVar.wait( m_mutex );
101  }
102 
103  assert( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] == 0 );
104 
105  m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] = 1;
106 
107  m_raster.getBand( bandIdx )->read( blockXIndex, blockYIndex, blkDataPtr );
108 
109  m_mutex.unlock();
110 
111  return true;
112  }
113  else
114  {
115  ++( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] );
116 
117  m_raster.getBand( bandIdx )->read( blockXIndex, blockYIndex, blkDataPtr );
118 
119  m_mutex.unlock();
120 
121  return true;
122  }
123 }
124 
125 bool te::rst::RasterSynchronizer::releaseBlock( const unsigned int bandIdx,
126  const unsigned int blockXIndex, const unsigned int blockYIndex,
127  void* blkDataPtr )
128 {
129  m_mutex.lock();
130 
131  if( bandIdx >= m_blocksUseCounters.size() )
132  {
133  m_mutex.unlock();
134  throw Exception(TE_TR("Inalid band index") );
135  }
136  if( blockYIndex >= m_blocksUseCounters[ bandIdx ].size() )
137  {
138  m_mutex.unlock();
139  throw Exception(TE_TR("Inalid block Y index") );
140  }
141  if( blockXIndex >= m_blocksUseCounters[ bandIdx ][ blockYIndex ].size() )
142  {
143  m_mutex.unlock();
144  throw Exception(TE_TR("Inalid block X index") );
145  }
146 
147  if( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] )
148  {
149  if( ( m_policy & te::common::WAccess ) &&
151  {
152  m_raster.getBand( bandIdx )->write( blockXIndex, blockYIndex, blkDataPtr );
153  }
154 
155  --( m_blocksUseCounters[ bandIdx ][ blockYIndex ][ blockXIndex ] );
156  }
157 
158  m_mutex.unlock();
159 
160  m_condVar.notify_all();
161 
162  return true;
163 }
164 
An exception class for the Raster module.
It gives access to values in one band (dimension) of a raster.
It describes one band (or dimension) of a raster.
Base exception class for plugin module.
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
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:242
boost::mutex m_mutex
General sync mutex;.
boost::condition_variable_any m_condVar
Block use request sync variable.
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
AccessPolicy
Supported data access policies (can be used as bitfield).
Raster & m_raster
The input raster.
RasterSynchronizer(Raster &raster, const te::common::AccessPolicy policy)
Constructor.
An abstract class for raster data strucutures.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
An access synchronizer to be used in SynchronizedRaster raster instances.
virtual void write(int x, int y, void *buffer)=0
It writes a data block from the specified buffer.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
virtual void read(int x, int y, void *buffer) const =0
It reads a data block to the specified buffer.
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.