source: ThirdParty/mpqc_open/src/lib/util/ref/ref.cc@ 6b1a6a

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 6b1a6a was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 5.4 KB
Line 
1//
2// ref.cc --- implementation of the reference counting classes
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUC__
29#pragma implementation
30#endif
31
32#include <typeinfo>
33
34#include <util/ref/ref.h>
35#include <util/misc/exenv.h>
36
37using namespace std;
38
39#if REF_USE_LOCKS
40
41#if HAVE_STHREAD
42
43#include <synch.h>
44#include <thread.h>
45
46typedef mutex_t sc_lock_t;
47
48static unsigned int __base_nlock__;
49static int __base_locker__;
50
51static int
52__init_lock__(sc_lock_t* inLock, int inCount)
53{
54 for (int i=0; i < inCount; i++)
55 mutex_init(&inLock[i], USYNC_THREAD, 0);
56 return 1;
57}
58
59#define __LOCK(l) mutex_lock(&l)
60#define __UNLOCK(l) mutex_unlock(&l)
61
62#elif HAVE_PTHREAD
63
64#include <pthread.h>
65
66typedef pthread_mutex_t sc_lock_t;
67
68static int
69__init_lock__(sc_lock_t* inLock, int inCount)
70{
71 for (int i=0; i < inCount; i++)
72 pthread_mutex_init(&inLock[i], 0);
73 return 1;
74}
75
76#define __LOCK(l) pthread_mutex_lock(&l)
77#define __UNLOCK(l) pthread_mutex_unlock(&l)
78
79#elif HAVE_CREATETHREAD
80
81#include <windows.h>
82
83typedef HANDLE sc_lock_t;
84
85HANDLE __base_lock__ = 0;
86
87static int
88__init_lock__(sc_lock_t* inLock, int inCount)
89{
90 for (int i=0; i < inCount; i++)
91 inLock[i] = CreateMutex(0, FALSE, 0);
92 return 1;
93}
94
95// windows threads are recursive, so no fanciness is required
96#define __LOCK(l) WaitForSingleObject(l, INFINITE)
97#define __UNLOCK(l) ReleaseMutex(l)
98
99#else /* !PTHREAD && !STHREAD && !CREATETHREAD */
100
101#define __LOCK(l) 0
102#define __UNLOCK(l) 0
103
104#endif /* HAVE_STHREAD */
105
106/*
107 * this is the number of locks to use in the round-robin.
108 * since an unsigned char is used for the lock handle,
109 * this cannot be greater than 255.
110 */
111#define NLOCKS 128
112
113static sc_lock_t sRefLocks[NLOCKS];
114static int sRefLocksInit = __init_lock__(sRefLocks, NLOCKS);
115static unsigned char sRefLock = 0;
116
117#else /* !REF_USE_LOCKS */
118
119#define __LOCK(l) 0
120#define __UNLOCK(l) 0
121
122#endif /* !REF_USE_LOCKS */
123
124using namespace sc;
125
126int
127RefCount::lock_ptr() const
128{
129#if REF_USE_LOCKS
130 if (ref_lock_ == 0xff)
131 return 1;
132 return __LOCK(sRefLocks[ref_lock_]);
133#else
134 return 1;
135#endif
136}
137
138int
139RefCount::unlock_ptr() const
140{
141#if REF_USE_LOCKS
142 if (ref_lock_ == 0xff)
143 return 1;
144 return __UNLOCK(sRefLocks[ref_lock_]);
145#else
146 return 1;
147#endif
148}
149
150void
151RefCount::use_locks(bool inVal)
152{
153#if REF_USE_LOCKS
154 if (inVal) {
155 ref_lock_ = sRefLock;
156 unsigned char tmp_sRefLock = sRefLock+1;
157 if (tmp_sRefLock >= NLOCKS)
158 tmp_sRefLock = 0;
159 sRefLock = tmp_sRefLock;
160 }
161 else
162 ref_lock_ = 0xff;
163#endif
164}
165
166
167void
168RefCount::error(const char * w) const
169{
170 ExEnv::errn() << "RefCount: ERROR: " << w << endl;
171 ExEnv::errn() << "The type name is " << typeid(*this).name()
172 << std::endl;
173 abort();
174}
175
176void
177RefCount::too_many_refs() const
178{
179 error("Too many refs.");
180}
181
182void
183RefCount::not_enough_refs() const
184{
185 error("Ref count dropped below zero.");
186}
187
188RefCount::~RefCount()
189{
190#if REF_MANAGE
191 if (managed() && nreference()) {
192 error("Deleting a referenced object.");
193 }
194#endif
195}
196
197///////////////////////////////////////////////////////////////////////
198
199void
200RefBase::warn ( const char * msg) const
201{
202 ExEnv::errn() << "WARNING: " << msg << endl;
203}
204void
205RefBase::warn_ref_to_stack() const
206{
207 warn("Ref: creating a reference to stack data");
208}
209void
210RefBase::warn_skip_stack_delete() const
211{
212 warn("Ref: skipping delete of object on the stack");
213}
214void
215RefBase::warn_bad_ref_count() const
216{
217 warn("Ref: bad reference count in referenced object\n");
218}
219void
220RefBase::ref_info(RefCount*p, ostream& os) const
221{
222 if (p)
223 os << "nreference() = " << p->nreference() << endl;
224 else
225 os << "reference is null" << endl;
226}
227
228void
229RefBase::require_nonnull() const
230{
231 if (parentpointer() == 0) {
232 ExEnv::errn() << "RefBase: needed a nonnull pointer but got null"
233 << endl;
234 abort();
235 }
236}
237
238RefBase::~RefBase()
239{
240}
241
242void
243RefBase::check_pointer() const
244{
245 if (parentpointer() && parentpointer()->nreference() <= 0) {
246 warn_bad_ref_count();
247 }
248}
249
250void
251RefBase::ref_info(ostream& os) const
252{
253 RefBase::ref_info(parentpointer(),os);
254}
255
256void
257RefBase::reference(RefCount *p)
258{
259 if (p) {
260#if REF_CHECK_STACK
261 if (DO_REF_CHECK_STACK(p)) {
262 DO_REF_UNMANAGE(p);
263 warn_ref_to_stack();
264 }
265#endif
266 p->reference();
267 }
268}
269
270int
271RefBase::dereference(RefCount *p)
272{
273 if (p)
274 return p->dereference();
275 else
276 return -1;
277}
278
279/////////////////////////////////////////////////////////////////////////////
280
281// Local Variables:
282// mode: c++
283// c-file-style: "CLJ"
284// End:
Note: See TracBrowser for help on using the repository browser.