CPPMyth
Library to interoperate with MythTV server
mythintrinsic.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 "mythintrinsic.h"
23 
24 #include <cppmyth_config.h>
25 #if __cplusplus >= 201103L
26 #include <atomic>
27 typedef std::atomic<int> counter_t;
28 #define GETVALUE(p) (p)->load()
29 #define INCREMENT(p) ((p)->fetch_add(1, std::memory_order_relaxed) + 1)
30 #define DECREMENT(p) ((p)->fetch_sub(1, std::memory_order_relaxed) - 1)
31 
32 #elif defined _MSC_VER
33 #define WIN32_LEAN_AND_MEAN
34 #include <Windows.h>
35 typedef volatile LONG counter_t;
36 #define GETVALUE(p) (*(p))
37 #define INCREMENT(p) InterlockedIncrement(p)
38 #define DECREMENT(p) InterlockedDecrement(p)
39 
40 #elif defined __APPLE__
41 #include <libkern/OSAtomic.h>
42 typedef volatile int32_t counter_t;
43 #define GETVALUE(p) (*(p))
44 #define INCREMENT(p) OSAtomicIncrement32(p)
45 #define DECREMENT(p) OSAtomicDecrement32(p)
46 
47 #elif defined HAS_BUILTIN_SYNC_ADD_AND_FETCH
48 typedef volatile int counter_t;
49 #define GETVALUE(p) (*(p))
50 #define INCREMENT(p) __sync_add_and_fetch(p, 1)
51 #if defined HAS_BUILTIN_SYNC_SUB_AND_FETCH
52 #define DECREMENT(p) __sync_sub_and_fetch(p, 1)
53 #else
54 #define DECREMENT(p) __sync_add_and_fetch(p, -1)
55 #endif
56 
57 #else
58 #include "private/atomic.h"
59 #ifndef ATOMIC_NOATOMIC
60 typedef Myth::atomic<int> counter_t;
61 #define GETVALUE(p) (p)->load()
62 #define INCREMENT(p) (p)->add_fetch(1)
63 #define DECREMENT(p) (p)->sub_fetch(1)
64 //
65 // Don't know how to do atomic operation for the architecture
66 //
67 #elif defined USE_LOCKED
68 #include "mythlocked.h"
69 typedef Myth::LockedNumber<int> counter_t;
70 #define GETVALUE(p) (p)->Load()
71 #define INCREMENT(p) (p)->Add(1)
72 #define DECREMENT(p) (p)->Sub(1)
73 
74 #else
75 #error Atomic add/sub are not. Overcome using definition USE_LOCKED.
76 #endif
77 #endif
78 
79 using namespace Myth;
80 
81 namespace Myth
82 {
84  {
85  counter_t counter;
86  Counter(int val) : counter(val) {}
87  };
88 }
89 
90 IntrinsicCounter::IntrinsicCounter(int val)
91 : m_ptr(new Counter(val))
92 {
93 }
94 
95 IntrinsicCounter::~IntrinsicCounter()
96 {
97  delete m_ptr;
98 }
99 
100 int IntrinsicCounter::GetValue()
101 {
102  return GETVALUE(&m_ptr->counter);
103 }
104 
105 int IntrinsicCounter::Increment()
106 {
107  return INCREMENT(&m_ptr->counter);
108 }
109 
110 int IntrinsicCounter::Decrement()
111 {
112  return DECREMENT(&m_ptr->counter);
113 }
This is the main namespace that encloses all public classes.
Definition: mythcontrol.h:29