annotate liboctave/util/oct-refcount.h @ 22381:6eba7555794a

style fixes * oct-refcount.h: Style fixes. Rename macros. * dim-vector.h: Update for renamed macros.
author John W. Eaton <jwe@octave.org>
date Thu, 25 Aug 2016 06:45:15 -0400
parents bac0d6f07a3e
children 4caa7b28d183
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
1 /*
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2
22323
bac0d6f07a3e maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents: 21751
diff changeset
3 Copyright (C) 2012-2016 Jaroslav Hajek
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 This file is part of Octave.
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 option) any later version.
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 for more details.
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 */
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22
20791
f7084eae3318 maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents: 19697
diff changeset
23 #if ! defined (octave_oct_refcount_h)
17822
ebb3ef964372 maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents: 17769
diff changeset
24 #define octave_oct_refcount_h 1
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25
21240
7af5ca01ecac allow octave-config.h to be included unconditionally
John W. Eaton <jwe@octave.org>
parents: 21229
diff changeset
26 #include "octave-config.h"
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 12174
diff changeset
27
21244
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 21240
diff changeset
28 #include "octave-config.h"
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 21240
diff changeset
29
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
30 #if (defined (OCTAVE_ENABLE_ATOMIC_REFCOUNT) \
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
31 && (defined (__GNUC__) || defined (_MSC_VER)))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
32
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
33 # if defined (__GNUC__)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
34
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
35 # define OCTAVE_ATOMIC_INCREMENT(x) __sync_add_and_fetch (x, 1)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
36 # define OCTAVE_ATOMIC_DECREMENT(x) __sync_add_and_fetch (x, -1)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
37 # define OCTAVE_ATOMIC_POST_INCREMENT(x) __sync_fetch_and_add (x, 1)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
38 # define OCTAVE_ATOMIC_POST_DECREMENT(x) __sync_fetch_and_add (x, -1)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
39
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
40 # elif defined (_MSC_VER)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
41
21202
f7121e111991 maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents: 21139
diff changeset
42 # include <intrin.h>
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
43
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
44 # define OCTAVE_ATOMIC_INCREMENT(x) \
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
45 _InterlockedIncrement (static_cast<long *> (x))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
46
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
47 # define OCTAVE_ATOMIC_DECREMENT(x) \
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
48 _InterlockedDecrement (static_cast<long *> (x))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
49
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
50 # define OCTAVE_ATOMIC_POST_INCREMENT(x) \
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
51 _InterlockedExchangeAdd (static_cast<long *> (x))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
52
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
53 # define OCTAVE_ATOMIC_POST_DECREMENT(x) \
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
54 _InterlockedExchangeAdd (static_cast<long *> (x))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
55
21202
f7121e111991 maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents: 21139
diff changeset
56 # endif
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
57
20945
9d9270e2f98f eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents: 20791
diff changeset
58 #else
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
59
20945
9d9270e2f98f eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents: 20791
diff changeset
60 // Generic non-locking versions
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
61
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
62 # define OCTAVE_ATOMIC_INCREMENT(x) ++(*(x))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
63 # define OCTAVE_ATOMIC_DECREMENT(x) --(*(x))
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
64 # define OCTAVE_ATOMIC_POST_INCREMENT(x) (*(x))++
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
65 # define OCTAVE_ATOMIC_POST_DECREMENT(x) (*(x))--
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
66
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 12174
diff changeset
67 #endif
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 12174
diff changeset
68
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 // Encapsulates a reference counter.
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21131
diff changeset
70 template <typename T>
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 class octave_refcount
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
72 {
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 public:
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
74
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
75 typedef T count_type;
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
77 octave_refcount (count_type initial_count)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
78 : count (initial_count)
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
79 { }
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 21244
diff changeset
81 // Increment/Decrement. int is postfix.
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
82 count_type operator++ (void)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
83 {
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
84 return OCTAVE_ATOMIC_INCREMENT (&count);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
85 }
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
86
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
87 count_type operator++ (int)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
88 {
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
89 return OCTAVE_ATOMIC_POST_INCREMENT (&count);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
90 }
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
91
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
92 count_type operator-- (void)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
93 {
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
94 return OCTAVE_ATOMIC_DECREMENT (&count);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
95 }
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
96
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
97 count_type operator-- (int)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
98 {
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
99 return OCTAVE_ATOMIC_POST_DECREMENT (&count);
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
100 }
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101
13985
43cc49c7abd1 Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents: 12174
diff changeset
102 operator count_type (void) const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
103 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
104 return static_cast<count_type const volatile&> (count);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
105 }
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
106
14951
4c9fd3e31436 Start of jit support for double matricies
Max Brister <max@2bass.com>
parents: 14138
diff changeset
107 count_type *get (void)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
108 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
109 return &count;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
110 }
14951
4c9fd3e31436 Start of jit support for double matricies
Max Brister <max@2bass.com>
parents: 14138
diff changeset
111
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
112 private:
22381
6eba7555794a style fixes
John W. Eaton <jwe@octave.org>
parents: 22323
diff changeset
113
12125
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 count_type count;
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 };
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
116
a21a3875ca83 implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
117 #endif