putil: Make UniqueIdAllocator safe for multithreaded usage

Closes #995
This commit is contained in:
Daniel 2020-08-27 14:35:47 +02:00 committed by rdb
parent 95d1ac2f8b
commit 085795db83
2 changed files with 16 additions and 0 deletions

View File

@ -16,6 +16,7 @@
#include "notifyCategoryProxy.h"
#include "uniqueIdAllocator.h"
#include "lightMutexHolder.h"
using std::endl;
@ -84,6 +85,8 @@ UniqueIdAllocator::
*/
uint32_t UniqueIdAllocator::
allocate() {
LightMutexHolder holder(_lock);
if (_next_free == IndexEnd) {
// ...all ids allocated.
uniqueIdAllocator_warning("allocate Error: no more free ids.");
@ -115,6 +118,8 @@ allocate() {
*/
void UniqueIdAllocator::
initial_reserve_id(uint32_t id) {
LightMutexHolder holder(_lock);
nassertv(id >= _min && id <= _max); // Attempt to reserve out-of-range id.
uint32_t index = id - _min; // Convert to _table index.
@ -175,6 +180,8 @@ initial_reserve_id(uint32_t id) {
*/
bool UniqueIdAllocator::
is_allocated(uint32_t id) {
LightMutexHolder holder(_lock);
if (id < _min || id > _max) {
// This id is out of range, not allocated.
return false;
@ -190,6 +197,8 @@ is_allocated(uint32_t id) {
*/
void UniqueIdAllocator::
free(uint32_t id) {
LightMutexHolder holder(_lock);
uniqueIdAllocator_debug("free("<<id<<")");
nassertv(id >= _min && id <= _max); // Attempt to free out-of-range id.
@ -234,6 +243,8 @@ output(std::ostream &out) const {
*/
void UniqueIdAllocator::
write(std::ostream &out) const {
LightMutexHolder holder(_lock);
out << "_min: " << _min << "; _max: " << _max
<< ";\n_next_free: " << int32_t(_next_free)
<< "; _last_free: " << int32_t(_last_free)

View File

@ -16,6 +16,7 @@
#include "pandabase.h"
#include "numeric_types.h"
#include "lightMutex.h"
/**
* Manage a set of ID values from min to max inclusive. The ID numbers that
@ -86,6 +87,10 @@ protected:
// The number of free elements in _table.
uint32_t _free;
private:
// A light mutex lock to make usage safe in multi-threaded scenarios.
LightMutex _lock;
};
#endif //]