Patchwork [3/4] trace: Clean up the "try to update atomic until it worked" loops

login
register
mail settings
Submitter Markus Armbruster
Date Jan. 25, 2013, 3:43 p.m.
Message ID <1359128620-14226-4-git-send-email-armbru@redhat.com>
Download mbox | patch
Permalink /patch/215779/
State New
Headers show

Comments

Markus Armbruster - Jan. 25, 2013, 3:43 p.m.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 trace/simple.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
Laszlo Ersek - Jan. 31, 2013, 11:28 a.m.
On 01/25/13 16:43, Markus Armbruster wrote:
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  trace/simple.c | 18 +++++-------------
>  1 file changed, 5 insertions(+), 13 deletions(-)

Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Harsh Prateek Bora - Jan. 31, 2013, noon
On 01/25/2013 09:13 PM, Markus Armbruster wrote:
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   trace/simple.c | 18 +++++-------------
>   1 file changed, 5 insertions(+), 13 deletions(-)

Reviewed-by: Harsh Prateek Bora <harsh@linux.vnet.ibm.com>

>
> diff --git a/trace/simple.c b/trace/simple.c
> index 592ff48..74701e3 100644
> --- a/trace/simple.c
> +++ b/trace/simple.c
> @@ -171,13 +171,10 @@ static gpointer writeout_thread(gpointer opaque)
>               dropped.rec.timestamp_ns = get_clock();
>               dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t),
>               dropped.rec.reserved = 0;
> -            while (1) {
> +            do {
>                   dropped_count = g_atomic_int_get(&dropped_events);
> -                if (g_atomic_int_compare_and_exchange(&dropped_events,
> -                                                      dropped_count, 0)) {
> -                    break;
> -                }
> -            }
> +            } while (!g_atomic_int_compare_and_exchange(&dropped_events,
> +                                                        dropped_count, 0));
>               dropped.rec.arguments[0] = dropped_count;
>               unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
>           }
> @@ -213,7 +210,7 @@ int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasi
>       uint32_t rec_len = sizeof(TraceRecord) + datasize;
>       uint64_t timestamp_ns = get_clock();
>
> -    while (1) {
> +    do {
>           old_idx = g_atomic_int_get(&trace_idx);
>           smp_rmb();
>           new_idx = old_idx + rec_len;
> @@ -223,12 +220,7 @@ int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasi
>               g_atomic_int_inc(&dropped_events);
>               return -ENOSPC;
>           }
> -
> -        if (g_atomic_int_compare_and_exchange(&trace_idx,
> -                                              old_idx, new_idx)) {
> -            break;
> -        }
> -    }
> +    } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
>
>       idx = old_idx % TRACE_BUF_LEN;
>

Patch

diff --git a/trace/simple.c b/trace/simple.c
index 592ff48..74701e3 100644
--- a/trace/simple.c
+++ b/trace/simple.c
@@ -171,13 +171,10 @@  static gpointer writeout_thread(gpointer opaque)
             dropped.rec.timestamp_ns = get_clock();
             dropped.rec.length = sizeof(TraceRecord) + sizeof(uint64_t),
             dropped.rec.reserved = 0;
-            while (1) {
+            do {
                 dropped_count = g_atomic_int_get(&dropped_events);
-                if (g_atomic_int_compare_and_exchange(&dropped_events,
-                                                      dropped_count, 0)) {
-                    break;
-                }
-            }
+            } while (!g_atomic_int_compare_and_exchange(&dropped_events,
+                                                        dropped_count, 0));
             dropped.rec.arguments[0] = dropped_count;
             unused = fwrite(&dropped.rec, dropped.rec.length, 1, trace_fp);
         }
@@ -213,7 +210,7 @@  int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasi
     uint32_t rec_len = sizeof(TraceRecord) + datasize;
     uint64_t timestamp_ns = get_clock();
 
-    while (1) {
+    do {
         old_idx = g_atomic_int_get(&trace_idx);
         smp_rmb();
         new_idx = old_idx + rec_len;
@@ -223,12 +220,7 @@  int trace_record_start(TraceBufferRecord *rec, TraceEventID event, size_t datasi
             g_atomic_int_inc(&dropped_events);
             return -ENOSPC;
         }
-
-        if (g_atomic_int_compare_and_exchange(&trace_idx,
-                                              old_idx, new_idx)) {
-            break;
-        }
-    }
+    } while (!g_atomic_int_compare_and_exchange(&trace_idx, old_idx, new_idx));
 
     idx = old_idx % TRACE_BUF_LEN;