zephyr/kernel/unified/mutex.c
Benjamin Walsh 456c6daa9f unified: initial unified kernel implementation
Summary of what this includes:

    initialization:

    Copy from nano_init.c, with the following changes:

    - the main thread is the continuation of the init thread, but an idle
      thread is created as well

    - _main() initializes threads in groups and starts the EXE group

    - the ready queues are initialized

    - the main thread is marked as non-essential once the system init is
      done

    - a weak main() symbol is provided if the application does not provide a
      main() function

    scheduler:

    Not an exhaustive list, but basically provide primitives for:

    - adding/removing a thread to/from a wait queue
    - adding/removing a thread to/from the ready queue
    - marking thread as ready
    - locking/unlocking the scheduler
      - instead of locking interrupts
    - getting/setting thread priority
      - checking what state (coop/preempt) a thread is currenlty running in
    - rescheduling threads
    - finding what thread is the next to run
    - yielding/sleeping/aborting sleep
    - finding the current thread

    threads:

    - Add operationns on threads, such as creating and starting them.

    standardized handling of kernel object return codes:

    - Kernel objects now cause _Swap() to return the following values:
         0      => operation successful
        -EAGAIN => operation timed out
        -Exxxxx => operation failed for another reason

    - The thread's swap_data field can be used to return any additional
    information required to complete the operation, such as the actual
    result of a successful operation.

    timeouts:

    - same as nano timeouts, renamed to simply 'timeouts'
    - the kernel is still tick-based, but objects take timeout values in
      ms for forward compatibility with a tickless kernel.

    semaphores:

      - Port of the nanokernel semaphores, which have the same basic behaviour
      as the microkernel ones. Semaphore groups are not yet implemented.

      - These semaphores are enhanced in that they accept an initial count and a
      count limit. This allows configuring them as binary semaphores, and also
      provisioning them without having to "give" the semaphore multiple times
      before using them.

    mutexes:

    - Straight port of the microkernel mutexes. An init function is added to
    allow defining them at runtime.

    pipes:

    - straight port

    timers:

    - amalgamation of nano and micro timers, with all functionalities
      intact.

    events:

    - re-implementation, using semaphores and workqueues.

    mailboxes:

    - straight port

    message queues:

    - straight port of  microkernel FIFOs

    memory maps:

    - straight port

    workqueues:

    - Basically, have all APIs follow the k_ naming rule, and use the _timeout
    subsystem from the unified kernel directory, and not the _nano_timeout
    one.

    stacks:

    - Port of the nanokernel stacks. They can now have multiple threads
    pending on them and threads can wait with a timeout.

    LIFOs:

    - Straight port of the nanokernel LIFOs.

    FIFOs:

    - Straight port of the nanokernel FIFOs.

Work by: Dmitriy Korovkin <dmitriy.korovkin@windriver.com>
         Peter Mitsis <peter.mitsis@windriver.com>
         Allan Stephens <allan.stephens@windriver.com>
         Benjamin Walsh <benjamin.walsh@windriver.com>

Change-Id: Id3cadb3694484ab2ca467889cfb029be3cd3a7d6
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
2016-09-13 17:12:55 -04:00

243 lines
5.9 KiB
C

/*
* Copyright (c) 2016 Wind River Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file @brief mutex kernel services
*
* This module contains routines for handling mutex locking and unlocking.
*
* Mutexes implement a priority inheritance algorithm that boosts the priority
* level of the owning thread to match the priority level of the highest
* priority thread waiting on the mutex.
*
* Each mutex that contributes to priority inheritance must be released in the
* reverse order in which is was acquired. Furthermore each subsequent mutex
* that contributes to raising the owning thread's priority level must be
* acquired at a point after the most recent "bumping" of the priority level.
*
* For example, if thread A has two mutexes contributing to the raising of its
* priority level, the second mutex M2 must be acquired by thread A after
* thread A's priority level was bumped due to owning the first mutex M1.
* When releasing the mutex, thread A must release M2 before it releases M1.
* Failure to follow this nested model may result in threads running at
* unexpected priority levels (too high, or too low).
*/
#include <kernel.h>
#include <nano_private.h>
#include <toolchain.h>
#include <sections.h>
#include <wait_q.h>
#include <misc/dlist.h>
#include <errno.h>
#ifdef CONFIG_OBJECT_MONITOR
#define RECORD_STATE_CHANGE(mutex) \
do { (mutex)->num_lock_state_changes++; } while ((0))
#define RECORD_CONFLICT(mutex) \
do { (mutex)->num_conflicts++; } while ((0))
#else
#define RECORD_STATE_CHANGE(mutex) do { } while ((0))
#define RECORD_CONFLICT(mutex) do { } while ((0))
#endif
#ifdef CONFIG_OBJECT_MONITOR
#define INIT_OBJECT_MONITOR(mutex) do { \
mutex->num_lock_state_changes = 0; \
mutex->num_conflicts = 0; \
} while ((0))
#else
#define INIT_OBJECT_MONITOR(mutex) do { } while ((0))
#endif
#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
#define INIT_KERNEL_TRACING(mutex) do { \
mutex->__next = NULL; \
} while ((0))
#else
#define INIT_KERNEL_TRACING(mutex) do { } while ((0))
#endif
void k_mutex_init(struct k_mutex *mutex)
{
mutex->owner = NULL;
mutex->lock_count = 0;
/* initialized upon first use */
/* mutex->owner_orig_prio = 0; */
sys_dlist_init(&mutex->wait_q);
INIT_OBJECT_MONITOR(mutex);
INIT_KERNEL_TRACING(mutex);
}
static int new_prio_for_inheritance(int target, int limit)
{
int new_prio = _is_prio_higher(target, limit) ? target : limit;
new_prio = _get_new_prio_with_ceiling(new_prio);
return new_prio;
}
static void adjust_owner_prio(struct k_mutex *mutex, int new_prio)
{
if (mutex->owner->prio != new_prio) {
K_DEBUG("%p (ready (y/n): %c) prio changed to %d (was %d)\n",
mutex->owner, _is_thread_ready(mutex->owner) ?
'y' : 'n',
new_prio, mutex->owner->prio);
_thread_priority_set(mutex->owner, new_prio);
}
}
int k_mutex_lock(struct k_mutex *mutex, int32_t timeout)
{
int new_prio, key;
k_sched_lock();
if (likely(mutex->lock_count == 0 || mutex->owner == _current)) {
RECORD_STATE_CHANGE();
mutex->owner_orig_prio = mutex->lock_count == 0 ?
_current->prio :
mutex->owner_orig_prio;
mutex->lock_count++;
mutex->owner = _current;
K_DEBUG("%p took mutex %p, count: %d, orig prio: %d\n",
_current, mutex, mutex->lock_count,
mutex->owner_orig_prio);
k_sched_unlock();
return 0;
}
RECORD_CONFLICT();
if (unlikely(timeout == K_NO_WAIT)) {
k_sched_unlock();
return -EBUSY;
}
#if 0
if (_is_prio_higher(_current->prio, mutex->owner->prio)) {
new_prio = _current->prio;
}
new_prio = _get_new_prio_with_ceiling(new_prio);
#endif
new_prio = new_prio_for_inheritance(_current->prio, mutex->owner->prio);
key = irq_lock();
K_DEBUG("adjusting prio up on mutex %p\n", mutex);
adjust_owner_prio(mutex, new_prio);
_pend_current_thread(&mutex->wait_q, timeout);
int got_mutex = _Swap(key);
K_DEBUG("on mutex %p got_mutex value: %d\n", mutex, got_mutex);
K_DEBUG("%p got mutex %p (y/n): %c\n", _current, mutex,
got_mutex ? 'y' : 'n');
if (got_mutex == 0) {
k_sched_unlock();
return 0;
}
/* timed out */
K_DEBUG("%p timeout on mutex %p\n", _current, mutex);
struct tcs *waiter = (struct tcs *)sys_dlist_peek_head(&mutex->wait_q);
new_prio = mutex->owner_orig_prio;
new_prio = waiter ? new_prio_for_inheritance(waiter->prio, new_prio) :
new_prio;
K_DEBUG("adjusting prio down on mutex %p\n", mutex);
key = irq_lock();
adjust_owner_prio(mutex, new_prio);
irq_unlock(key);
k_sched_unlock();
return -EAGAIN;
}
void k_mutex_unlock(struct k_mutex *mutex)
{
int key;
__ASSERT(mutex->owner == _current, "");
k_sched_lock();
RECORD_STATE_CHANGE();
mutex->lock_count--;
K_DEBUG("mutex %p lock_count: %d\n", mutex, mutex->lock_count);
if (mutex->lock_count != 0) {
k_sched_unlock();
return;
}
key = irq_lock();
adjust_owner_prio(mutex, mutex->owner_orig_prio);
struct tcs *new_owner = _unpend_first_thread(&mutex->wait_q);
K_DEBUG("new owner of mutex %p: %p (prio: %d)\n",
mutex, new_owner, new_owner ? new_owner->prio : -1000);
if (new_owner) {
_timeout_abort(new_owner);
_ready_thread(new_owner);
irq_unlock(key);
_set_thread_return_value(new_owner, 0);
/*
* new owner is already of higher or equal prio than first
* waiter since the wait queue is priority-based: no need to
* ajust its priority
*/
mutex->owner = new_owner;
mutex->lock_count++;
mutex->owner_orig_prio = new_owner->prio;
} else {
irq_unlock(key);
mutex->owner = NULL;
}
k_sched_unlock();
}