diff options
author | Chris Robinson <[email protected]> | 2018-11-17 05:31:29 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-17 05:31:29 -0800 |
commit | 1fae8c16a8c0634ffa44b4a2e25f3be4899ea7e2 (patch) | |
tree | 622a682c96ec30cb08a11e6b4e6969daaaeeacf1 /common/uintmap.h | |
parent | ccdaca80c910047e16f710d44f640a6d6f86a195 (diff) |
Convert threads.c to C++
Also vastly simplify and remove related code.
Diffstat (limited to 'common/uintmap.h')
-rw-r--r-- | common/uintmap.h | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/common/uintmap.h b/common/uintmap.h index 32868653..0646d2b5 100644 --- a/common/uintmap.h +++ b/common/uintmap.h @@ -1,41 +1,35 @@ #ifndef AL_UINTMAP_H #define AL_UINTMAP_H -#include <limits.h> +#include <unordered_map> +#include <mutex> #include "AL/al.h" -#include "rwlock.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct UIntMap { - ALuint *keys; - /* Shares memory with keys. */ - ALvoid **values; - - ALsizei size; - ALsizei capacity; - ALsizei limit; - RWLock lock; -} UIntMap; -#define UINTMAP_STATIC_INITIALIZE_N(_n) { NULL, NULL, 0, 0, (_n), RWLOCK_STATIC_INITIALIZE } -#define UINTMAP_STATIC_INITIALIZE UINTMAP_STATIC_INITIALIZE_N(INT_MAX) - -void InitUIntMap(UIntMap *map, ALsizei limit); -void ResetUIntMap(UIntMap *map); -ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value); -ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key); -ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key); - -inline void LockUIntMapRead(UIntMap *map) { ReadLock(&map->lock); } -inline void UnlockUIntMapRead(UIntMap *map) { ReadUnlock(&map->lock); } -inline void LockUIntMapWrite(UIntMap *map) { WriteLock(&map->lock); } -inline void UnlockUIntMapWrite(UIntMap *map) { WriteUnlock(&map->lock); } - -#ifdef __cplusplus -} -#endif + +template<typename T0, typename T1> +class ThrSafeMap { + std::unordered_map<T0, T1> mValues; + std::mutex mLock; + +public: + void InsertEntry(T0 key, T1 value) noexcept + { + std::lock_guard<std::mutex> _{mLock}; + mValues[key] = value; + } + + T1 RemoveKey(T0 key) noexcept + { + T1 retval{}; + + std::lock_guard<std::mutex> _{mLock}; + auto iter = mValues.find(key); + if(iter != mValues.end()) + retval = iter->second; + mValues.erase(iter); + + return retval; + } +}; #endif /* AL_UINTMAP_H */ |