blob: 0dd1f586665135c10537f278263a5541863c755b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/* -*- c++ -*- */
/*
* Copyright 2008,2009,2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gnuradio/hier_block2.h>
#include <gnuradio/sptr_magic.h>
#include <map>
#include <stdexcept>
#include <gnuradio/thread/thread.h>
namespace gnuradio {
static gr::thread::mutex s_mutex;
typedef std::map<gr::basic_block*, gr::basic_block_sptr> sptr_map;
static sptr_map s_map;
struct disarmable_deleter {
bool armed;
disarmable_deleter() { armed = true; }
void operator()(void* p) const
{
if (armed)
delete static_cast<gr::basic_block*>(p);
}
void disarm() { armed = false; }
};
void detail::sptr_magic::create_and_stash_initial_sptr(gr::hier_block2* p)
{
gr::basic_block_sptr sptr(p, disarmable_deleter());
gr::thread::scoped_lock guard(s_mutex);
s_map.insert(sptr_map::value_type(static_cast<gr::basic_block*>(p), sptr));
}
void detail::sptr_magic::cancel_initial_sptr(gr::hier_block2* p)
{
gr::thread::scoped_lock guard(s_mutex);
sptr_map::iterator pos = s_map.find(static_cast<gr::basic_block*>(p));
if (pos == s_map.end())
return; /* Not in the map, nothing to do */
gr::basic_block_sptr sptr = pos->second;
s_map.erase(pos);
std::get_deleter<disarmable_deleter, gr::basic_block>(sptr)->disarm();
}
gr::basic_block_sptr detail::sptr_magic::fetch_initial_sptr(gr::basic_block* p)
{
/*
* If p isn't a subclass of gr::hier_block2, just create the
* shared ptr and return it.
*/
gr::hier_block2* hb2 = dynamic_cast<gr::hier_block2*>(p);
if (!hb2) {
return gr::basic_block_sptr(p);
}
/*
* p is a subclass of gr::hier_block2, thus we've already created the shared pointer
* and stashed it away. Fish it out and return it.
*/
gr::thread::scoped_lock guard(s_mutex);
sptr_map::iterator pos = s_map.find(static_cast<gr::basic_block*>(p));
if (pos == s_map.end())
throw std::invalid_argument("sptr_magic: invalid pointer!");
gr::basic_block_sptr sptr = pos->second;
s_map.erase(pos);
return sptr;
}
} // namespace gnuradio
|