GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-refcount.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 #if (defined (OCTAVE_ENABLE_ATOMIC_REFCOUNT) \
29  && (defined (__GNUC__) || defined (_MSC_VER)))
30 
31 # if defined (__GNUC__)
32 
33 # define OCTAVE_ATOMIC_INCREMENT(x) __sync_add_and_fetch (x, 1)
34 # define OCTAVE_ATOMIC_DECREMENT(x) __sync_add_and_fetch (x, -1)
35 # define OCTAVE_ATOMIC_POST_INCREMENT(x) __sync_fetch_and_add (x, 1)
36 # define OCTAVE_ATOMIC_POST_DECREMENT(x) __sync_fetch_and_add (x, -1)
37 
38 # elif defined (_MSC_VER)
39 
40 # include <intrin.h>
41 
42 # define OCTAVE_ATOMIC_INCREMENT(x) \
43  _InterlockedIncrement (static_cast<long *> (x))
44 
45 # define OCTAVE_ATOMIC_DECREMENT(x) \
46  _InterlockedDecrement (static_cast<long *> (x))
47 
48 # define OCTAVE_ATOMIC_POST_INCREMENT(x) \
49  _InterlockedExchangeAdd (static_cast<long *> (x))
50 
51 # define OCTAVE_ATOMIC_POST_DECREMENT(x) \
52  _InterlockedExchangeAdd (static_cast<long *> (x))
53 
54 # endif
55 
56 #else
57 
58 // Generic non-locking versions
59 
60 # define OCTAVE_ATOMIC_INCREMENT(x) ++(*(x))
61 # define OCTAVE_ATOMIC_DECREMENT(x) --(*(x))
62 # define OCTAVE_ATOMIC_POST_INCREMENT(x) (*(x))++
63 # define OCTAVE_ATOMIC_POST_DECREMENT(x) (*(x))--
64 
65 #endif
66 
67 namespace octave
68 {
69 
70  // Encapsulates a reference counter.
71 
72  template <typename T>
73  class refcount
74  {
75  public:
76 
77  typedef T count_type;
78 
79  refcount (count_type initial_count)
80  : count (initial_count)
81  { }
82 
83  // Increment/Decrement. int is postfix.
85  {
87  }
88 
90  {
92  }
93 
95  {
97  }
98 
100  {
102  }
103 
104  count_type value (void) const
105  {
106  return static_cast<count_type const volatile&> (count);
107  }
108 
109  operator count_type (void) const
110  {
111  return value ();
112  }
113 
114  count_type * get (void)
115  {
116  return &count;
117  }
118 
119  private:
120 
122  };
123 }
124 
125 template <typename T>
126 using octave_refcount OCTAVE_DEPRECATED (4.4, "use 'octave::refcount' instead") = octave::refcount<T>;
127 
128 #endif
#define OCTAVE_ATOMIC_DECREMENT(x)
Definition: oct-refcount.h:61
count_type operator++(void)
Definition: oct-refcount.h:84
#define OCTAVE_ATOMIC_INCREMENT(x)
Definition: oct-refcount.h:60
refcount(count_type initial_count)
Definition: oct-refcount.h:79
count_type count
Definition: oct-refcount.h:121
#define OCTAVE_ATOMIC_POST_INCREMENT(x)
Definition: oct-refcount.h:62
#define OCTAVE_ATOMIC_POST_DECREMENT(x)
Definition: oct-refcount.h:63
count_type value(void) const
Definition: oct-refcount.h:104
count_type operator--(void)
Definition: oct-refcount.h:94