| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <ostream> |
| 12 |
|
| 13 |
#include "atlas/runtime/Log.h" |
| 14 |
#include "atlas/util/Object.h" |
| 15 |
#include "atlas/util/ObjectHandle.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace util { |
| 19 |
|
| 20 |
//---------------------------------------------------------------------------------------------------------------------- |
| 21 |
|
| 22 |
int ObjectHandleBase::owners() const { |
| 23 |
return static_cast<int>(object_->owners()); |
| 24 |
} |
| 25 |
|
| 26 |
void ObjectHandleBase::release() { |
| 27 |
object_->lock(); |
| 28 |
object_->detach(); /* lock/unlock in detach() isn't sufficient, else there is race |
| 29 |
condition on owners() */ |
| 30 |
|
| 31 |
if (object_->owners() == 0) { |
| 32 |
object_->unlock(); |
| 33 |
delete object_; |
| 34 |
object_ = nullptr; |
| 35 |
return; |
| 36 |
} |
| 37 |
object_->unlock(); |
| 38 |
object_ = nullptr; |
| 39 |
} |
| 40 |
|
| 41 |
void ObjectHandleBase::assign(const ObjectHandleBase& other) { |
| 42 |
assign(other.object_); |
| 43 |
} |
| 44 |
|
| 45 |
void ObjectHandleBase::assign(const Object* other) { |
| 46 |
if (object_) { |
| 47 |
release(); |
| 48 |
} |
| 49 |
|
| 50 |
object_ = const_cast<Object*>(other); |
| 51 |
|
| 52 |
attach(); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
void ObjectHandleBase::attach() { |
| 57 |
if (!null()) { |
| 58 |
object_->attach(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// ------------------------------------------------------------------ |
| 63 |
|
| 64 |
} // namespace util |
| 65 |
} // namespace atlas |