2013-03-15 18:05:37 +00:00
|
|
|
/*!
|
2013-10-18 18:26:06 +00:00
|
|
|
* \file concurrent_map.h
|
2013-03-15 18:05:37 +00:00
|
|
|
* \brief Interface of a thread-safe std::map
|
|
|
|
* \author Javier Arribas, 2011. jarribas(at)cttc.es
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
2018-05-13 20:49:11 +00:00
|
|
|
* Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors)
|
2013-03-15 18:05:37 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
|
|
|
* GNSS-SDR is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2014-05-07 21:35:44 +00:00
|
|
|
* (at your option) any later version.
|
2013-03-15 18:05:37 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-05-13 20:49:11 +00:00
|
|
|
* along with GNSS-SDR. If not, see <https://www.gnu.org/licenses/>.
|
2013-03-15 18:05:37 +00:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GNSS_SDR_CONCURRENT_MAP_H
|
|
|
|
#define GNSS_SDR_CONCURRENT_MAP_H
|
|
|
|
|
2018-02-26 02:15:53 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2013-03-15 18:05:37 +00:00
|
|
|
#include <map>
|
2014-01-12 20:07:38 +00:00
|
|
|
#include <utility>
|
2018-02-26 02:15:53 +00:00
|
|
|
|
2013-03-15 18:05:37 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename Data>
|
2013-03-15 18:05:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief This class implements a thread-safe std::map
|
|
|
|
*
|
|
|
|
*/
|
2019-02-22 09:47:24 +00:00
|
|
|
class Concurrent_Map
|
2013-03-15 18:05:37 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
typedef typename std::map<int, Data>::iterator Data_iterator; // iterator is scope dependent
|
2013-03-15 18:05:37 +00:00
|
|
|
private:
|
2018-03-03 01:03:39 +00:00
|
|
|
std::map<int, Data> the_map;
|
2013-03-15 18:05:37 +00:00
|
|
|
boost::mutex the_mutex;
|
2018-03-03 01:03:39 +00:00
|
|
|
|
2013-03-15 18:05:37 +00:00
|
|
|
public:
|
|
|
|
void write(int key, Data const& data)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
2014-05-06 09:59:48 +00:00
|
|
|
Data_iterator data_iter;
|
|
|
|
data_iter = the_map.find(key);
|
|
|
|
if (data_iter != the_map.end())
|
2014-05-07 21:35:44 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
data_iter->second = data; // update
|
2014-05-07 21:35:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
the_map.insert(std::pair<int, Data>(key, data)); // insert SILENTLY fails if the item already exists in the map!
|
2014-05-07 21:35:44 +00:00
|
|
|
}
|
2013-03-15 18:05:37 +00:00
|
|
|
lock.unlock();
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
std::map<int, Data> get_map_copy()
|
2013-07-04 13:47:40 +00:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
2018-03-03 01:03:39 +00:00
|
|
|
std::map<int, Data> map_aux = the_map;
|
2013-07-04 13:47:40 +00:00
|
|
|
lock.unlock();
|
2015-05-12 21:45:56 +00:00
|
|
|
return map_aux;
|
2013-07-04 13:47:40 +00:00
|
|
|
}
|
2013-03-15 18:05:37 +00:00
|
|
|
|
2015-05-12 21:45:56 +00:00
|
|
|
size_t size()
|
2013-03-15 18:05:37 +00:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
2015-05-12 21:45:56 +00:00
|
|
|
size_t size_ = the_map.size();
|
2013-03-15 18:05:37 +00:00
|
|
|
lock.unlock();
|
2015-05-12 21:45:56 +00:00
|
|
|
return size_;
|
2013-03-15 18:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool read(int key, Data& p_data)
|
|
|
|
{
|
2013-07-04 13:47:40 +00:00
|
|
|
boost::mutex::scoped_lock lock(the_mutex);
|
|
|
|
Data_iterator data_iter;
|
|
|
|
data_iter = the_map.find(key);
|
|
|
|
if (data_iter != the_map.end())
|
|
|
|
{
|
|
|
|
p_data = data_iter->second;
|
|
|
|
lock.unlock();
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
lock.unlock();
|
|
|
|
return false;
|
2013-03-15 18:05:37 +00:00
|
|
|
}
|
|
|
|
};
|
2013-07-04 13:47:40 +00:00
|
|
|
|
2013-03-15 18:05:37 +00:00
|
|
|
#endif
|