Browse Source

thread: encapsulation of the pthread_spin_xxx functions

Nathalie Furmento 11 years ago
parent
commit
93ce4ecd0e
5 changed files with 130 additions and 8 deletions
  1. 2 1
      configure.ac
  2. 1 0
      include/starpu_config.h.in
  3. 39 1
      include/starpu_thread.h
  4. 86 1
      src/common/thread.c
  5. 2 5
      src/common/thread.h

+ 2 - 1
configure.ac

@@ -1,7 +1,7 @@
 # StarPU --- Runtime system for heterogeneous multicore architectures.
 # StarPU --- Runtime system for heterogeneous multicore architectures.
 #
 #
 # Copyright (C) 2009-2014  Université de Bordeaux 1
 # Copyright (C) 2009-2014  Université de Bordeaux 1
-# Copyright (C) 2010, 2011, 2012, 2013  Centre National de la Recherche Scientifique
+# Copyright (C) 2010, 2011, 2012, 2013, 2014  Centre National de la Recherche Scientifique
 # Copyright (C) 2011  Télécom-SudParis
 # Copyright (C) 2011  Télécom-SudParis
 # Copyright (C) 2011, 2012  Institut National de Recherche en Informatique et Automatique
 # Copyright (C) 2011, 2012  Institut National de Recherche en Informatique et Automatique
 #
 #
@@ -197,6 +197,7 @@ AC_CHECK_FUNCS([sysconf])
 AC_CHECK_FUNC([pthread_spin_lock], have_pthread_spin_lock=yes, have_pthread_spin_lock=no)
 AC_CHECK_FUNC([pthread_spin_lock], have_pthread_spin_lock=yes, have_pthread_spin_lock=no)
 if test x$have_pthread_spin_lock = xyes; then
 if test x$have_pthread_spin_lock = xyes; then
 	AC_DEFINE(HAVE_PTHREAD_SPIN_LOCK,[],[pthread_spin_lock is available])
 	AC_DEFINE(HAVE_PTHREAD_SPIN_LOCK,[],[pthread_spin_lock is available])
+	AC_DEFINE(STARPU_HAVE_PTHREAD_SPIN_LOCK,[],[pthread_spin_lock is available])
 fi
 fi
 
 
 # yes, that's non portable, but it's still better than sched_setaffinity
 # yes, that's non portable, but it's still better than sched_setaffinity

+ 1 - 0
include/starpu_config.h.in

@@ -121,5 +121,6 @@ struct timespec
 #undef STARPU_USE_TOP
 #undef STARPU_USE_TOP
 
 
 #undef STARPU_HAVE_HWLOC
 #undef STARPU_HAVE_HWLOC
+#undef STARPU_HAVE_PTHREAD_SPIN_LOCK
 
 
 #endif
 #endif

+ 39 - 1
include/starpu_thread.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  *
  * Copyright (C) 2010, 2012-2014  Université de Bordeaux 1
  * Copyright (C) 2010, 2012-2014  Université de Bordeaux 1
- * Copyright (C) 2010, 2011, 2012, 2013  Centre National de la Recherche Scientifique
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014  Centre National de la Recherche Scientifique
  *
  *
  * StarPU is free software; you can redistribute it and/or modify
  * StarPU is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
  * it under the terms of the GNU Lesser General Public License as published by
@@ -24,12 +24,14 @@ extern "C"
 #endif
 #endif
 
 
 #include <starpu_config.h>
 #include <starpu_config.h>
+#include <starpu_util.h>
 #ifdef STARPU_SIMGRID
 #ifdef STARPU_SIMGRID
 #include <xbt/synchro_core.h>
 #include <xbt/synchro_core.h>
 #include <msg/msg.h>
 #include <msg/msg.h>
 #elif !defined(_MSC_VER)
 #elif !defined(_MSC_VER)
 #include <pthread.h>
 #include <pthread.h>
 #endif
 #endif
+#include <stdint.h>
 
 
 /*
 /*
  * Encapsulation of the pthread_create function.
  * Encapsulation of the pthread_create function.
@@ -216,6 +218,42 @@ int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier);
 
 
 #endif /* STARPU_SIMGRID, _MSC_VER */
 #endif /* STARPU_SIMGRID, _MSC_VER */
 
 
+/*
+ * Encapsulation of the pthread_spin_* functions.
+ */
+
+#if defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
+
+typedef struct
+{
+#ifdef STARPU_SIMGRID
+	int taken;
+#else /* we only have a trivial implementation yet ! */
+	uint32_t taken STARPU_ATTRIBUTE_ALIGNED(16);
+#endif
+} starpu_pthread_spinlock_t;
+
+int starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared);
+int starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock);
+int starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock);
+int starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock);
+int starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock);
+
+#else /* !( defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)) */
+
+typedef pthread_spinlock_t starpu_pthread_spinlock_t;
+#define starpu_pthread_spin_init pthread_spin_init
+#define starpu_pthread_spin_destroy pthread_spin_destroy
+#define starpu_pthread_spin_lock pthread_spin_lock
+#define starpu_pthread_spin_trylock pthread_spin_trylock
+#define starpu_pthread_spin_unlock pthread_spin_unlock
+
+#endif /* !( defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)) */
+
+/*
+ * Other needed pthread definitions
+ */
+
 #ifdef _MSC_VER
 #ifdef _MSC_VER
 typedef void* starpu_pthread_rwlock_t;
 typedef void* starpu_pthread_rwlock_t;
 typedef void* starpu_pthread_mutex_t;
 typedef void* starpu_pthread_mutex_t;

+ 86 - 1
src/common/thread.c

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  *
  * Copyright (C) 2010, 2012-2014  Université de Bordeaux 1
  * Copyright (C) 2010, 2012-2014  Université de Bordeaux 1
- * Copyright (C) 2010, 2011, 2012, 2013  Centre National de la Recherche Scientifique
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014  Centre National de la Recherche Scientifique
  *
  *
  * StarPU is free software; you can redistribute it and/or modify
  * StarPU is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
  * it under the terms of the GNU Lesser General Public License as published by
@@ -437,3 +437,88 @@ int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
 }
 }
 
 
 #endif /* STARPU_SIMGRID, _MSC_VER */
 #endif /* STARPU_SIMGRID, _MSC_VER */
+
+#if defined(STARPU_SIMGRID) || !defined(HAVE_PTHREAD_SPIN_LOCK)
+
+int starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared)
+{
+	lock->taken = 0;
+	return 0;
+}
+
+int starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock)
+{
+	/* we don't do anything */
+	return 0;
+}
+
+int starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
+{
+#ifdef STARPU_SIMGRID
+	while (1)
+	{
+		if (!lock->taken)
+		{
+			lock->taken = 1;
+			return 0;
+		}
+		/* Give hand to another thread, hopefully the one which has the
+		 * spinlock and probably just has also a short-lived mutex. */
+		MSG_process_sleep(0.000001);
+		STARPU_UYIELD();
+	}
+#else
+	uint32_t prev;
+	do
+	{
+		prev = STARPU_TEST_AND_SET(&lock->taken, 1);
+		if (prev)
+			STARPU_UYIELD();
+	}
+	while (prev);
+	return 0;
+#endif
+}
+
+int starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
+{
+#ifdef STARPU_SIMGRID
+	if (lock->taken)
+		return EBUSY;
+	lock->taken = 1;
+	return 0;
+#else
+	uint32_t prev;
+	prev = STARPU_TEST_AND_SET(&lock->taken, 1);
+	return (prev == 0)?0:EBUSY;
+#endif
+}
+
+int starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
+{
+#ifdef STARPU_SIMGRID
+	lock->taken = 0;
+	return 0;
+#else
+	STARPU_RELEASE(&lock->taken);
+	return 0;
+#endif
+}
+
+#endif /* defined(STARPU_SIMGRID) || !defined(HAVE_PTHREAD_SPIN_LOCK) */
+
+int _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock)
+{
+#ifdef STARPU_SIMGRID
+	STARPU_ASSERT(lock->taken);
+	return !lock->taken;
+#elif defined(HAVE_PTHREAD_SPIN_LOCK)
+	int ret = pthread_spin_trylock((pthread_spinlock_t *)lock);
+	STARPU_ASSERT(ret != 0);
+	return ret == 0;
+#else
+	STARPU_ASSERT(lock->taken);
+	return !lock->taken;
+#endif
+}
+

+ 2 - 5
src/common/thread.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  *
  * Copyright (C) 2010, 2012-2014  Université de Bordeaux 1
  * Copyright (C) 2010, 2012-2014  Université de Bordeaux 1
- * Copyright (C) 2010, 2011, 2012, 2013  Centre National de la Recherche Scientifique
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014  Centre National de la Recherche Scientifique
  *
  *
  * StarPU is free software; you can redistribute it and/or modify
  * StarPU is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
  * it under the terms of the GNU Lesser General Public License as published by
@@ -19,11 +19,8 @@
 #define __COMMON_THREAD_H__
 #define __COMMON_THREAD_H__
 
 
 #include <starpu.h>
 #include <starpu.h>
-#include <common/fxt.h>
 
 
-#ifdef HAVE_PTHREAD_SPIN_LOCK
-typedef pthread_spinlock_t _starpu_pthread_spinlock_t;
-#endif
+int _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock);
 
 
 #endif /* __COMMON_THREAD_H__ */
 #endif /* __COMMON_THREAD_H__ */