Update for thread-safety of object manager
authorJaesung Ku <jaesung.ku@samsung.com>
Mon, 27 May 2013 12:19:25 +0000 (21:19 +0900)
committerJaesung Ku <jaesung.ku@samsung.com>
Mon, 27 May 2013 12:21:40 +0000 (21:21 +0900)
Change-Id: I3ed9b38f23c3b999c26667cea99d35c746b3fc2b
Signed-off-by: Jaesung Ku <jaesung.ku@samsung.com>
src/base/inc/FBase_ObjectManagerImpl.h [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 444dcb9..5da1ad5
@@ -27,6 +27,8 @@
 
 #include <FBaseObject.h>
 #include <FBaseColLinkedListT.h>
+#include <FBaseRtMutex.h>
+#include <FBaseSysLog.h>
 
 
 namespace Tizen { namespace Base
@@ -48,6 +50,12 @@ public:
        {
                __slotCapacity = DEFAULT_CAPACITY;
                __pSlots = new _HandleElement[__slotCapacity];
+
+               __pLock = new (std::nothrow) Tizen::Base::Runtime::Mutex();
+               SysTryLog(NID_BASE, __pLock != null, "Failed to allocate the mutex instance");
+
+               result r = __pLock->Create();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to create the mutex instance");
        }
 
        /**
@@ -58,6 +66,7 @@ public:
        virtual ~_ObjectManagerImpl(void)
        {
                delete[] __pSlots;
+               delete __pLock;
        }
 
        /**
@@ -75,6 +84,12 @@ public:
                {
                        __pSlots[i] = pObjectManagerImpl.__pSlots[i];
                }
+
+               __pLock = new (std::nothrow) Tizen::Base::Runtime::Mutex();
+               SysTryLog(NID_BASE, __pLock != null, "Failed to allocate the mutex instance");
+
+               result r = __pLock->Create();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to create the mutex instance");
        }
 
        /**
@@ -116,6 +131,9 @@ public:
         */
        int Register(void* pObject)
        {
+               result r = __pLock->Acquire();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
+
                _Handle handle;
 
                if (__freeSlots.GetCount() == 0)
@@ -146,6 +164,9 @@ public:
                        __pSlots[index].__pObject = pObject;
                }
 
+               r = __pLock->Release();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
+
                return handle.__handle;
        }
 
@@ -161,6 +182,9 @@ public:
         */
        void* Unregister(unsigned int handle)
        {
+               result r = __pLock->Acquire();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
+
                void* pObject = null;
                const unsigned int index = ((_Handle*)&handle)->__index;
 
@@ -184,6 +208,9 @@ public:
 
                __freeSlots.Add(index);
 
+               r = __pLock->Release();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
+
                return pObject;
        }
 
@@ -199,6 +226,9 @@ public:
         */
        const void* GetObject(unsigned int handle) const
        {
+               result r = __pLock->Acquire();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
+
                const unsigned int index = ((_Handle*)&handle)->__index;
 
                if (index >= __slotCount)
@@ -214,6 +244,9 @@ public:
                        return null;
                }
 
+               r = __pLock->Release();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
+
                return element.__pObject;
        }
 
@@ -241,7 +274,15 @@ public:
         */
        int GetObjectCount(void) const
        {
-               return static_cast <int>(__slotCount - __freeSlots.GetCount());
+               result r = __pLock->Acquire();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
+
+               int cnt = static_cast <int>(__slotCount - __freeSlots.GetCount());
+
+               r = __pLock->Release();
+               SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
+
+               return cnt;
        }
 
        bool IsValidHandle(unsigned int handle) const
@@ -356,6 +397,7 @@ private:
        unsigned int __slotCount;
        unsigned int __slotCapacity;
        Tizen::Base::Collection::LinkedListT<unsigned int> __freeSlots;
+       Tizen::Base::Runtime::Mutex* __pLock;
 
 };