GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-refcount.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2017 Jaroslav Hajek
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_oct_refcount_h)
24 #define octave_oct_refcount_h 1
25 
26 #include "octave-config.h"
27 
28 #include "octave-config.h"
29 
30 #if (defined (OCTAVE_ENABLE_ATOMIC_REFCOUNT) \
31  && (defined (__GNUC__) || defined (_MSC_VER)))
32 
33 # if defined (__GNUC__)
34 
35 # define OCTAVE_ATOMIC_INCREMENT(x) __sync_add_and_fetch (x, 1)
36 # define OCTAVE_ATOMIC_DECREMENT(x) __sync_add_and_fetch (x, -1)
37 # define OCTAVE_ATOMIC_POST_INCREMENT(x) __sync_fetch_and_add (x, 1)
38 # define OCTAVE_ATOMIC_POST_DECREMENT(x) __sync_fetch_and_add (x, -1)
39 
40 # elif defined (_MSC_VER)
41 
42 # include <intrin.h>
43 
44 # define OCTAVE_ATOMIC_INCREMENT(x) \
45  _InterlockedIncrement (static_cast<long *> (x))
46 
47 # define OCTAVE_ATOMIC_DECREMENT(x) \
48  _InterlockedDecrement (static_cast<long *> (x))
49 
50 # define OCTAVE_ATOMIC_POST_INCREMENT(x) \
51  _InterlockedExchangeAdd (static_cast<long *> (x))
52 
53 # define OCTAVE_ATOMIC_POST_DECREMENT(x) \
54  _InterlockedExchangeAdd (static_cast<long *> (x))
55 
56 # endif
57 
58 #else
59 
60 // Generic non-locking versions
61 
62 # define OCTAVE_ATOMIC_INCREMENT(x) ++(*(x))
63 # define OCTAVE_ATOMIC_DECREMENT(x) --(*(x))
64 # define OCTAVE_ATOMIC_POST_INCREMENT(x) (*(x))++
65 # define OCTAVE_ATOMIC_POST_DECREMENT(x) (*(x))--
66 
67 #endif
68 
69 // Encapsulates a reference counter.
70 template <typename T>
72 {
73 public:
74 
75  typedef T count_type;
76 
77  octave_refcount (count_type initial_count)
78  : count (initial_count)
79  { }
80 
81  // Increment/Decrement. int is postfix.
82  count_type operator++ (void)
83  {
85  }
86 
87  count_type operator++ (int)
88  {
90  }
91 
92  count_type operator-- (void)
93  {
95  }
96 
97  count_type operator-- (int)
98  {
100  }
101 
102  operator count_type (void) const
103  {
104  return static_cast<count_type const volatile&> (count);
105  }
106 
107  count_type *get (void)
108  {
109  return &count;
110  }
111 
112 private:
113 
114  count_type count;
115 };
116 
117 #endif
#define OCTAVE_ATOMIC_DECREMENT(x)
Definition: oct-refcount.h:63
#define OCTAVE_ATOMIC_INCREMENT(x)
Definition: oct-refcount.h:62
octave_refcount(count_type initial_count)
Definition: oct-refcount.h:77
#define OCTAVE_ATOMIC_POST_INCREMENT(x)
Definition: oct-refcount.h:64
count_type operator++(void)
Definition: oct-refcount.h:82
#define OCTAVE_ATOMIC_POST_DECREMENT(x)
Definition: oct-refcount.h:65
count_type count
Definition: oct-refcount.h:114
count_type operator--(void)
Definition: oct-refcount.h:92