[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / integration-api / lockless-buffer.cpp
index a1a7541..dd2217d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -27,13 +27,13 @@ namespace Dali
 namespace Integration
 {
 
-LocklessBuffer::LocklessBuffer( size_t size )
+LocklessBuffer::LocklessBuffer( uint32_t size )
 : mState( R0W1 ),
   mSize( size )
 {
   // allocate memory to speed up operation
-  mBuffer[0] = new unsigned char[size];
-  mBuffer[1] = new unsigned char[size];
+  mBuffer[0] = new uint8_t[size];
+  mBuffer[1] = new uint8_t[size];
 
   memset (mBuffer[0], 0, size );
   memset (mBuffer[1], 0, size );
@@ -45,13 +45,12 @@ LocklessBuffer::~LocklessBuffer()
   delete[] mBuffer[1];
 }
 
-void LocklessBuffer::Write( const unsigned char *src, size_t size )
+void LocklessBuffer::Write( const uint8_t *src, uint32_t size )
 {
   DALI_ASSERT_ALWAYS( size <= mSize );
 
   // set WRITING bit
-  BufferState currentState( std::atomic_fetch_or( &mState, WRITING ) );
-
+  BufferState currentState( __sync_fetch_and_or( &mState, WRITING ) );
   DALI_ASSERT_DEBUG( !(currentState & WRITING_MASK) ); // WRITING bit should never be set when we get here
 
   // copy data to current write buffer, negate state to get actual index (recap: R0W1 = 0, R1W0 = 1)
@@ -59,15 +58,15 @@ void LocklessBuffer::Write( const unsigned char *src, size_t size )
   memcpy( mBuffer[index], src, size );
 
   // unset WRITING bit, set UPDATED bit
-  BufferState writingState = static_cast<BufferState>(currentState | WRITING);
-  BufferState checkState = mState;
-  mState.compare_exchange_strong( writingState, static_cast<BufferState>(index | UPDATED) );
+  BufferState checkState = __sync_val_compare_and_swap( &mState,
+                                                        static_cast<BufferState>(currentState | WRITING),
+                                                        static_cast<BufferState>(index | UPDATED) );
 
   DALI_ASSERT_DEBUG( checkState & WRITING );
   (void)checkState; // Avoid unused variable warning
 }
 
-const unsigned char* LocklessBuffer::Read()
+const uint8_t* LocklessBuffer::Read()
 {
   // current state (only to avoid multiple memory reads with volatile variable)
   BufferState currentState( mState );
@@ -77,8 +76,9 @@ const unsigned char* LocklessBuffer::Read()
   {
     // Try to swap buffers.
     // This will set mState to 1 if readbuffer 0 was updated, 0 if readbuffer 1 was updated and fail if WRITING is set
-    BufferState writingState = static_cast<BufferState>(currentWriteBuf | UPDATED);
-    if( mState.compare_exchange_strong( writingState, static_cast<BufferState>(!currentWriteBuf) ) )
+    if( __sync_bool_compare_and_swap( &mState,
+                                      static_cast<BufferState>(currentWriteBuf | UPDATED),
+                                      static_cast<BufferState>(!currentWriteBuf) )  )
     {
       // swap successful
       return mBuffer[currentWriteBuf];
@@ -90,7 +90,7 @@ const unsigned char* LocklessBuffer::Read()
   return mBuffer[!currentWriteBuf];
 }
 
-unsigned int LocklessBuffer::GetSize() const
+uint32_t LocklessBuffer::GetSize() const
 {
   return static_cast<unsigned int>(mSize);
 }