Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use temp variable in mutex release loop #52

Merged
merged 3 commits into from
Oct 4, 2016

Conversation

kuggenhoffen
Copy link
Contributor

Fix in #51 still had possibility of unexpected behavior as the mutex_lock_count variable might get modified after releasing mutex during last loop iteration.

@kuggenhoffen
Copy link
Contributor Author

@kjbracey-arm @TeroJaasko @SeppoTakalo @tommikas
I think the previous fix still had possibility of modifying mutex_lock_count during the last loop iteration immediately after releasing the mutex resulting in unexpected behavior.

Don't merge yet, I will test this still tomorrow morning.

@jupe jupe added the bugfix label Oct 3, 2016
@jupe jupe added this to the v1.1.3 milestone Oct 3, 2016
@@ -454,8 +454,12 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)

end:
if ( m_trace.mutex_release_f ) {
while (m_trace.mutex_lock_count > 0) {
m_trace.mutex_lock_count--;
// Store the mutex lock count to temp variable so that it won't get
Copy link
Contributor

Choose a reason for hiding this comment

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

"temp_mutex_lock_count" is long-winded to the point of making it hard to read - looks visually like the global.

Just "count" would do.

Could make it a "do/while" loop, as we "know" count is at least 1, and we hold the mutex at least once.

Logic seems solid though. (But then I didn't spot the problem in the last one either.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe

int count = m_trace.mutex_lock_count;
m_trace.mutex_lock_count = 0;
do {
    m_trace.mutex_release_f();
} while (--count > 0);

Copy link
Contributor

Choose a reason for hiding this comment

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

This might be a good time to add some kind of comments why the unlocking loop exists at all. Every time I have looked that code it has given a impression that something must be very, very wrong here.
+1 for functional part.

Copy link

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree on adding more comment on this. I thought I did already but I guess I forgot to commit it last night.

@kuggenhoffen
Copy link
Contributor Author

Guys does this look good now? The unittest coverage fails because this removes 1 line completely and resulting coverage is 0.04% less than previously....

Copy link
Contributor

@SeppoTakalo SeppoTakalo left a comment

Choose a reason for hiding this comment

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

Looks OK-ish..
However, I don't understand the need of this counter. Why helpers do not unlock, if they lock.

@kuggenhoffen
Copy link
Contributor Author

@SeppoTakalo @TeroJaasko
I explained the mutex unlock loop a bit more on the comment here, let me know if it still is not obvious enough.

@tommikas
Copy link

tommikas commented Oct 4, 2016

@kuggenhoffen It's green now.

@SeppoTakalo : A comment in the original PR: #30 (comment)

@tommikas
Copy link

tommikas commented Oct 4, 2016

That comment seems fairly explicit.

@kuggenhoffen kuggenhoffen merged commit 31e338c into master Oct 4, 2016
@yogpan01
Copy link

yogpan01 commented Oct 4, 2016

@SeppoTakalo Please make a PR to mbed-os for this fix as well. We need to get this in mbed-os 5.2

@tommikas tommikas deleted the better-mutex-unlock-fix branch December 27, 2016 11:08
tommikas pushed a commit that referenced this pull request Jun 16, 2017
* origin/master: (22 commits)
  output/ added to gitignore list (#66)
  Added include_directories(${CMAKE_CURRENT_SOURCE_DIR}/) include to resolve compilation error for Linux. (#63)
  Ensure tr_array doesn't print <null> when len == 0 (#65)
  Add CMakeLists.txt to support PAL Tools (#61)
  Fix header documentation (#58)
  Update README.md (#57)
  Provide environment-agnostic configuration macros. (#54)
  Flash size can be limited by defining MBED_TRACE_MAX_LEVEL
  Use temp variable in mutex release loop (#52)
  Fix race condition when unlocking mutex
  Add instructions for enabling the library in mbed OS 5. (#50)
  document update. (#47)
  Allow setting mutex locking/unlocking callbacks before trace init. (#46)
  Ipv6 helpers update (#44)
  Fix disabling traces through the YOTTA_CFG_MBED_TRACE flag. (#45)
  Deprecate the misnamed YOTTA_CFG_MTRACE_TMP_LINE_LENGTH flag, which is to be removed in the next release. (#43)
  Changed dummy definitions to match the actual functions' return types (#40)
  FEA_TRACE_SUPPORT flag is always set when traces are enabled. (#39)
  Release merge to master (#38)
  Add nanostack-libservice lib to CMakeLists (#31)
  ...
@jupe jupe modified the milestones: v1.2.2, v1.1.3 Jul 5, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants