optimized refcounting for MIPS (use asm optimized atomic inc/dec)
[enigma2.git] / lib / base / object.h
index 1717845..f93f425 100644 (file)
@@ -39,16 +39,97 @@ public:
 };
 
 #ifndef SWIG
-#define DECLARE_REF(x) private: eSingleLock ref_lock; oRefCount ref; public: void AddRef(); void Release();
-#ifdef OBJECT_DEBUG
-extern int object_total_remaining;
-#define DEFINE_REF(c) void c::AddRef() { eSingleLocker l(ref_lock); ++object_total_remaining; ++ref; eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); } void c::Release() { { eSingleLocker l(ref_lock); --object_total_remaining; --ref; eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); } if (!ref) delete this; }
-#error fix locking for debug
-#else
-#define DEFINE_REF(c) void c::AddRef() { eSingleLocker l(ref_lock); ++ref; } void c::Release() { { eSingleLocker l(ref_lock); --ref; } if (!ref) delete this; }
-#endif
+       #if defined(__mips__)
+               #define DECLARE_REF(x)                  \
+                       private: oRefCount ref;         \
+                       public: void AddRef();          \
+                                       void Release();
+               #define DEFINE_REF(c) \
+                       void c::AddRef() \
+                       { \
+                               unsigned long temp; \
+                               __asm__ __volatile__( \
+                               "               .set    mips3           \n" \
+                               "1:             ll              %0, %1          \n" \
+                               "               .set    mips0           \n" \
+                               "               addu    %0, 1           \n" \
+                               "               .set    mips3           \n" \
+                               "               sc              %0, %1          \n" \
+                               "               .set    mips0           \n" \
+                               "               beqz    %0, 1b          \n" \
+                               : "=&r" (temp), "=m" ((int)ref) \
+                               : "m" ((int)ref) \
+                               : "memory"); \
+                       } \
+                       void c::Release() \
+                       { \
+                               unsigned long temp; \
+                               __asm__ __volatile__( \
+                               "               .set    mips3           \n" \
+                               "1:             ll              %0, %1          \n" \
+                               "               .set    mips0           \n" \
+                               "               subu    %0, 1           \n" \
+                               "               .set    mips3           \n" \
+                               "               sc              %0, %1          \n" \
+                               "               .set    mips0           \n" \
+                               "               beqz    %0, 1b          \n" \
+                               : "=&r" (temp), "=m" ((int)ref) \
+                               : "m" ((int)ref) \
+                               : "memory"); \
+                               if (!ref) \
+                                       delete this; \
+                       }
+       #else
+               #define DECLARE_REF(x)                  \
+                       private:oRefCount ref;  \
+                                       eSingleLock ref_lock; \
+                       public: void AddRef();          \
+                                       void Release();
+               #ifdef OBJECT_DEBUG
+                       extern int object_total_remaining;
+                       #define DEFINE_REF(c) \
+                               void c::AddRef() \
+                               { \
+                                       eSingleLocker l(ref_lock); \
+                                       ++object_total_remaining; \
+                                       ++ref; \
+                                       eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); \
+                               } \
+                               void c::Release() \
+                               { \
+                                       { \
+                                               eSingleLocker l(ref_lock); \
+                                               --object_total_remaining; \
+                                               --ref; \
+                                               eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); \
+                                       } \
+                                       if (!ref) \
+                                               delete this; \
+                               }
+                               #error fix locking for debug
+               #else
+                       #define DEFINE_REF(c) \
+                               void c::AddRef() \
+                               { \
+                                       eSingleLocker l(ref_lock); \
+                                       ++ref; \
+                               } \
+                               void c::Release() \
+                               { \
+                                       { \
+                                               eSingleLocker l(ref_lock); \
+                                               --ref; \
+                                       } \
+                                       if (!ref) \
+                                               delete this; \
+                               }
+               #endif
+       #endif
 #else
-#define DECLARE_REF(x) private: void AddRef(); void Release();
+       #define DECLARE_REF(x) \
+               private: \
+                       void AddRef(); \
+                       void Release();
 #endif
 
 #ifdef SWIG