summaryrefslogtreecommitdiffstats
path: root/cpukit
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2015-07-13 10:00:28 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2015-07-30 09:11:18 +0200
commit40188718f232423c6e28922e39ea6e9c10eb444b (patch)
tree1b5d5408bd46e66e414f55f47e2c819024e9c6b2 /cpukit
parentscore: Add self-contained semaphore implementation (diff)
downloadrtems-40188718f232423c6e28922e39ea6e9c10eb444b.tar.bz2
score: Add self-contained futex implementation
Diffstat (limited to 'cpukit')
-rw-r--r--cpukit/libmisc/monitor/mon-prmisc.c1
-rw-r--r--cpukit/score/Makefile.am1
-rw-r--r--cpukit/score/include/rtems/score/statesimpl.h3
-rw-r--r--cpukit/score/src/futex.c199
4 files changed, 204 insertions, 0 deletions
diff --git a/cpukit/libmisc/monitor/mon-prmisc.c b/cpukit/libmisc/monitor/mon-prmisc.c
index 4656ed297a..14ba173256 100644
--- a/cpukit/libmisc/monitor/mon-prmisc.c
+++ b/cpukit/libmisc/monitor/mon-prmisc.c
@@ -134,6 +134,7 @@ static const rtems_assoc_t rtems_monitor_state_assoc[] = {
{ "Wseg", STATES_WAITING_FOR_SEGMENT, 0 },
{ "Wsem", STATES_WAITING_FOR_SEMAPHORE, 0 },
{ "Wsig", STATES_WAITING_FOR_SIGNAL, 0 },
+ { "Wslftx", STATES_WAITING_FOR_SYS_LOCK_FUTEX, 0 },
{ "Wslmtx", STATES_WAITING_FOR_SYS_LOCK_MUTEX, 0 },
{ "Wslsem", STATES_WAITING_FOR_SYS_LOCK_SEMAPHORE, 0 },
{ "Wsysev", STATES_WAITING_FOR_SYSTEM_EVENT, 0 },
diff --git a/cpukit/score/Makefile.am b/cpukit/score/Makefile.am
index 4b4c1f5fce..460076c6a9 100644
--- a/cpukit/score/Makefile.am
+++ b/cpukit/score/Makefile.am
@@ -345,6 +345,7 @@ libscore_a_SOURCES += src/apiext.c src/chain.c src/chainappend.c \
src/interr.c src/isr.c src/wkspace.c src/wkstringduplicate.c
libscore_a_SOURCES += src/isrisinprogress.c
libscore_a_SOURCES += src/debugisownerofallocator.c
+libscore_a_SOURCES += src/futex.c
libscore_a_SOURCES += src/profilingisrentryexit.c
libscore_a_SOURCES += src/mutex.c
libscore_a_SOURCES += src/once.c
diff --git a/cpukit/score/include/rtems/score/statesimpl.h b/cpukit/score/include/rtems/score/statesimpl.h
index 7805ca4c9b..82d222c490 100644
--- a/cpukit/score/include/rtems/score/statesimpl.h
+++ b/cpukit/score/include/rtems/score/statesimpl.h
@@ -90,6 +90,8 @@ extern "C" {
#define STATES_WAITING_FOR_SYS_LOCK_MUTEX 0x2000000
/** This macro corresponds to a task waiting for a <sys/lock.h> semaphore. */
#define STATES_WAITING_FOR_SYS_LOCK_SEMAPHORE 0x4000000
+/** This macro corresponds to a task waiting for a <sys/lock.h> futex. */
+#define STATES_WAITING_FOR_SYS_LOCK_FUTEX 0x8000000
/** This macro corresponds to a task which is in an interruptible
* blocking state.
@@ -109,6 +111,7 @@ extern "C" {
STATES_WAITING_FOR_BSD_WAKEUP | \
STATES_WAITING_FOR_SYS_LOCK_MUTEX | \
STATES_WAITING_FOR_SYS_LOCK_SEMAPHORE | \
+ STATES_WAITING_FOR_SYS_LOCK_FUTEX | \
STATES_WAITING_FOR_RWLOCK )
/** This macro corresponds to a task waiting which is blocked. */
diff --git a/cpukit/score/src/futex.c b/cpukit/score/src/futex.c
new file mode 100644
index 0000000000..2ec38a3747
--- /dev/null
+++ b/cpukit/score/src/futex.c
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2015 embedded brains GmbH. All rights reserved.
+ *
+ * embedded brains GmbH
+ * Dornierstr. 4
+ * 82178 Puchheim
+ * Germany
+ * <rtems@embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#if HAVE_CONFIG_H
+ #include "config.h"
+#endif
+
+#if HAVE_STRUCT__THREAD_QUEUE_QUEUE
+
+#include <sys/lock.h>
+#include <errno.h>
+
+#include <rtems/score/atomic.h>
+#include <rtems/score/chainimpl.h>
+#include <rtems/score/threadimpl.h>
+#include <rtems/score/threadqimpl.h>
+
+#define FUTEX_TQ_OPERATIONS &_Thread_queue_Operations_FIFO
+
+typedef struct {
+ Thread_queue_Syslock_queue Queue;
+} Futex_Control;
+
+RTEMS_STATIC_ASSERT(
+ offsetof( Futex_Control, Queue )
+ == offsetof( struct _Futex_Control, _Queue ),
+ FUTEX_CONTROL_QUEUE
+);
+
+RTEMS_STATIC_ASSERT(
+ sizeof( Futex_Control ) == sizeof( struct _Futex_Control ),
+ FUTEX_CONTROL_SIZE
+);
+
+static Futex_Control *_Futex_Get( struct _Futex_Control *_futex )
+{
+ return (Futex_Control *) _futex;
+}
+
+static Thread_Control *_Futex_Queue_acquire(
+ Futex_Control *futex,
+ ISR_lock_Context *lock_context
+)
+{
+ Thread_Control *executing;
+
+ _ISR_lock_ISR_disable( lock_context );
+ executing = _Thread_Executing;
+ _Thread_queue_Queue_acquire_critical(
+ &futex->Queue.Queue,
+ &executing->Potpourri_stats,
+ lock_context
+ );
+
+ return executing;
+}
+
+static void _Futex_Queue_release(
+ Futex_Control *futex,
+ ISR_lock_Context *lock_context
+)
+{
+ _Thread_queue_Queue_release( &futex->Queue.Queue, lock_context );
+}
+
+int _Futex_Wait( struct _Futex_Control *_futex, int *uaddr, int val )
+{
+ Futex_Control *futex;
+ ISR_lock_Context lock_context;
+ Thread_Control *executing;
+ int eno;
+
+ futex = _Futex_Get( _futex );
+ executing = _Futex_Queue_acquire( futex, &lock_context );
+
+ if ( *uaddr == val ) {
+ _Thread_queue_Enqueue_critical(
+ &futex->Queue.Queue,
+ FUTEX_TQ_OPERATIONS,
+ executing,
+ STATES_WAITING_FOR_SYS_LOCK_FUTEX,
+ 0,
+ 0,
+ &lock_context
+ );
+ eno = 0;
+ } else {
+ _Futex_Queue_release( futex, &lock_context );
+ eno = EWOULDBLOCK;
+ }
+
+ return eno;
+}
+
+/*
+ * Use a noinline function to force the compiler to set up and tear down the
+ * large stack frame only in the slow case.
+ */
+static __attribute__((noinline)) int _Futex_Wake_slow(
+ Futex_Control *futex,
+ int count,
+ Thread_queue_Heads *heads,
+ ISR_lock_Context *lock_context
+)
+{
+ Chain_Control unblock;
+ Chain_Node *node;
+ Chain_Node *tail;
+ int woken;
+
+ woken = 0;
+ _Chain_Initialize_empty( &unblock );
+
+ while ( count > 0 && heads != NULL ) {
+ const Thread_queue_Operations *operations;
+ Thread_Control *first;
+ bool do_unblock;
+
+ operations = FUTEX_TQ_OPERATIONS;
+ first = ( *operations->first )( heads );
+
+ do_unblock = _Thread_queue_Extract_locked(
+ &futex->Queue.Queue,
+ operations,
+ first
+ );
+ if (do_unblock) {
+ _Chain_Append_unprotected( &unblock, &first->Wait.Node.Chain );
+ }
+
+ ++woken;
+ --count;
+ heads = futex->Queue.Queue.heads;
+ }
+
+ node = _Chain_First( &unblock );
+ tail = _Chain_Tail( &unblock );
+
+ if ( node != tail ) {
+ Per_CPU_Control *cpu_self;
+
+ cpu_self = _Thread_Dispatch_disable_critical( lock_context );
+ _Futex_Queue_release( futex, lock_context );
+
+ do {
+ Thread_Control *thread;
+ Chain_Node *next;
+
+ next = _Chain_Next( node );
+ thread = THREAD_CHAIN_NODE_TO_THREAD( node );
+ _Thread_Unblock( thread );
+
+ node = next;
+ } while ( node != tail );
+
+ _Thread_Dispatch_enable( cpu_self );
+ } else {
+ _Futex_Queue_release( futex, lock_context );
+ }
+
+ return woken;
+}
+
+int _Futex_Wake( struct _Futex_Control *_futex, int count )
+{
+ Futex_Control *futex;
+ ISR_lock_Context lock_context;
+ Thread_queue_Heads *heads;
+
+ futex = _Futex_Get( _futex );
+ _Futex_Queue_acquire( futex, &lock_context );
+
+ /*
+ * For some synchronization objects like barriers the _Futex_Wake() must be
+ * called in the fast path. Normally there are no threads on the queue, so
+ * check this condition early.
+ */
+ heads = futex->Queue.Queue.heads;
+ if ( __predict_true( heads == NULL ) ) {
+ _Futex_Queue_release( futex, &lock_context );
+
+ return 0;
+ }
+
+ return _Futex_Wake_slow( futex, count, heads, &lock_context );
+}
+
+#endif /* HAVE_STRUCT__THREAD_QUEUE_QUEUE */