Add timing tests
[urcu.git] / test_urcu.c
index 10d4d9961292d3149d04fe88417a7247de3c8ad8..7752e6ed23f5392e69b3bee6b50b0ef9e224f05a 100644 (file)
 #include <unistd.h>
 #include <stdio.h>
 #include <assert.h>
+#include <sys/syscall.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 "urcu.h"
 
 struct test_array {
@@ -36,13 +53,13 @@ void *thr_reader(void *arg)
        int qparity, i, j;
        struct test_array *local_ptr;
 
-       printf("thread %s, thread id : %lu, pid %lu\n",
-                       "reader", pthread_self(), getpid());
+       printf("thread %s, thread id : %lx, tid %lu\n",
+                       "reader", pthread_self(), (unsigned long)gettid());
        sleep(2);
 
        urcu_register_thread();
 
-       for (i = 0; i < 1000; i++) {
+       for (i = 0; i < 100000; i++) {
                for (j = 0; j < 100000000; j++) {
                        qparity = rcu_read_lock();
                        local_ptr = rcu_dereference(test_rcu_pointer);
@@ -66,23 +83,23 @@ void *thr_writer(void *arg)
        int i;
        struct test_array *new, *old;
 
-       printf("thread %s, thread id : %lu, pid %lu\n",
-                       "writer", pthread_self(), getpid());
+       printf("thread %s, thread id : %lx, tid %lu\n",
+                       "writer", pthread_self(), (unsigned long)gettid());
        sleep(2);
 
-       for (i = 0; i < 100000; i++) {
-               rcu_write_lock();
+       for (i = 0; i < 10000000; i++) {
                new = malloc(sizeof(struct test_array));
+               rcu_write_lock();
                old = test_rcu_pointer;
                if (old) {
                        assert(old->a == 8);
                        assert(old->b == 12);
                        assert(old->c[55] == 2);
                }
-               new->a = 8;
-               new->b = 12;
                new->c[55] = 2;
-               old = urcu_publish_content(&test_rcu_pointer, new);
+               new->b = 12;
+               new->a = 8;
+               old = urcu_publish_content((void **)&test_rcu_pointer, new);
                rcu_write_unlock();
                /* can be done after unlock */
                if (old) {
@@ -104,6 +121,9 @@ int main()
        void *tret;
        int i;
 
+       printf("thread %-6s, thread id : %lx, tid %lu\n",
+                       "main", pthread_self(), (unsigned long)gettid());
+
        for (i = 0; i < NR_READ; i++) {
                err = pthread_create(&tid_reader[i], NULL, thr_reader, NULL);
                if (err != 0)
@@ -117,7 +137,7 @@ int main()
 
        sleep(10);
 
-       for (i = 0; i < NR_WRITE; i++) {
+       for (i = 0; i < NR_READ; i++) {
                err = pthread_join(tid_reader[i], &tret);
                if (err != 0)
                        exit(1);
@@ -127,6 +147,7 @@ int main()
                if (err != 0)
                        exit(1);
        }
+       free(test_rcu_pointer);
 
        return 0;
 }
This page took 0.024906 seconds and 4 git commands to generate.