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

5
Last change on this file since c3d8d9e was c3d8d9e, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/16 at 04:55:49

score: Get rid of mp_id parameter

Get rid of the mp_id parameter used for some thread queue methods. Use
THREAD_QUEUE_QUEUE_TO_OBJECT() instead.

  • 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      0,
94      0,
95      &lock_context
96    );
97    eno = 0;
98  } else {
99    _Futex_Queue_release( futex, &lock_context );
100    eno = EWOULDBLOCK;
101  }
102
103  return eno;
104}
105
106typedef struct {
107  ISR_lock_Context Base;
108  int              count;
109} Futex_Lock_context;
110
111static Thread_Control *_Futex_Flush_filter(
112  Thread_Control     *the_thread,
113  Thread_queue_Queue *queue,
114  ISR_lock_Context   *lock_context
115)
116{
117  Futex_Lock_context *futex_lock_context;
118
119  futex_lock_context = (Futex_Lock_context *) lock_context;
120
121  if ( futex_lock_context->count <= 0 ) {
122    return NULL;
123  }
124
125  --futex_lock_context->count;
126
127  return the_thread;
128}
129
130int _Futex_Wake( struct _Futex_Control *_futex, int count )
131{
132  Futex_Control      *futex;
133  Futex_Lock_context  lock_context;
134
135  futex = _Futex_Get( _futex );
136  _Futex_Queue_acquire( futex, &lock_context.Base );
137
138  /*
139   * For some synchronization objects like barriers the _Futex_Wake() must be
140   * called in the fast path.  Normally there are no threads on the queue, so
141   * check this condition early.
142   */
143  if ( __predict_true( _Thread_queue_Is_empty( &futex->Queue.Queue ) ) ) {
144    _Futex_Queue_release( futex, &lock_context.Base );
145    return 0;
146  }
147
148  lock_context.count = count;
149  return (int) _Thread_queue_Flush_critical(
150    &futex->Queue.Queue,
151    FUTEX_TQ_OPERATIONS,
152    _Futex_Flush_filter,
153    NULL,
154    &lock_context.Base
155  );
156}
157
158#endif /* HAVE_STRUCT__THREAD_QUEUE_QUEUE */
Note: See TracBrowser for help on using the repository browser.