uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / common / api.h
index 67c388e13465ff322b02dda79e35579a7ee12f7a..ec3ce1ef7486251ec0321d0c65d18aa761816af4 100644 (file)
@@ -1,31 +1,20 @@
+// SPDX-FileCopyrightText: 2006 Paul E. McKenney, IBM.
+//
+// SPDX-License-Identifier: GPL-2.0-only
+
 #ifndef _INCLUDE_API_H
 #define _INCLUDE_API_H
 
 /*
  * common.h: Common Linux kernel-isms.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; but version 2 of the License only due
- * to code included from the Linux kernel.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Copyright (c) 2006 Paul E. McKenney, IBM.
- *
  * Much code taken from the Linux kernel.  For such code, the option
  * to redistribute under later versions of GPL might not be available.
  */
 
 #include <urcu/compiler.h>
 #include <urcu/arch.h>
+#include <urcu/uatomic.h>
 
 /*
  * Machine parameters.
 #define ____cacheline_internodealigned_in_smp \
        __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
 
-/*
- * api_pthreads.h: API mapping to pthreads environment.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.  However, please note that much
- * of the code in this file derives from the Linux kernel, and that such
- * code may not be available except under GPLv2.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Copyright (c) 2006 Paul E. McKenney, IBM.
- */
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -135,7 +103,7 @@ static int __smp_thread_id(void)
        thread_id_t tid = pthread_self();
 
        for (i = 0; i < NR_THREADS; i++) {
-               if (__thread_id_map[i] == tid) {
+               if (uatomic_read(&__thread_id_map[i]) == tid) {
                        long v = i + 1;  /* must be non-NULL. */
 
                        if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
@@ -165,7 +133,7 @@ static int smp_thread_id(void)
        id = pthread_getspecific(thread_id_key);
        if (id == NULL)
                return __smp_thread_id();
-       return (long)(id - 1);
+       return ((long) id - 1);
 }
 
 static thread_id_t create_thread(void *(*func)(void *), void *arg)
@@ -184,12 +152,13 @@ static thread_id_t create_thread(void *(*func)(void *), void *arg)
                exit(-1);
        }
        __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
-       spin_unlock(&__thread_id_map_mutex);
+
        if (pthread_create(&tid, NULL, func, arg) != 0) {
                perror("create_thread:pthread_create");
                exit(-1);
        }
-       __thread_id_map[i] = tid;
+       uatomic_set(&__thread_id_map[i], tid);
+       spin_unlock(&__thread_id_map_mutex);
        return tid;
 }
 
@@ -199,7 +168,7 @@ static void *wait_thread(thread_id_t tid)
        void *vp;
 
        for (i = 0; i < NR_THREADS; i++) {
-               if (__thread_id_map[i] == tid)
+               if (uatomic_read(&__thread_id_map[i]) == tid)
                        break;
        }
        if (i >= NR_THREADS){
@@ -211,7 +180,7 @@ static void *wait_thread(thread_id_t tid)
                perror("wait_thread:pthread_join");
                exit(-1);
        }
-       __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
+       uatomic_set(&__thread_id_map[i], __THREAD_ID_MAP_EMPTY);
        return vp;
 }
 
@@ -228,16 +197,20 @@ static void wait_all_threads(void)
        }
 }
 
+#ifdef HAVE_SCHED_SETAFFINITY
 static void run_on(int cpu)
 {
-#if HAVE_SCHED_SETAFFINITY
        cpu_set_t mask;
 
        CPU_ZERO(&mask);
        CPU_SET(cpu, &mask);
        sched_setaffinity(0, sizeof(mask), &mask);
-#endif /* HAVE_SCHED_SETAFFINITY */
 }
+#else
+
+static void run_on(int cpu __attribute__((unused)))
+{}
+#endif /* HAVE_SCHED_SETAFFINITY */
 
 /*
  * timekeeping -- very crude -- should use MONOTONIC...
This page took 0.024708 seconds and 4 git commands to generate.