CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
mythsharedptr.cpp
1/*
2 * Copyright (C) 2015 Jean-Luc Barriere
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#include "mythsharedptr.h"
23#include "private/os/threads/atomic.h"
24
25// Compatibility with C++98 remains
26
27using namespace Myth;
28
29shared_ptr_base::shared_ptr_base()
30: pc(NULL)
31, spare(NULL) { }
32
33shared_ptr_base::~shared_ptr_base()
34{
35 clear_counter();
36 if (spare != NULL)
37 delete spare;
38}
39
40shared_ptr_base::shared_ptr_base(const shared_ptr_base& s)
41: pc(s.pc)
42, spare(NULL)
43{
44 if (pc != NULL && (pc->load() == 0 || pc->add_fetch(1) < 2))
45 pc = NULL;
46}
47
48shared_ptr_base& shared_ptr_base::operator=(const shared_ptr_base& s)
49{
50 if (this != &s)
51 {
52 clear_counter();
53 pc = s.pc;
54 if (pc != NULL && (pc->load() == 0 || pc->add_fetch(1) < 2))
55 pc = NULL;
56 }
57 return *this;
58}
59
60bool shared_ptr_base::clear_counter()
61{
62 if (pc != NULL && pc->load() > 0 && pc->sub_fetch(1) == 0)
63 {
64 /* delete later */
65 if (spare != NULL)
66 delete spare;
67 spare = pc;
68 pc = NULL;
69 return true;
70 }
71 pc = NULL;
72 return false;
73}
74
75void shared_ptr_base::reset_counter()
76{
77 clear_counter();
78 if (spare != NULL)
79 {
80 /* reuse the spare */
81 spare->store(1);
82 pc = spare;
83 spare = NULL;
84 }
85 else
86 {
87 /* create a new */
88 pc = new OS::Atomic(1);
89 }
90}
91
92void shared_ptr_base::swap_counter(shared_ptr_base& s)
93{
94 OS::Atomic* _pc = pc;
95 pc = s.pc;
96 s.pc = _pc;
97}
98
99int shared_ptr_base::get_count() const
100{
101 return (pc != NULL ? pc->load() : 0);
102}
This is the main namespace that encloses all public classes.
Definition mythcontrol.h:30