Skip to content

Commit 40e151d

Browse files
Added mutex-isOwned function
1 parent 982c01d commit 40e151d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

threadX/inc/u_tx_mutex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ typedef struct {
2525
uint8_t create_mutex(mutex_t *mutex); // Create a mutex
2626
uint8_t mutex_get(mutex_t *mutex); // Gets a mutex.
2727
uint8_t mutex_put(mutex_t *mutex); // Puts a mutex.
28+
bool mutex_isOwned(
29+
mutex_t *mutex); // Checks if a mutex is owned by the current thread.
2830

2931
#endif /* u_tx_queues.h */

threadX/src/u_tx_mutex.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ uint8_t mutex_put(mutex_t *mutex)
3939
return U_SUCCESS;
4040
}
4141

42+
/* Checks if a mutex is owned by the current thread. */
43+
bool mutex_isOwned(mutex_t *mutex) {
44+
TX_THREAD* mutex_thread; // Thread that currently owns the mutex.
45+
uint8_t status = tx_mutex_info_get(&mutex->_TX_MUTEX, TX_NULL, TX_NULL, &mutex_thread, TX_NULL, TX_NULL, TX_NULL);
46+
if(status != TX_SUCCESS) {
47+
PRINTLN_ERROR("Failed to call tx_mutex_info_get() (Status: %d/%s, Mutex: %s).", status, tx_status_toString(status), mutex->name);
48+
return false;
49+
}
50+
51+
TX_THREAD* current_thread = tx_thread_identify(); // The current active thread (i.e., the thread that called mutex_isOwned()).
52+
return (mutex_thread == current_thread); // If mutex_thread is the same as current_thread, return true. If not, return false.
53+
}
54+
4255
// clang-format on

0 commit comments

Comments
 (0)