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
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2013-03-15 18:05:37 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2013-03-15 18:05:37 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2013-03-15 18:05:37 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2013-03-15 18:05:37 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-08 09:10:46 +00:00
|
|
|
#ifndef GNSS_SDR_CONCURRENT_MAP_H
|
|
|
|
#define GNSS_SDR_CONCURRENT_MAP_H
|
2013-03-15 18:05:37 +00:00
|
|
|
|
|
|
|
#include <map>
|
2019-07-21 10:55:59 +00:00
|
|
|
#include <mutex>
|
2014-01-12 20:07:38 +00:00
|
|
|
#include <utility>
|
2018-02-26 02:15:53 +00:00
|
|
|
|
2020-11-01 12:37:19 +00:00
|
|
|
/** \addtogroup Core
|
|
|
|
* \{ */
|
|
|
|
/** \addtogroup Core_Receiver core_receiver
|
|
|
|
* \{ */
|
|
|
|
|
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
|
|
|
public:
|
|
|
|
void write(int key, Data const& data)
|
|
|
|
{
|
2019-07-21 10:55:59 +00:00
|
|
|
std::unique_lock<std::mutex> 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
|
|
|
{
|
2019-07-21 10:55:59 +00:00
|
|
|
std::unique_lock<std::mutex> 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
|
|
|
{
|
2019-07-21 10:55:59 +00:00
|
|
|
std::unique_lock<std::mutex> 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)
|
|
|
|
{
|
2019-07-21 10:55:59 +00:00
|
|
|
std::unique_lock<std::mutex> lock(the_mutex);
|
2013-07-04 13:47:40 +00:00
|
|
|
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
|
|
|
}
|
2019-07-01 21:44:42 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<int, Data> the_map;
|
2019-07-21 10:55:59 +00:00
|
|
|
mutable std::mutex the_mutex;
|
2013-03-15 18:05:37 +00:00
|
|
|
};
|
2013-07-04 13:47:40 +00:00
|
|
|
|
2020-11-01 12:37:19 +00:00
|
|
|
|
|
|
|
/** \} */
|
|
|
|
/** \} */
|
2020-02-08 09:10:46 +00:00
|
|
|
#endif // GNSS_SDR_CONCURRENT_MAP_H
|