OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TLB.h
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/TLB.h
3  *
4  * Copyright (C) 2008-2010 OpenCog Foundation
5  * Copyright (C) 2002-2007 Novamente LLC
6  * Copyright (C) 2014, 2015 Linas Vepstas
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #ifndef _OPENCOG_TLB_H
26 #define _OPENCOG_TLB_H
27 
28 #include <atomic>
29 
30 #include <opencog/util/Logger.h>
31 #include <opencog/atomspace/Atom.h>
33 
34 class TLBUTest;
35 class BasicSaveUTest;
36 
37 namespace opencog
38 {
39 
40 class AtomSpaceBenchmark;
41 class AtomStorage;
42 class AtomTable;
43 
62 class TLB
63 {
64  friend class Atom;
65  friend class AtomSpaceBenchmark;
66  friend class AtomStorage;
67  friend class AtomTable;
68  friend class ::TLBUTest;
69  friend class ::BasicSaveUTest;
70 
71 private:
72 
76  TLB() {}
77 
78  // Thread-safe atomic
79  static std::atomic<UUID> _brk_uuid;
80 
87  static inline void addAtom(AtomPtr atom);
88 
89  static inline bool isInvalidHandle(const Handle& h);
90 
91  static inline bool isValidHandle(const Handle& h);
92 
93  static UUID getMaxUUID(void) { return _brk_uuid; }
94 
98  static inline void reserve_range(UUID lo, UUID hi)
99  {
100  if (hi <= lo)
101  throw InvalidParamException(TRACE_INFO,
102  "Bad argument order.");
103  UUID extent = hi - lo + 1;
104 
105  UUID oldlo = _brk_uuid.fetch_add(extent, std::memory_order_relaxed);
106 
107  if (lo < oldlo)
108  throw InvalidParamException(TRACE_INFO,
109  "Bad range reserve.");
110  }
111 
115  static inline UUID reserve_extent(UUID extent)
116  {
117  return _brk_uuid.fetch_add(extent, std::memory_order_relaxed);
118  }
119 
123  static inline void reserve_upto(UUID hi)
124  {
125  if (hi < _brk_uuid) return;
126  UUID extent = hi - _brk_uuid + 1;
127  _brk_uuid.fetch_add(extent, std::memory_order_relaxed);
128  }
129 };
130 
131 inline bool TLB::isInvalidHandle(const Handle& h)
132 {
133  return (h == Handle::UNDEFINED) ||
134  (h.value() >= _brk_uuid);
135 }
136 
137 inline bool TLB::isValidHandle(const Handle& h)
138 {
139  return not isInvalidHandle(h);
140 }
141 
143 {
144  if (atom->_uuid != Handle::UNDEFINED.value())
145  throw InvalidParamException(TRACE_INFO,
146  "Atom is already in the TLB!");
147 
148  atom->_uuid = _brk_uuid.fetch_add(1, std::memory_order_relaxed);
149 }
150 
151 } // namespace opencog
152 
153 #endif // _OPENCOG_TLB_H
static bool isInvalidHandle(const Handle &h)
Definition: TLB.h:131
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
static UUID getMaxUUID(void)
Definition: TLB.h:93
static void reserve_range(UUID lo, UUID hi)
Definition: TLB.h:98
static void reserve_upto(UUID hi)
Definition: TLB.h:123
TLB()
Definition: TLB.h:76
static const Handle UNDEFINED
Definition: Handle.h:77
unsigned long UUID
UUID == Universally Unique Identifier.
Definition: Handle.h:46
static bool isValidHandle(const Handle &h)
Definition: TLB.h:137
UUID value(void) const
Definition: Handle.h:85
static UUID reserve_extent(UUID extent)
Definition: TLB.h:115
static std::atomic< UUID > _brk_uuid
Definition: TLB.h:79
static void addAtom(AtomPtr atom)
Definition: TLB.h:142