Cleanup: doc/examples makefile
[urcu.git] / tests / test_perthreadlock_timing.c
index 9eb1c1357b65c84fe833a16a1a1c991889d3ebc6..97bdd044eae4d231621456efbd4b010b3d72c1aa 100644 (file)
 #include <unistd.h>
 #include <stdio.h>
 #include <assert.h>
-#include <sys/syscall.h>
 #include <pthread.h>
 #include <errno.h>
 
 #include <urcu/arch.h>
 
-#if defined(_syscall0)
-_syscall0(pid_t, gettid)
-#elif defined(__NR_gettid)
-static inline pid_t gettid(void)
-{
-       return syscall(__NR_gettid);
-}
-#else
-#warning "use pid as tid"
-static inline pid_t gettid(void)
-{
-       return getpid();
-}
-#endif
+#include "thread-id.h"
 
 #include <urcu.h>
 
@@ -60,7 +46,7 @@ static struct test_array test_array = { 8 };
 
 struct per_thread_lock {
        pthread_mutex_t lock;
-} __attribute__((aligned(CACHE_LINE_SIZE)));   /* cache-line aligned */
+} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));       /* cache-line aligned */
 
 static struct per_thread_lock *per_thread_lock;
 
@@ -78,8 +64,8 @@ static int num_write;
 #define NR_READ num_read
 #define NR_WRITE num_write
 
-static cycles_t __attribute__((aligned(CACHE_LINE_SIZE))) *reader_time;
-static cycles_t __attribute__((aligned(CACHE_LINE_SIZE))) *writer_time;
+static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time;
+static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time;
 
 void *thr_reader(void *arg)
 {
@@ -87,11 +73,11 @@ void *thr_reader(void *arg)
        cycles_t time1, time2;
        long tidx = (long)arg;
 
-       printf("thread_begin %s, thread id : %lx, tid %lu\n",
-                       "reader", pthread_self(), (unsigned long)gettid());
+       printf("thread_begin %s, tid %lu\n",
+               "reader", urcu_get_thread_id());
        sleep(2);
 
-       time1 = get_cycles();
+       time1 = caa_get_cycles();
        for (i = 0; i < OUTER_READ_LOOP; i++) {
                for (j = 0; j < INNER_READ_LOOP; j++) {
                        pthread_mutex_lock(&per_thread_lock[tidx].lock);
@@ -99,13 +85,13 @@ void *thr_reader(void *arg)
                        pthread_mutex_unlock(&per_thread_lock[tidx].lock);
                }
        }
-       time2 = get_cycles();
+       time2 = caa_get_cycles();
 
        reader_time[tidx] = time2 - time1;
 
        sleep(2);
-       printf("thread_end %s, thread id : %lx, tid %lu\n",
-                       "reader", pthread_self(), (unsigned long)gettid());
+       printf("thread_end %s, tid %lu\n",
+               "reader", urcu_get_thread_id());
        return ((void*)1);
 
 }
@@ -116,13 +102,13 @@ void *thr_writer(void *arg)
        long tidx;
        cycles_t time1, time2;
 
-       printf("thread_begin %s, thread id : %lx, tid %lu\n",
-                       "writer", pthread_self(), (unsigned long)gettid());
+       printf("thread_begin %s, tid %lu\n",
+               "writer", urcu_get_thread_id());
        sleep(2);
 
        for (i = 0; i < OUTER_WRITE_LOOP; i++) {
                for (j = 0; j < INNER_WRITE_LOOP; j++) {
-                       time1 = get_cycles();
+                       time1 = caa_get_cycles();
                        for (tidx = 0; tidx < NR_READ; tidx++) {
                                pthread_mutex_lock(&per_thread_lock[tidx].lock);
                        }
@@ -130,14 +116,14 @@ void *thr_writer(void *arg)
                        for (tidx = NR_READ - 1; tidx >= 0; tidx--) {
                                pthread_mutex_unlock(&per_thread_lock[tidx].lock);
                        }
-                       time2 = get_cycles();
+                       time2 = caa_get_cycles();
                        writer_time[(unsigned long)arg] += time2 - time1;
                        usleep(1);
                }
        }
 
-       printf("thread_end %s, thread id : %lx, tid %lu\n",
-                       "writer", pthread_self(), (unsigned long)gettid());
+       printf("thread_end %s, tid %lu\n",
+               "writer", urcu_get_thread_id());
        return ((void*)2);
 }
 
@@ -157,13 +143,13 @@ int main(int argc, char **argv)
        num_read = atoi(argv[1]);
        num_write = atoi(argv[2]);
 
-       reader_time = malloc(sizeof(*reader_time) * num_read);
-       writer_time = malloc(sizeof(*writer_time) * num_write);
-       tid_reader = malloc(sizeof(*tid_reader) * num_read);
-       tid_writer = malloc(sizeof(*tid_writer) * num_write);
+       reader_time = calloc(num_read, sizeof(*reader_time));
+       writer_time = calloc(num_write, sizeof(*writer_time));
+       tid_reader = calloc(num_read, sizeof(*tid_reader));
+       tid_writer = calloc(num_write, sizeof(*tid_writer));
 
-       printf("thread %-6s, thread id : %lx, tid %lu\n",
-                       "main", pthread_self(), (unsigned long)gettid());
+       printf("thread %-6s, tid %lu\n",
+               "main", urcu_get_thread_id());
 
        per_thread_lock = malloc(sizeof(struct per_thread_lock) * NR_READ);
 
This page took 0.025644 seconds and 4 git commands to generate.