Fix tests: finer-grained use of CPU_SET, CPU_ZERO and cpu_set_t
[urcu.git] / tests / test_perthreadlock.c
index 6c7114ca059096f98340ce7f815a7b7a8fa40bca..2a8b0c3718c6b4aa964d83c244e1a7284715e77b 100644 (file)
 #include <unistd.h>
 #include <stdio.h>
 #include <assert.h>
-#include <sched.h>
 #include <errno.h>
 
 #include <urcu/arch.h>
+#include <urcu/tls-compat.h>
+#include "cpuset.h"
 
 #ifdef __linux__
 #include <syscall.h>
@@ -89,9 +90,9 @@ static unsigned long rduration;
 /* write-side C.S. duration, in loops */
 static unsigned long wduration;
 
-static inline void loop_sleep(unsigned long l)
+static inline void loop_sleep(unsigned long loops)
 {
-       while(l-- != 0)
+       while (loops-- != 0)
                caa_cpu_relax();
 }
 
@@ -109,17 +110,12 @@ static int use_affinity = 0;
 
 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-#ifndef HAVE_CPU_SET_T
-typedef unsigned long cpu_set_t;
-# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
-# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
-#endif
-
 static void set_affinity(void)
 {
+#if HAVE_SCHED_SETAFFINITY
        cpu_set_t mask;
-       int cpu;
-       int ret;
+       int cpu, ret;
+#endif /* HAVE_SCHED_SETAFFINITY */
 
        if (!use_affinity)
                return;
@@ -159,8 +155,8 @@ static int test_duration_read(void)
        return !test_stop;
 }
 
-static unsigned long long __thread nr_writes;
-static unsigned long long __thread nr_reads;
+static DEFINE_URCU_TLS(unsigned long long, nr_writes);
+static DEFINE_URCU_TLS(unsigned long long, nr_reads);
 
 static
 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
@@ -198,7 +194,8 @@ void *thr_reader(void *data)
        unsigned long tidx = (unsigned long)data;
 
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
-                       "reader", pthread_self(), (unsigned long)gettid());
+                       "reader", (unsigned long) pthread_self(),
+                       (unsigned long) gettid());
 
        set_affinity();
 
@@ -209,17 +206,18 @@ void *thr_reader(void *data)
        for (;;) {
                pthread_mutex_lock(&per_thread_lock[tidx].lock);
                assert(test_array.a == 8);
-               if (unlikely(rduration))
+               if (caa_unlikely(rduration))
                        loop_sleep(rduration);
                pthread_mutex_unlock(&per_thread_lock[tidx].lock);
-               nr_reads++;
-               if (unlikely(!test_duration_read()))
+               URCU_TLS(nr_reads)++;
+               if (caa_unlikely(!test_duration_read()))
                        break;
        }
 
-       tot_nr_reads[tidx] = nr_reads;
+       tot_nr_reads[tidx] = URCU_TLS(nr_reads);
        printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
-                       "reader", pthread_self(), (unsigned long)gettid());
+                       "reader", (unsigned long) pthread_self(),
+                       (unsigned long) gettid());
        return ((void*)1);
 
 }
@@ -230,7 +228,8 @@ void *thr_writer(void *data)
        long tidx;
 
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
-                       "writer", pthread_self(), (unsigned long)gettid());
+                       "writer", (unsigned long) pthread_self(),
+                       (unsigned long) gettid());
 
        set_affinity();
 
@@ -245,21 +244,22 @@ void *thr_writer(void *data)
                }
                test_array.a = 0;
                test_array.a = 8;
-               if (unlikely(wduration))
+               if (caa_unlikely(wduration))
                        loop_sleep(wduration);
                for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
                        pthread_mutex_unlock(&per_thread_lock[tidx].lock);
                }
-               nr_writes++;
-               if (unlikely(!test_duration_write()))
+               URCU_TLS(nr_writes)++;
+               if (caa_unlikely(!test_duration_write()))
                        break;
-               if (unlikely(wdelay))
+               if (caa_unlikely(wdelay))
                        loop_sleep(wdelay);
        }
 
        printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
-                       "writer", pthread_self(), (unsigned long)gettid());
-       tot_nr_writes[wtidx] = nr_writes;
+                       "writer", (unsigned long) pthread_self(),
+                       (unsigned long) gettid());
+       tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
        return ((void*)2);
 }
 
@@ -282,7 +282,6 @@ int main(int argc, char **argv)
        int err;
        pthread_t *tid_reader, *tid_writer;
        void *tret;
-       unsigned long long *count_reader, *count_writer;
        unsigned long long tot_reads = 0, tot_writes = 0;
        int i, a;
 
@@ -364,15 +363,18 @@ int main(int argc, char **argv)
        printf_verbose("Writer delay : %lu loops.\n", wdelay);
        printf_verbose("Reader duration : %lu loops.\n", rduration);
        printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
-                       "main", pthread_self(), (unsigned long)gettid());
+                       "main", (unsigned long) pthread_self(), (unsigned long) gettid());
 
        tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
        tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
-       count_reader = malloc(sizeof(*count_reader) * nr_readers);
-       count_writer = malloc(sizeof(*count_writer) * nr_writers);
        tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
        tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
        per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers);
+       for (i = 0; i < nr_readers; i++) {
+               err = pthread_mutex_init(&per_thread_lock[i].lock, NULL);
+               if (err != 0)
+                       exit(1);
+       }
 
        next_aff = 0;
 
@@ -421,8 +423,6 @@ int main(int argc, char **argv)
 
        free(tid_reader);
        free(tid_writer);
-       free(count_reader);
-       free(count_writer);
        free(tot_nr_reads);
        free(tot_nr_writes);
        free(per_thread_lock);
This page took 0.026192 seconds and 4 git commands to generate.