mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-29 16:20:11 -04:00
pstats: Switch from AtomicAdjust to C++11-style atomics
This commit is contained in:
parent
e27162df0b
commit
77b0d2d6a7
@ -16,8 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
INLINE int PStatClient::
|
INLINE int PStatClient::
|
||||||
get_num_collectors() const {
|
get_num_collectors() const {
|
||||||
ReMutexHolder holder(_lock);
|
return _num_collectors.load(std::memory_order_relaxed);
|
||||||
return (int)_num_collectors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,7 +24,7 @@ get_num_collectors() const {
|
|||||||
*/
|
*/
|
||||||
INLINE PStatCollectorDef *PStatClient::
|
INLINE PStatCollectorDef *PStatClient::
|
||||||
get_collector_def(int index) const {
|
get_collector_def(int index) const {
|
||||||
nassertr(index >= 0 && index < _num_collectors, nullptr);
|
nassertr(index >= 0 && index < get_num_collectors(), nullptr);
|
||||||
|
|
||||||
return get_collector_ptr(index)->get_def(this, index);
|
return get_collector_ptr(index)->get_def(this, index);
|
||||||
}
|
}
|
||||||
@ -35,8 +34,7 @@ get_collector_def(int index) const {
|
|||||||
*/
|
*/
|
||||||
INLINE int PStatClient::
|
INLINE int PStatClient::
|
||||||
get_num_threads() const {
|
get_num_threads() const {
|
||||||
ReMutexHolder holder(_lock);
|
return _num_threads.load(std::memory_order_relaxed);
|
||||||
return (int)_num_threads;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,7 +42,7 @@ get_num_threads() const {
|
|||||||
*/
|
*/
|
||||||
INLINE std::string PStatClient::
|
INLINE std::string PStatClient::
|
||||||
get_thread_name(int index) const {
|
get_thread_name(int index) const {
|
||||||
nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), std::string());
|
nassertr(index >= 0 && index < get_num_threads(), std::string());
|
||||||
return get_thread_ptr(index)->_name;
|
return get_thread_ptr(index)->_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +51,7 @@ get_thread_name(int index) const {
|
|||||||
*/
|
*/
|
||||||
INLINE std::string PStatClient::
|
INLINE std::string PStatClient::
|
||||||
get_thread_sync_name(int index) const {
|
get_thread_sync_name(int index) const {
|
||||||
nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), std::string());
|
nassertr(index >= 0 && index < get_num_threads(), std::string());
|
||||||
return get_thread_ptr(index)->_sync_name;
|
return get_thread_ptr(index)->_sync_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +60,7 @@ get_thread_sync_name(int index) const {
|
|||||||
*/
|
*/
|
||||||
INLINE PT(Thread) PStatClient::
|
INLINE PT(Thread) PStatClient::
|
||||||
get_thread_object(int index) const {
|
get_thread_object(int index) const {
|
||||||
nassertr(index >= 0 && index < AtomicAdjust::get(_num_threads), nullptr);
|
nassertr(index >= 0 && index < get_num_threads(), nullptr);
|
||||||
InternalThread *thread = get_thread_ptr(index);
|
InternalThread *thread = get_thread_ptr(index);
|
||||||
return thread->_thread.lock();
|
return thread->_thread.lock();
|
||||||
}
|
}
|
||||||
@ -144,7 +142,7 @@ get_impl() const {
|
|||||||
*/
|
*/
|
||||||
INLINE PStatClient::Collector *PStatClient::
|
INLINE PStatClient::Collector *PStatClient::
|
||||||
get_collector_ptr(int collector_index) const {
|
get_collector_ptr(int collector_index) const {
|
||||||
CollectorPointer *collectors = (CollectorPointer *)AtomicAdjust::get_ptr(_collectors);
|
CollectorPointer *collectors = _collectors.load(std::memory_order_consume);
|
||||||
return collectors[collector_index];
|
return collectors[collector_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +151,7 @@ get_collector_ptr(int collector_index) const {
|
|||||||
*/
|
*/
|
||||||
INLINE PStatClient::InternalThread *PStatClient::
|
INLINE PStatClient::InternalThread *PStatClient::
|
||||||
get_thread_ptr(int thread_index) const {
|
get_thread_ptr(int thread_index) const {
|
||||||
ThreadPointer *threads = (ThreadPointer *)AtomicAdjust::get_ptr(_threads);
|
ThreadPointer *threads = _threads.load(std::memory_order_consume);
|
||||||
return threads[thread_index];
|
return threads[thread_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,14 +77,6 @@ PStatClient() :
|
|||||||
_lock("PStatClient::_lock"),
|
_lock("PStatClient::_lock"),
|
||||||
_impl(nullptr)
|
_impl(nullptr)
|
||||||
{
|
{
|
||||||
_collectors = nullptr;
|
|
||||||
_collectors_size = 0;
|
|
||||||
_num_collectors = 0;
|
|
||||||
|
|
||||||
_threads = nullptr;
|
|
||||||
_threads_size = 0;
|
|
||||||
_num_threads = 0;
|
|
||||||
|
|
||||||
// We always have a collector at index 0 named "Frame". This tracks the
|
// We always have a collector at index 0 named "Frame". This tracks the
|
||||||
// total frame time and is the root of all other collectors. We have to
|
// total frame time and is the root of all other collectors. We have to
|
||||||
// make this one by hand since it's the root.
|
// make this one by hand since it's the root.
|
||||||
@ -152,7 +144,7 @@ get_max_rate() const {
|
|||||||
*/
|
*/
|
||||||
PStatCollector PStatClient::
|
PStatCollector PStatClient::
|
||||||
get_collector(int index) const {
|
get_collector(int index) const {
|
||||||
nassertr(index >= 0 && index < AtomicAdjust::get(_num_collectors), PStatCollector());
|
nassertr(index >= 0 && index < get_num_collectors(), PStatCollector());
|
||||||
return PStatCollector((PStatClient *)this, index);
|
return PStatCollector((PStatClient *)this, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +153,7 @@ get_collector(int index) const {
|
|||||||
*/
|
*/
|
||||||
string PStatClient::
|
string PStatClient::
|
||||||
get_collector_name(int index) const {
|
get_collector_name(int index) const {
|
||||||
nassertr(index >= 0 && index < AtomicAdjust::get(_num_collectors), string());
|
nassertr(index >= 0 && index < get_num_collectors(), string());
|
||||||
|
|
||||||
return get_collector_ptr(index)->get_name();
|
return get_collector_ptr(index)->get_name();
|
||||||
}
|
}
|
||||||
@ -173,7 +165,7 @@ get_collector_name(int index) const {
|
|||||||
*/
|
*/
|
||||||
string PStatClient::
|
string PStatClient::
|
||||||
get_collector_fullname(int index) const {
|
get_collector_fullname(int index) const {
|
||||||
nassertr(index >= 0 && index < AtomicAdjust::get(_num_collectors), string());
|
nassertr(index >= 0 && index < get_num_collectors(), string());
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(index);
|
Collector *collector = get_collector_ptr(index);
|
||||||
int parent_index = collector->get_parent_index();
|
int parent_index = collector->get_parent_index();
|
||||||
@ -191,7 +183,7 @@ get_collector_fullname(int index) const {
|
|||||||
PStatThread PStatClient::
|
PStatThread PStatClient::
|
||||||
get_thread(int index) const {
|
get_thread(int index) const {
|
||||||
ReMutexHolder holder(_lock);
|
ReMutexHolder holder(_lock);
|
||||||
nassertr(index >= 0 && index < _num_threads, PStatThread());
|
nassertr(index >= 0 && index < get_num_threads(), PStatThread());
|
||||||
return PStatThread((PStatClient *)this, index);
|
return PStatThread((PStatClient *)this, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -462,8 +454,9 @@ client_disconnect() {
|
|||||||
_impl = nullptr;
|
_impl = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadPointer *threads = (ThreadPointer *)_threads;
|
// These can be relaxed loads because we hold the lock.
|
||||||
for (int ti = 0; ti < _num_threads; ++ti) {
|
ThreadPointer *threads = _threads.load(std::memory_order_relaxed);
|
||||||
|
for (int ti = 0; ti < get_num_threads(); ++ti) {
|
||||||
InternalThread *thread = threads[ti];
|
InternalThread *thread = threads[ti];
|
||||||
thread->_frame_number = 0;
|
thread->_frame_number = 0;
|
||||||
thread->_is_active = false;
|
thread->_is_active = false;
|
||||||
@ -471,14 +464,11 @@ client_disconnect() {
|
|||||||
thread->_frame_data.clear();
|
thread->_frame_data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectorPointer *collectors = (CollectorPointer *)_collectors;
|
CollectorPointer *collectors = _collectors.load(std::memory_order_relaxed);
|
||||||
for (int ci = 0; ci < _num_collectors; ++ci) {
|
for (int ci = 0; ci < get_num_collectors(); ++ci) {
|
||||||
Collector *collector = collectors[ci];
|
Collector *collector = collectors[ci];
|
||||||
PerThread::iterator ii;
|
for (PerThreadData &per_thread : collector->_per_thread) {
|
||||||
for (ii = collector->_per_thread.begin();
|
per_thread._nested_count = 0;
|
||||||
ii != collector->_per_thread.end();
|
|
||||||
++ii) {
|
|
||||||
(*ii)._nested_count = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -576,7 +566,8 @@ PStatCollector PStatClient::
|
|||||||
make_collector_with_name(int parent_index, const string &name) {
|
make_collector_with_name(int parent_index, const string &name) {
|
||||||
ReMutexHolder holder(_lock);
|
ReMutexHolder holder(_lock);
|
||||||
|
|
||||||
nassertr(parent_index >= 0 && parent_index < _num_collectors,
|
int num_collectors = get_num_collectors();
|
||||||
|
nassertr(parent_index >= 0 && parent_index < num_collectors,
|
||||||
PStatCollector());
|
PStatCollector());
|
||||||
|
|
||||||
Collector *parent = get_collector_ptr(parent_index);
|
Collector *parent = get_collector_ptr(parent_index);
|
||||||
@ -593,26 +584,25 @@ make_collector_with_name(int parent_index, const string &name) {
|
|||||||
if (ni != parent->_children.end()) {
|
if (ni != parent->_children.end()) {
|
||||||
// We already had a collector by this name; return it.
|
// We already had a collector by this name; return it.
|
||||||
int index = (*ni).second;
|
int index = (*ni).second;
|
||||||
nassertr(index >= 0 && index < _num_collectors, PStatCollector());
|
nassertr(index >= 0 && index < num_collectors, PStatCollector());
|
||||||
return PStatCollector(this, (*ni).second);
|
return PStatCollector(this, (*ni).second);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new collector for this name.
|
// Create a new collector for this name.
|
||||||
int new_index = _num_collectors;
|
parent->_children.insert(ThingsByName::value_type(name, num_collectors));
|
||||||
parent->_children.insert(ThingsByName::value_type(name, new_index));
|
|
||||||
|
|
||||||
Collector *collector = new Collector(parent_index, name);
|
Collector *collector = new Collector(parent_index, name);
|
||||||
// collector->_def = new PStatCollectorDef(new_index, name);
|
// collector->_def = new PStatCollectorDef(num_collectors, name);
|
||||||
// collector->_def->set_parent(*_collectors[parent_index]._def);
|
// collector->_def->set_parent(*_collectors[parent_index]._def);
|
||||||
// initialize_collector_def(this, collector->_def);
|
// initialize_collector_def(this, collector->_def);
|
||||||
|
|
||||||
// We need one PerThreadData for each thread.
|
// We need one PerThreadData for each thread.
|
||||||
while ((int)collector->_per_thread.size() < _num_threads) {
|
while ((int)collector->_per_thread.size() < get_num_threads()) {
|
||||||
collector->_per_thread.push_back(PerThreadData());
|
collector->_per_thread.push_back(PerThreadData());
|
||||||
}
|
}
|
||||||
add_collector(collector);
|
add_collector(collector);
|
||||||
|
|
||||||
return PStatCollector(this, new_index);
|
return PStatCollector(this, num_collectors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -662,8 +652,8 @@ do_make_thread(Thread *thread) {
|
|||||||
vi != indices.end();
|
vi != indices.end();
|
||||||
++vi) {
|
++vi) {
|
||||||
int index = (*vi);
|
int index = (*vi);
|
||||||
nassertr(index >= 0 && index < _num_threads, PStatThread());
|
nassertr(index >= 0 && index < get_num_threads(), PStatThread());
|
||||||
ThreadPointer *threads = (ThreadPointer *)_threads;
|
ThreadPointer *threads = _threads.load(std::memory_order_relaxed);
|
||||||
if (threads[index]->_thread.was_deleted() &&
|
if (threads[index]->_thread.was_deleted() &&
|
||||||
threads[index]->_sync_name == thread->get_sync_name()) {
|
threads[index]->_sync_name == thread->get_sync_name()) {
|
||||||
// Yes, re-use this one.
|
// Yes, re-use this one.
|
||||||
@ -676,7 +666,7 @@ do_make_thread(Thread *thread) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new PStatsThread for this thread pointer.
|
// Create a new PStatsThread for this thread pointer.
|
||||||
int new_index = _num_threads;
|
int new_index = get_num_threads();
|
||||||
thread->set_pstats_index(new_index);
|
thread->set_pstats_index(new_index);
|
||||||
thread->set_pstats_callback(this);
|
thread->set_pstats_callback(this);
|
||||||
|
|
||||||
@ -693,7 +683,7 @@ do_make_thread(Thread *thread) {
|
|||||||
PStatThread PStatClient::
|
PStatThread PStatClient::
|
||||||
make_gpu_thread(const string &name) {
|
make_gpu_thread(const string &name) {
|
||||||
ReMutexHolder holder(_lock);
|
ReMutexHolder holder(_lock);
|
||||||
int new_index = _num_threads;
|
int new_index = get_num_threads();
|
||||||
|
|
||||||
InternalThread *pthread = new InternalThread(name, "GPU");
|
InternalThread *pthread = new InternalThread(name, "GPU");
|
||||||
add_thread(pthread);
|
add_thread(pthread);
|
||||||
@ -710,8 +700,8 @@ make_gpu_thread(const string &name) {
|
|||||||
*/
|
*/
|
||||||
bool PStatClient::
|
bool PStatClient::
|
||||||
is_active(int collector_index, int thread_index) const {
|
is_active(int collector_index, int thread_index) const {
|
||||||
nassertr(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors), false);
|
nassertr(collector_index >= 0 && collector_index < get_num_collectors(), false);
|
||||||
nassertr(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads), false);
|
nassertr(thread_index >= 0 && thread_index < get_num_threads(), false);
|
||||||
|
|
||||||
return (client_is_connected() &&
|
return (client_is_connected() &&
|
||||||
get_collector_ptr(collector_index)->is_active() &&
|
get_collector_ptr(collector_index)->is_active() &&
|
||||||
@ -727,8 +717,8 @@ is_active(int collector_index, int thread_index) const {
|
|||||||
*/
|
*/
|
||||||
bool PStatClient::
|
bool PStatClient::
|
||||||
is_started(int collector_index, int thread_index) const {
|
is_started(int collector_index, int thread_index) const {
|
||||||
nassertr(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors), false);
|
nassertr(collector_index >= 0 && collector_index < get_num_collectors(), false);
|
||||||
nassertr(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads), false);
|
nassertr(thread_index >= 0 && thread_index < get_num_threads(), false);
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
InternalThread *thread = get_thread_ptr(thread_index);
|
InternalThread *thread = get_thread_ptr(thread_index);
|
||||||
@ -758,8 +748,8 @@ start(int collector_index, int thread_index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -789,8 +779,8 @@ start(int collector_index, int thread_index, double as_of) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -820,8 +810,8 @@ stop(int collector_index, int thread_index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -862,8 +852,8 @@ stop(int collector_index, int thread_index, double as_of) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -905,8 +895,8 @@ clear_level(int collector_index, int thread_index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -930,8 +920,8 @@ set_level(int collector_index, int thread_index, double level) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -963,8 +953,8 @@ add_level(int collector_index, int thread_index, double increment) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertv(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors));
|
nassertv(collector_index >= 0 && collector_index < get_num_collectors());
|
||||||
nassertv(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads));
|
nassertv(thread_index >= 0 && thread_index < get_num_threads());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -991,8 +981,8 @@ get_level(int collector_index, int thread_index) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
nassertr(collector_index >= 0 && collector_index < AtomicAdjust::get(_num_collectors), 0.0f);
|
nassertr(collector_index >= 0 && collector_index < get_num_collectors(), 0.0f);
|
||||||
nassertr(thread_index >= 0 && thread_index < AtomicAdjust::get(_num_threads), 0.0f);
|
nassertr(thread_index >= 0 && thread_index < get_num_threads(), 0.0f);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Collector *collector = get_collector_ptr(collector_index);
|
Collector *collector = get_collector_ptr(collector_index);
|
||||||
@ -1050,17 +1040,19 @@ stop_clock_wait() {
|
|||||||
*/
|
*/
|
||||||
void PStatClient::
|
void PStatClient::
|
||||||
add_collector(PStatClient::Collector *collector) {
|
add_collector(PStatClient::Collector *collector) {
|
||||||
if (_num_collectors >= _collectors_size) {
|
int num_collectors = get_num_collectors();
|
||||||
|
if (num_collectors >= _collectors_size) {
|
||||||
// We need to grow the array. We have to be careful here, because there
|
// We need to grow the array. We have to be careful here, because there
|
||||||
// might be clients accessing the array right now who are not protected by
|
// might be clients accessing the array right now who are not protected by
|
||||||
// the lock.
|
// the lock.
|
||||||
int new_collectors_size = (_collectors_size == 0) ? 128 : _collectors_size * 2;
|
size_t new_collectors_size = (_collectors_size == 0) ? 128 : _collectors_size * 2;
|
||||||
|
CollectorPointer *old_collectors = _collectors.load(std::memory_order_relaxed);
|
||||||
CollectorPointer *new_collectors = new CollectorPointer[new_collectors_size];
|
CollectorPointer *new_collectors = new CollectorPointer[new_collectors_size];
|
||||||
if (_collectors != nullptr) {
|
if (old_collectors != nullptr) {
|
||||||
memcpy(new_collectors, _collectors, _num_collectors * sizeof(CollectorPointer));
|
memcpy(new_collectors, old_collectors, num_collectors * sizeof(CollectorPointer));
|
||||||
}
|
}
|
||||||
AtomicAdjust::set_ptr(_collectors, new_collectors);
|
_collectors_size = new_collectors_size;
|
||||||
AtomicAdjust::set(_collectors_size, new_collectors_size);
|
_collectors.store(new_collectors, std::memory_order_release);
|
||||||
|
|
||||||
// Now, we still have the old array, which we allow to leak. We should
|
// Now, we still have the old array, which we allow to leak. We should
|
||||||
// delete it, but there might be a thread out there that's still trying to
|
// delete it, but there might be a thread out there that's still trying to
|
||||||
@ -1068,14 +1060,14 @@ add_collector(PStatClient::Collector *collector) {
|
|||||||
// much, since it's not a big leak. (We will only reallocate the array so
|
// much, since it's not a big leak. (We will only reallocate the array so
|
||||||
// many times in an application, and then no more.)
|
// many times in an application, and then no more.)
|
||||||
|
|
||||||
new_collectors[_num_collectors] = collector;
|
new_collectors[num_collectors] = collector;
|
||||||
AtomicAdjust::inc(_num_collectors);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
CollectorPointer *collectors = (CollectorPointer *)_collectors;
|
|
||||||
collectors[_num_collectors] = collector;
|
|
||||||
AtomicAdjust::inc(_num_collectors);
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
CollectorPointer *collectors = _collectors.load(std::memory_order_relaxed);
|
||||||
|
collectors[num_collectors] = collector;
|
||||||
|
}
|
||||||
|
|
||||||
|
_num_collectors.fetch_add(1, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1084,21 +1076,22 @@ add_collector(PStatClient::Collector *collector) {
|
|||||||
*/
|
*/
|
||||||
void PStatClient::
|
void PStatClient::
|
||||||
add_thread(PStatClient::InternalThread *thread) {
|
add_thread(PStatClient::InternalThread *thread) {
|
||||||
_threads_by_name[thread->_name].push_back(_num_threads);
|
int num_threads = get_num_threads();
|
||||||
_threads_by_sync_name[thread->_sync_name].push_back(_num_threads);
|
_threads_by_name[thread->_name].push_back(num_threads);
|
||||||
|
_threads_by_sync_name[thread->_sync_name].push_back(num_threads);
|
||||||
|
|
||||||
if (_num_threads >= _threads_size) {
|
if (num_threads >= _threads_size) {
|
||||||
// We need to grow the array. We have to be careful here, because there
|
// We need to grow the array. We have to be careful here, because there
|
||||||
// might be clients accessing the array right now who are not protected by
|
// might be clients accessing the array right now who are not protected by
|
||||||
// the lock.
|
// the lock.
|
||||||
int new_threads_size = (_threads_size == 0) ? 128 : _threads_size * 2;
|
size_t new_threads_size = (_threads_size == 0) ? 128 : _threads_size * 2;
|
||||||
|
ThreadPointer *old_threads = _threads.load(std::memory_order_relaxed);
|
||||||
ThreadPointer *new_threads = new ThreadPointer[new_threads_size];
|
ThreadPointer *new_threads = new ThreadPointer[new_threads_size];
|
||||||
if (_threads != nullptr) {
|
if (old_threads != nullptr) {
|
||||||
memcpy(new_threads, _threads, _num_threads * sizeof(ThreadPointer));
|
memcpy(new_threads, old_threads, num_threads * sizeof(ThreadPointer));
|
||||||
}
|
}
|
||||||
// We assume that assignment to a pointer and to an int are each atomic.
|
_threads_size = new_threads_size;
|
||||||
AtomicAdjust::set_ptr(_threads, new_threads);
|
_threads.store(new_threads, std::memory_order_release);
|
||||||
AtomicAdjust::set(_threads_size, new_threads_size);
|
|
||||||
|
|
||||||
// Now, we still have the old array, which we allow to leak. We should
|
// Now, we still have the old array, which we allow to leak. We should
|
||||||
// delete it, but there might be a thread out there that's still trying to
|
// delete it, but there might be a thread out there that's still trying to
|
||||||
@ -1106,22 +1099,23 @@ add_thread(PStatClient::InternalThread *thread) {
|
|||||||
// much, since it's not a big leak. (We will only reallocate the array so
|
// much, since it's not a big leak. (We will only reallocate the array so
|
||||||
// many times in an application, and then no more.)
|
// many times in an application, and then no more.)
|
||||||
|
|
||||||
new_threads[_num_threads] = thread;
|
new_threads[num_threads] = thread;
|
||||||
|
}
|
||||||
} else {
|
else {
|
||||||
ThreadPointer *threads = (ThreadPointer *)_threads;
|
ThreadPointer *threads = _threads.load(std::memory_order_relaxed);
|
||||||
threads[_num_threads] = thread;
|
threads[num_threads] = thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
AtomicAdjust::inc(_num_threads);
|
_num_threads.fetch_add(1, std::memory_order_release);
|
||||||
|
++num_threads;
|
||||||
|
|
||||||
// We need an additional PerThreadData for this thread in all of the
|
// We need an additional PerThreadData for this thread in all of the
|
||||||
// collectors.
|
// collectors.
|
||||||
CollectorPointer *collectors = (CollectorPointer *)_collectors;
|
CollectorPointer *collectors = _collectors.load(std::memory_order_relaxed);
|
||||||
for (int ci = 0; ci < _num_collectors; ++ci) {
|
for (int ci = 0; ci < get_num_collectors(); ++ci) {
|
||||||
Collector *collector = collectors[ci];
|
Collector *collector = collectors[ci];
|
||||||
collector->_per_thread.push_back(PerThreadData());
|
collector->_per_thread.push_back(PerThreadData());
|
||||||
nassertv((int)collector->_per_thread.size() == _num_threads);
|
nassertv((int)collector->_per_thread.size() == num_threads);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "weakPointerTo.h"
|
#include "weakPointerTo.h"
|
||||||
#include "vector_int.h"
|
#include "vector_int.h"
|
||||||
#include "atomicAdjust.h"
|
#include "patomic.h"
|
||||||
#include "numeric_types.h"
|
#include "numeric_types.h"
|
||||||
#include "bitArray.h"
|
#include "bitArray.h"
|
||||||
|
|
||||||
@ -195,9 +195,9 @@ private:
|
|||||||
PerThread _per_thread;
|
PerThread _per_thread;
|
||||||
};
|
};
|
||||||
typedef Collector *CollectorPointer;
|
typedef Collector *CollectorPointer;
|
||||||
AtomicAdjust::Pointer _collectors; // CollectorPointer *_collectors;
|
patomic<CollectorPointer *> _collectors {nullptr};
|
||||||
AtomicAdjust::Integer _collectors_size; // size of the allocated array
|
size_t _collectors_size {0}; // size of the allocated array
|
||||||
AtomicAdjust::Integer _num_collectors; // number of in-use elements within the array
|
patomic<int> _num_collectors {0}; // number of in-use elements within the array
|
||||||
|
|
||||||
// This defines a single thread, i.e. a separate chain of execution,
|
// This defines a single thread, i.e. a separate chain of execution,
|
||||||
// independent of all other threads. Timing and level data are maintained
|
// independent of all other threads. Timing and level data are maintained
|
||||||
@ -224,9 +224,9 @@ private:
|
|||||||
LightMutex _thread_lock;
|
LightMutex _thread_lock;
|
||||||
};
|
};
|
||||||
typedef InternalThread *ThreadPointer;
|
typedef InternalThread *ThreadPointer;
|
||||||
AtomicAdjust::Pointer _threads; // ThreadPointer *_threads;
|
patomic<ThreadPointer *> _threads {nullptr};
|
||||||
AtomicAdjust::Integer _threads_size; // size of the allocated array
|
size_t _threads_size {0}; // size of the allocated array
|
||||||
AtomicAdjust::Integer _num_threads; // number of in-use elements within the array
|
patomic<int> _num_threads {0}; // number of in-use elements within the array
|
||||||
|
|
||||||
mutable PStatClientImpl *_impl;
|
mutable PStatClientImpl *_impl;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user