JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A try-lock on a mutex that has already been locked with no intervening unlock.
| Class Name | Try-lock that will never succeed | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | |||||||||
| Mnemonic | CONCURRENCY.TL | |||||||||
| Categories |
|
|||||||||
| Availability | Available for C and C++. |
|||||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Try-lock that will never succeed" |
#include <pthread.h>
#include <stdlib.h>
void concurrency_tl_bad(void){
pthread_mutex_t mut;
if( pthread_mutex_init( &mut, NULL ) != 0 ) abort();
if( pthread_mutex_lock( &mut ) != 0 ) abort();
if( pthread_mutex_trylock( &mut ) != 0 ) abort(); /* 'Try-lock that will never succeed' warning issued here
* - &mut is already locked by the previous call
* - execution will never reach the following line
*/
if( pthread_mutex_unlock( &mut ) != 0 ) abort();
if( pthread_mutex_destroy( &mut ) != 0 ) abort();
}
void concurrency_tl_ok(void){
pthread_mutex_t mut;
if( pthread_mutex_init( &mut, NULL ) != 0 ) abort();
if( pthread_mutex_trylock( &mut ) != 0 ) abort(); /* ok: &mut is not already locked */
if( pthread_mutex_unlock( &mut ) != 0 ) abort();
if( pthread_mutex_destroy( &mut ) != 0 ) abort();
}
CodeSonar ships with library models that allow it to recognize a number of try-lock functions, across many different libraries. Some examples are shown in the table below. If one of these functions is called when the mutex is always already locked, a warning will be issued.
If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Try-lock that will never succeed warnings.
| Functions that can trigger warnings include... | |
|---|---|
| Apache Portable Runtime (APR) | apr_global_mutex_trylock() |
| libc | pthread_mutex_trylock() |
| Linux Kernel | down_trylock() |
| Mac OS X | hw_lock_try() |
| OpenMP | omp_test_lock() |
| Qt | QMutex::tryLock() |
| VxWorks | semBCreate() |
| Win32 | TryEnterCriticalSection() |
| wxWidgets | wxMutex::TryLock() |
This warning indicates a redundant (and potentially misleading) program statement.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.