source: rtems/cpukit/score/src/futex.c @ dce48791

5
Last change on this file since dce48791 was dce48791, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/16 at 11:37:59

score: Add Status_Control for all APIs

Unify the status codes of the Classic and POSIX API to use the new enum
Status_Control. This eliminates the Thread_Control::Wait::timeout_code
field and the timeout parameter of _Thread_queue_Enqueue_critical() and
_MPCI_Send_request_packet(). It gets rid of the status code translation
tables and instead uses simple bit operations to get the status for a
particular API. This enables translation of status code constants at
compile time. Add _Thread_Wait_get_status() to avoid direct access of
thread internal data structures.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2015, 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#if HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#if HAVE_STRUCT__THREAD_QUEUE_QUEUE
20
21#include <sys/lock.h>
22#include <errno.h>
23
24#include <rtems/score/atomic.h>
25#include <rtems/score/chainimpl.h>
26#include <rtems/score/threadimpl.h>
27#include <rtems/score/threadqimpl.h>
28
29#define FUTEX_TQ_OPERATIONS &_Thread_queue_Operations_FIFO
30
31typedef struct {
32  Thread_queue_Syslock_queue Queue;
33} Futex_Control;
34
35RTEMS_STATIC_ASSERT(
36  offsetof( Futex_Control, Queue )
37    == offsetof( struct _Futex_Control, _Queue ),
38  FUTEX_CONTROL_QUEUE
39);
40
41RTEMS_STATIC_ASSERT(
42  sizeof( Futex_Control ) == sizeof( struct _Futex_Control ),
43  FUTEX_CONTROL_SIZE
44);
45
46static Futex_Control *_Futex_Get( struct _Futex_Control *_futex )
47{
48  return (Futex_Control *) _futex;
49}
50
51static Thread_Control *_Futex_Queue_acquire(
52  Futex_Control    *futex,
53  ISR_lock_Context *lock_context
54)
55{
56  Thread_Control *executing;
57
58  _ISR_lock_ISR_disable( lock_context );
59  executing = _Thread_Executing;
60  _Thread_queue_Queue_acquire_critical(
61    &futex->Queue.Queue,
62    &executing->Potpourri_stats,
63    lock_context
64  );
65
66  return executing;
67}
68
69static void _Futex_Queue_release(
70  Futex_Control    *futex,
71  ISR_lock_Context *lock_context
72)
73{
74  _Thread_queue_Queue_release( &futex->Queue.Queue, lock_context );
75}
76
77int _Futex_Wait( struct _Futex_Control *_futex, int *uaddr, int val )
78{
79  Futex_Control    *futex;
80  ISR_lock_Context  lock_context;
81  Thread_Control   *executing;
82  int               eno;
83
84  futex = _Futex_Get( _futex );
85  executing = _Futex_Queue_acquire( futex, &lock_context );
86
87  if ( *uaddr == val ) {
88    _Thread_queue_Enqueue_critical(
89      &futex->Queue.Queue,
90      FUTEX_TQ_OPERATIONS,
91      executing,
92      STATES_WAITING_FOR_SYS_LOCK_FUTEX,
93      WATCHDOG_NO_TIMEOUT,
94      &lock_context
95    );
96    eno = 0;
97  } else {
98    _Futex_Queue_release( futex, &lock_context );
99    eno = EWOULDBLOCK;
100  }
101
102  return eno;
103}
104
105typedef struct {
106  Thread_queue_Context Base;
107  int                  count;
108} Futex_Context;
109
110static Thread_Control *_Futex_Flush_filter(
111  Thread_Control       *the_thread,
112  Thread_queue_Queue   *queue,
113  Thread_queue_Context *queue_context
114)
115{
116  Futex_Context *context;
117
118  context = (Futex_Context *) queue_context;
119
120  if ( context->count <= 0 ) {
121    return NULL;
122  }
123
124  --context->count;
125
126  return the_thread;
127}
128
129int _Futex_Wake( struct _Futex_Control *_futex, int count )
130{
131  Futex_Control      *futex;
132  Futex_Context  context;
133
134  futex = _Futex_Get( _futex );
135  _Futex_Queue_acquire( futex, &context.Base.Lock_context );
136
137  /*
138   * For some synchronization objects like barriers the _Futex_Wake() must be
139   * called in the fast path.  Normally there are no threads on the queue, so
140   * check this condition early.
141   */
142  if ( __predict_true( _Thread_queue_Is_empty( &futex->Queue.Queue ) ) ) {
143    _Futex_Queue_release( futex, &context.Base.Lock_context );
144    return 0;
145  }
146
147  context.count = count;
148  return (int) _Thread_queue_Flush_critical(
149    &futex->Queue.Queue,
150    FUTEX_TQ_OPERATIONS,
151    _Futex_Flush_filter,
152    &context.Base
153  );
154}
155
156#endif /* HAVE_STRUCT__THREAD_QUEUE_QUEUE */
Note: See TracBrowser for help on using the repository browser.