Changeset b3ff71e in rtems-libbsd


Ignore:
Timestamp:
Apr 30, 2015, 8:41:34 AM (4 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, fd86c091b97759106da7355ce1dd81ebe030e285, freebsd-9.3, f020f08430150c1656a0ad0a1de13699db9b980b
Children:
c2f2392
Parents:
165dd8e
git-author:
Sebastian Huber <sebastian.huber@…> (04/30/15 08:41:34)
git-committer:
Sebastian Huber <sebastian.huber@…> (05/20/15 07:58:21)
Message:

rtems-bsd-mutex: Use standard thread queues

Location:
rtemsbsd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • rtemsbsd/include/machine/rtems-bsd-mutex.h

    r165dd8e rb3ff71e  
    4141#define _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEX_H_
    4242
    43 #include <rtems/score/isrlock.h>
    44 #include <rtems/score/rbtree.h>
    4543#include <rtems/score/thread.h>
     44#include <rtems/score/threadq.h>
    4645
    4746#ifdef __cplusplus
     
    5049
    5150typedef struct {
    52         ISR_LOCK_MEMBER(lock)
     51        Thread_queue_Control queue;
    5352        Thread_Control *owner;
    5453        int nest_level;
    55         RBTree_Control rivals;
    5654} rtems_bsd_mutex;
    5755
  • rtemsbsd/include/machine/rtems-bsd-muteximpl.h

    r165dd8e rb3ff71e  
    5858    struct lock_class *class, const char *name, const char *type, int flags)
    5959{
    60         _ISR_lock_Initialize(&m->lock, name);
     60        _Thread_queue_Initialize(&m->queue, THREAD_QUEUE_DISCIPLINE_PRIORITY);
    6161        m->owner = NULL;
    6262        m->nest_level = 0;
    63         _RBTree_Initialize_empty(&m->rivals);
    6463
    6564        lock_init(lock, class, name, type, flags);
     
    7776        Thread_Control *owner;
    7877
    79         _ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);
     78        _Thread_queue_Acquire(&m->queue, &lock_context);
    8079
    8180        owner = m->owner;
    8281        executing = _Thread_Executing;
     82        ++executing->resource_count;
    8383
    8484        if (__predict_true(owner == NULL)) {
    8585                m->owner = executing;
    86                 ++executing->resource_count;
    87 
    88                 _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
     86
     87                _Thread_queue_Release(&m->queue, &lock_context);
    8988        } else {
    9089                rtems_bsd_mutex_lock_more(lock, m, owner, executing,
     
    101100        Thread_Control *owner;
    102101
    103         _ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);
     102        _Thread_queue_Acquire(&m->queue, &lock_context);
    104103
    105104        owner = m->owner;
     
    118117        }
    119118
    120         _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
     119        _Thread_queue_Release(&m->queue, &lock_context);
    121120
    122121        return (success);
     
    133132        int nest_level;
    134133
    135         _ISR_lock_ISR_disable_and_acquire(&m->lock, &lock_context);
     134        _Thread_queue_Acquire(&m->queue, &lock_context);
    136135
    137136        nest_level = m->nest_level;
     
    141140
    142141        if (__predict_true(nest_level == 0)) {
    143                 RBTree_Node *first = _RBTree_First(&m->rivals, RBT_LEFT);
     142                RBTree_Node *first;
    144143                int keep_priority;
    145144
    146145                --owner->resource_count;
     146
     147                /*
     148                 * Ensure that the owner resource count is visible to all other
     149                 * processors and that we read the latest priority restore
     150                 * hint.
     151                 */
     152                _Atomic_Fence( ATOMIC_ORDER_ACQ_REL );
     153
     154                first = _RBTree_First(&m->queue.Queues.Priority, RBT_LEFT);
    147155                keep_priority = _Thread_Owns_resources(owner)
    148                     || owner->real_priority == owner->current_priority;
     156                    || !owner->priority_restore_hint;
    149157
    150158                m->owner = NULL;
    151159
    152160                if (__predict_true(first == NULL && keep_priority)) {
    153                         _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
     161                        _Thread_queue_Release(&m->queue, &lock_context);
    154162                } else {
    155163                        rtems_bsd_mutex_unlock_more(m, owner, keep_priority,
     
    160168                m->nest_level = nest_level - 1;
    161169
    162                 _ISR_lock_Release_and_ISR_enable(&m->lock, &lock_context);
     170                _Thread_queue_Release(&m->queue, &lock_context);
    163171        }
    164172}
     
    181189rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m)
    182190{
    183         BSD_ASSERT(_RBTree_Is_empty(&m->rivals));
     191        BSD_ASSERT(_RBTree_Is_empty(&m->queue.Queues.Priority));
    184192
    185193        if (rtems_bsd_mutex_owned(m)) {
     
    188196        }
    189197
    190         _ISR_lock_Destroy(&m->lock);
     198        _Thread_queue_Dequeue(&m->queue);
    191199        lock_destroy(lock);
    192200}
  • rtemsbsd/rtems/rtems-bsd-muteximpl.c

    r165dd8e rb3ff71e  
    4242
    4343#include <rtems/score/schedulerimpl.h>
    44 #include <rtems/score/threaddispatch.h>
    4544#include <rtems/score/threadqimpl.h>
    46 
    47 #define INTEND_TO_BLOCK \
    48     (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK)
    49 
    50 #define BLOCKED \
    51     (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_BLOCKED)
    52 
    53 #define INTERRUPT_SATISFIED \
    54     (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTERRUPT_SATISFIED)
    55 
    56 static void
    57 rtems_bsd_mutex_priority_change(Thread_Control *thread,
    58     Priority_Control new_priority, void *context)
    59 {
    60         rtems_bsd_mutex *m = context;
    61 
    62         _RBTree_Extract(&m->rivals, &thread->RBNode);
    63         _RBTree_Insert(&m->rivals, &thread->RBNode,
    64             _Thread_queue_Compare_priority, false);
    65 }
    6645
    6746void
     
    7453                ++m->nest_level;
    7554
    76                 _ISR_lock_Release_and_ISR_enable(&m->lock, lock_context);
     55                _Thread_queue_Release(&m->queue, lock_context);
    7756        } else {
    78                 Per_CPU_Control *cpu_self;
    79                 bool success;
     57                /* Priority inheritance */
     58                _Thread_Raise_priority(owner, executing->current_priority);
    8059
    81                 _Thread_Lock_set(executing, &m->lock);
    82                 _Thread_Priority_set_change_handler(executing,
    83                     rtems_bsd_mutex_priority_change, m);
    84                 ++executing->resource_count;
    85                 _RBTree_Insert(&m->rivals, &executing->RBNode,
    86                     _Thread_queue_Compare_priority, false);
    87 
    88                 cpu_self = _Thread_Dispatch_disable_critical();
    89 
    90                 /* Priority inheritance */
    91                 _Scheduler_Change_priority_if_higher(_Scheduler_Get(owner),
    92                     owner, executing->current_priority, false);
    93 
    94                 _Thread_Wait_flags_set(executing, INTEND_TO_BLOCK);
    95 
    96                 _ISR_lock_Release_and_ISR_enable(&m->lock, lock_context);
    97 
    98                 _Thread_Set_state(executing, STATES_WAITING_FOR_MUTEX);
    99 
    100                 success = _Thread_Wait_flags_try_change(executing,
    101                     INTEND_TO_BLOCK, BLOCKED);
    102                 if (!success) {
    103                         _Thread_Clear_state(executing,
    104                             STATES_WAITING_FOR_MUTEX);
    105                 }
    106 
    107                 _Thread_Dispatch_enable(cpu_self);
     60                _Thread_queue_Enqueue_critical(&m->queue, executing,
     61                    STATES_WAITING_FOR_MUTEX, WATCHDOG_NO_TIMEOUT, 0,
     62                    lock_context);
    10863        }
    10964}
     
    11570        if (first != NULL) {
    11671                Thread_Control *new_owner;
    117                 bool success;
    11872
    11973                new_owner = THREAD_RBTREE_NODE_TO_THREAD(first);
    12074                m->owner = new_owner;
    121                 _RBTree_Extract(&m->rivals, &new_owner->RBNode);
    122                 _Thread_Priority_restore_default_change_handler(new_owner);
    123                 _Thread_Lock_restore_default(new_owner);
    124 
    125                 success = _Thread_Wait_flags_try_change_critical(new_owner,
    126                     INTEND_TO_BLOCK, INTERRUPT_SATISFIED);
    127                 if (success) {
    128                         _ISR_lock_Release_and_ISR_enable(&m->lock,
    129                             lock_context);
    130                 } else {
    131                         Per_CPU_Control *cpu_self;
    132 
    133                         cpu_self = _Thread_Dispatch_disable_critical();
    134                         _ISR_lock_Release_and_ISR_enable(&m->lock,
    135                             lock_context);
    136 
    137                         _Thread_Clear_state(new_owner,
    138                             STATES_WAITING_FOR_MUTEX);
    139 
    140                         _Thread_Dispatch_enable(cpu_self);
    141                 }
     75                _Thread_queue_Extract_critical(&m->queue, new_owner,
     76                    lock_context);
    14277        } else {
    143                 _ISR_lock_Release_and_ISR_enable(&m->lock, lock_context);
     78                _Thread_queue_Release(&m->queue, lock_context);
    14479        }
    14580
     
    14883
    14984                cpu_self = _Thread_Dispatch_disable();
    150                 _Thread_Change_priority(owner, owner->real_priority, true);
     85                _Thread_Restore_priority(owner);
    15186                _Thread_Dispatch_enable(cpu_self);
    15287        }
Note: See TracChangeset for help on using the changeset viewer.