Skip to content

sys: add mutex lock with timeout function#6158

Merged
miri64 merged 1 commit intoRIOT-OS:masterfrom
lebrush:mutex-timeout
Jan 12, 2017
Merged

sys: add mutex lock with timeout function#6158
miri64 merged 1 commit intoRIOT-OS:masterfrom
lebrush:mutex-timeout

Conversation

@lebrush
Copy link
Copy Markdown
Member

@lebrush lebrush commented Nov 24, 2016

This implements mutex locking with a timeout, which is required for the semaphore implementation without IPC (#6155).

It's been split into a separate PR as suggested by @miri64

Depends on: #6211
Required by: #6155

@miri64 miri64 added this to the Release 2017.01 milestone Nov 24, 2016
@miri64 miri64 added Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Area: timers Area: timer subsystems labels Nov 24, 2016
* @brief Remove thread from list (internal)
*
* This will remove @p thread from @p list. It reuses the thread's rq_entry
* field. Used internally timeout mutex implementation.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you directly reference the function here?

Used internally by @ref xtimer_mutex_lock_timeout() "timeout mutex implementation".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Done

void thread_add_to_list(list_node_t *list, thread_t *thread);

/**
* @brief Remove thread from list (internal)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @internal?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread_add_to_list is not @internal. Should I update both?

@OlegHahm
Copy link
Copy Markdown
Member

@BytesGalore, didn't we have a discussion about this feature some years before?

@lebrush
Copy link
Copy Markdown
Member Author

lebrush commented Nov 25, 2016

What was the outcome of the discussion @OlegHahm ? :-)

@BytesGalore
Copy link
Copy Markdown
Member

@OlegHahm, @lebrush I really can't remember, but I guess it was related to the requirement of have condition variables. Since solely using mutex could result in a race and in some cases in a deadlock.

@lebrush
Copy link
Copy Markdown
Member Author

lebrush commented Nov 30, 2016

I see... I don't have such a deep overview of the sched and thread modules as @kaspar030 and @BytesGalore, can you guys think of any race/deadlock condition with the proposed implementation?

Also, I've to see if I've to rebase to accommodate the latest changes in the xtimer.

@kaspar030
Copy link
Copy Markdown
Contributor

@kaspar030 and @BytesGalore, can you guys think of any race/deadlock condition with the proposed implementation?

No, @BytesGalore, can you elaborate?

@lebrush
Copy link
Copy Markdown
Member Author

lebrush commented Dec 13, 2016

What is holding this back? I'd be nice to have this and #6155 merged for the next release

core/thread.c Outdated
list->next = new_node;
}

void thread_remove_from_list(list_node_t *list, thread_t *thread)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use clist_remove()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't consider it because: the list is not a circular list and because when added is using a thread_add_to_list which is in the thread module (I assume that this is done this way to lower inter-dependencies).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively: why not add a list_remove() implementation? Seems a lot cleaner.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread_add_to_list() is taking the thread's priority into account and adds the thread so afterwards the list is sorted by priority.

+1 for adding "list_remove()" to list.h! and using that!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding "list_remove()" to list.h

It's probably best to do that in a seperate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! Will do :-) now we'll have a PR which depends in this PR which depends in another PR, yeeey 🎉

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this will depend on #6211

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! Will do :-) now we'll have a PR which depends in this PR which depends in another PR, yeeey 🎉

Welcome to RIOT ;-P


static void _mutex_timeout(void *arg)
{
unsigned irqstate = irq_disable();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is run in ISR context, so no need for disabling IRQs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do need, otherwise you might run in a race condition in case an interrupt occurs while serving this interrupt. Imagine: an that the other interrupt occurs while removing the mutex from the queue and that interrupt does the same operation (i.e. by calling mutex_unlock).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lebrush RIOT currently doesn't allow nested interrupts, so while one ISR is running, no other will interrupt.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again what learnt ;-) Will fix

{
msg_t tmsg;
xtimer_t t;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

typedef struct {
mutex_t *mutex;
thread_t *thread;
int timeout;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this used for?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the mutex times out, this flag is set in order to indicate whether it timed out or not (see L257).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(after rebase, is line 255)

@kaspar030 kaspar030 changed the title Mutex timeout sys: add mutex lock with timeout function Dec 13, 2016
@lebrush
Copy link
Copy Markdown
Member Author

lebrush commented Dec 14, 2016

@kaspar030 rebased onto latest master and squashed changes

@lebrush lebrush force-pushed the mutex-timeout branch 2 times, most recently from bc5cfd0 to 6e33b59 Compare December 14, 2016 09:46
@miri64 miri64 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Dec 14, 2016
@miri64 miri64 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jan 10, 2017
@miri64
Copy link
Copy Markdown
Member

miri64 commented Jan 10, 2017

@kaspar030 ping?

@OlegHahm OlegHahm added the State: waiting for other PR State: The PR requires another PR to be merged first label Jan 10, 2017
@miri64 miri64 removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jan 10, 2017
@miri64
Copy link
Copy Markdown
Member

miri64 commented Jan 12, 2017

#6211 got merged

@miri64 miri64 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed State: waiting for other PR State: The PR requires another PR to be merged first labels Jan 12, 2017
@lebrush
Copy link
Copy Markdown
Member Author

lebrush commented Jan 12, 2017

Rebased. @kaspar030 any other change requested?

@kaspar030
Copy link
Copy Markdown
Contributor

ACK.

@lebrush
Copy link
Copy Markdown
Member Author

lebrush commented Jan 12, 2017

Can anyone trigger Murdock again, please?

Running './dist/tools/pr_check/pr_check.sh master' x
Command output:

	Pull request is waiting for another pull request according to its labels set on GitHub

@kaspar030 kaspar030 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jan 12, 2017
@miri64 miri64 merged commit 3ba1218 into RIOT-OS:master Jan 12, 2017
@lebrush lebrush deleted the mutex-timeout branch January 13, 2017 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants