[sysprof: 17/63] libsysprof-capture: Use intrinsic atomics rather than g_atomic_*()



commit 951b46fddf30ded3d396da6a3d89905adc24173f
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jul 1 17:00:36 2020 +0100

    libsysprof-capture: Use intrinsic atomics rather than g_atomic_*()
    
    Use the intrinsic atomics provided by the compiler, instead of GLib’s
    wrapper around them. This should work for all modern compilers.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Helps: #40

 src/libsysprof-capture/mapped-ring-buffer.c        | 25 ++++++++++++----------
 src/libsysprof-capture/sysprof-capture-condition.c |  4 ++--
 src/libsysprof-capture/sysprof-capture-cursor.c    |  4 ++--
 src/libsysprof-capture/sysprof-capture-reader.c    |  4 ++--
 src/libsysprof-capture/sysprof-capture-writer.c    |  4 ++--
 5 files changed, 22 insertions(+), 19 deletions(-)
---
diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c
index 1b0fff4..1c170c6 100644
--- a/src/libsysprof-capture/mapped-ring-buffer.c
+++ b/src/libsysprof-capture/mapped-ring-buffer.c
@@ -334,7 +334,7 @@ mapped_ring_buffer_unref (MappedRingBuffer *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test (&self->ref_count))
+  if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
     mapped_ring_buffer_finalize (self);
 }
 
@@ -344,7 +344,7 @@ mapped_ring_buffer_ref (MappedRingBuffer *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  g_atomic_int_inc (&self->ref_count);
+  __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
 
   return self;
 }
@@ -395,8 +395,8 @@ mapped_ring_buffer_allocate (MappedRingBuffer *self,
   assert ((length & 0x7) == 0);
 
   header = get_header (self);
-  headpos = g_atomic_int_get (&header->head);
-  tailpos = g_atomic_int_get (&header->tail);
+  __atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST);
+  __atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST);
 
   /* We need to check that there is enough space for @length at the
    * current position in the write buffer. We cannot fully catch up
@@ -463,7 +463,7 @@ mapped_ring_buffer_advance (MappedRingBuffer *self,
    * we just update the position as the only way the head could have
    * moved is forward.
    */
-  g_atomic_int_set (&header->tail, tail);
+  __atomic_store (&header->tail, &tail, __ATOMIC_SEQ_CST);
 }
 
 /**
@@ -494,8 +494,8 @@ mapped_ring_buffer_drain (MappedRingBuffer         *self,
   assert (callback != NULL);
 
   header = get_header (self);
-  headpos = g_atomic_int_get (&header->head);
-  tailpos = g_atomic_int_get (&header->tail);
+  __atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST);
+  __atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST);
 
   assert (headpos < self->body_size);
   assert (tailpos < self->body_size);
@@ -515,6 +515,7 @@ mapped_ring_buffer_drain (MappedRingBuffer         *self,
     {
       const void *data = get_body_at_pos (self, headpos);
       size_t len = tailpos - headpos;
+      uint32_t new_headpos;
 
       if (!callback (data, &len, user_data))
         return false;
@@ -525,9 +526,11 @@ mapped_ring_buffer_drain (MappedRingBuffer         *self,
       headpos += len;
 
       if (headpos >= self->body_size)
-        g_atomic_int_set (&header->head, headpos - self->body_size);
+        new_headpos = headpos - self->body_size;
       else
-        g_atomic_int_set (&header->head, headpos);
+        new_headpos = headpos;
+
+      __atomic_store (&header->head, &new_headpos, __ATOMIC_SEQ_CST);
     }
 
   return true;
@@ -552,8 +555,8 @@ mapped_ring_buffer_is_empty (MappedRingBuffer *self)
 
   header = get_header (self);
 
-  headpos = g_atomic_int_get (&header->head);
-  tailpos = g_atomic_int_get (&header->tail);
+  __atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST);
+  __atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST);
 
   return headpos == tailpos;
 }
diff --git a/src/libsysprof-capture/sysprof-capture-condition.c 
b/src/libsysprof-capture/sysprof-capture-condition.c
index febeeba..9921906 100644
--- a/src/libsysprof-capture/sysprof-capture-condition.c
+++ b/src/libsysprof-capture/sysprof-capture-condition.c
@@ -303,7 +303,7 @@ sysprof_capture_condition_ref (SysprofCaptureCondition *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  g_atomic_int_inc (&self->ref_count);
+  __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
   return self;
 }
 
@@ -313,7 +313,7 @@ sysprof_capture_condition_unref (SysprofCaptureCondition *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test (&self->ref_count))
+  if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
     sysprof_capture_condition_finalize (self);
 }
 
diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c
index 62b2072..a6af384 100644
--- a/src/libsysprof-capture/sysprof-capture-cursor.c
+++ b/src/libsysprof-capture/sysprof-capture-cursor.c
@@ -110,7 +110,7 @@ sysprof_capture_cursor_ref (SysprofCaptureCursor *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  g_atomic_int_inc (&self->ref_count);
+  __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
   return self;
 }
 
@@ -126,7 +126,7 @@ sysprof_capture_cursor_unref (SysprofCaptureCursor *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test (&self->ref_count))
+  if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
     sysprof_capture_cursor_finalize (self);
 }
 
diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c
index 5222547..6c4862e 100644
--- a/src/libsysprof-capture/sysprof-capture-reader.c
+++ b/src/libsysprof-capture/sysprof-capture-reader.c
@@ -973,7 +973,7 @@ sysprof_capture_reader_ref (SysprofCaptureReader *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  g_atomic_int_inc (&self->ref_count);
+  __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
 
   return self;
 }
@@ -984,7 +984,7 @@ sysprof_capture_reader_unref (SysprofCaptureReader *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test (&self->ref_count))
+  if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
     sysprof_capture_reader_finalize (self);
 }
 
diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c
index d4aa2fc..5036eda 100644
--- a/src/libsysprof-capture/sysprof-capture-writer.c
+++ b/src/libsysprof-capture/sysprof-capture-writer.c
@@ -192,7 +192,7 @@ sysprof_capture_writer_ref (SysprofCaptureWriter *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  g_atomic_int_inc (&self->ref_count);
+  __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
 
   return self;
 }
@@ -203,7 +203,7 @@ sysprof_capture_writer_unref (SysprofCaptureWriter *self)
   assert (self != NULL);
   assert (self->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test (&self->ref_count))
+  if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
     sysprof_capture_writer_finalize (self);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]