Mercurial > octave
annotate liboctave/util/oct-refcount.h @ 26377:fff643eb3514
maint: Merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 02 Jan 2019 16:45:58 -0500 |
parents | f23f27e78aa2 00f796120a6d |
children | 8498dccc15c7 |
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 |
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
3 Copyright (C) 2012-2019 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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23679
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23679
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
10 (at your option) any later version. |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
15 GNU General Public License for more details. |
12125
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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23679
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
12125
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 |
22381 | 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 | |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
40 # include <intrin.h> |
22381 | 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 | |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
54 # endif |
22381 | 55 |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
56 #else |
22381 | 57 |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
58 // Generic non-locking versions |
22381 | 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 | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
12174
diff
changeset
|
65 #endif |
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
12174
diff
changeset
|
66 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
67 namespace octave |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
68 { |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
69 |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
70 // Encapsulates a reference counter. |
22381 | 71 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
72 template <typename T> |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
73 class refcount |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
74 { |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
75 public: |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
76 |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
77 typedef T count_type; |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
78 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
79 refcount (count_type initial_count) |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
80 : count (initial_count) |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
81 { } |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
82 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
83 // Increment/Decrement. int is postfix. |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
84 count_type operator++ (void) |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
85 { |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
86 return OCTAVE_ATOMIC_INCREMENT (&count); |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
87 } |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
88 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
89 count_type operator++ (int) |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
90 { |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
91 return OCTAVE_ATOMIC_POST_INCREMENT (&count); |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
92 } |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
93 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
94 count_type operator-- (void) |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
95 { |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
96 return OCTAVE_ATOMIC_DECREMENT (&count); |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
97 } |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
98 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
99 count_type operator-- (int) |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
100 { |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
101 return OCTAVE_ATOMIC_POST_DECREMENT (&count); |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
102 } |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
103 |
23679
ece6f43304e5
new function to convert dim_vector to Array<octave_idx_type>
John W. Eaton <jwe@octave.org>
parents:
23615
diff
changeset
|
104 count_type value (void) const |
ece6f43304e5
new function to convert dim_vector to Array<octave_idx_type>
John W. Eaton <jwe@octave.org>
parents:
23615
diff
changeset
|
105 { |
ece6f43304e5
new function to convert dim_vector to Array<octave_idx_type>
John W. Eaton <jwe@octave.org>
parents:
23615
diff
changeset
|
106 return static_cast<count_type const volatile&> (count); |
ece6f43304e5
new function to convert dim_vector to Array<octave_idx_type>
John W. Eaton <jwe@octave.org>
parents:
23615
diff
changeset
|
107 } |
ece6f43304e5
new function to convert dim_vector to Array<octave_idx_type>
John W. Eaton <jwe@octave.org>
parents:
23615
diff
changeset
|
108 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
109 operator count_type (void) const |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
110 { |
23679
ece6f43304e5
new function to convert dim_vector to Array<octave_idx_type>
John W. Eaton <jwe@octave.org>
parents:
23615
diff
changeset
|
111 return value (); |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
112 } |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 |
23446
cd4e1ee28716
maint: Use convention 'void * fcn ()' for functions which return pointers.
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
114 count_type * get (void) |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
115 { |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
116 return &count; |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
117 } |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
118 |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
119 private: |
14951
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14138
diff
changeset
|
120 |
23012
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
121 count_type count; |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
122 }; |
27e4ec3b0b49
move octave_refcount inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
123 } |
22381 | 124 |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
125 #endif |