diff --git a/panda/src/putil/uniqueIdAllocator.cxx b/panda/src/putil/uniqueIdAllocator.cxx index b1b49cf53a..942a17459f 100644 --- a/panda/src/putil/uniqueIdAllocator.cxx +++ b/panda/src/putil/uniqueIdAllocator.cxx @@ -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("<= _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) diff --git a/panda/src/putil/uniqueIdAllocator.h b/panda/src/putil/uniqueIdAllocator.h index 2f3e3b9e8f..66b1a1d876 100644 --- a/panda/src/putil/uniqueIdAllocator.h +++ b/panda/src/putil/uniqueIdAllocator.h @@ -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 //]