mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
* **Documentation** * Added new C coding style guide detailing naming conventions for files, directories, macros, constants, variables, functions, and type definitions with illustrative examples. * Updated C++ coding style documentation with clarifications on C naming conventions and header directory organization patterns. * **Refactor** * Updated header include paths throughout the codebase to use lowercase naming conventions consistently.
63 lines
1.5 KiB
C
63 lines
1.5 KiB
C
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
#pragma once
|
|
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
|
|
#include <tactility/freertos/semphr.h>
|
|
#include <tactility/check.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
struct Mutex {
|
|
QueueHandle_t handle;
|
|
// TODO: Debugging functionality
|
|
};
|
|
|
|
inline static void mutex_construct(struct Mutex* mutex) {
|
|
assert(mutex->handle == NULL);
|
|
mutex->handle = xSemaphoreCreateMutex();
|
|
}
|
|
|
|
inline static void mutex_destruct(struct Mutex* mutex) {
|
|
assert(mutex->handle != NULL);
|
|
vPortAssertIfInISR();
|
|
vSemaphoreDelete(mutex->handle);
|
|
mutex->handle = NULL;
|
|
}
|
|
|
|
inline static void mutex_lock(struct Mutex* mutex) {
|
|
check(xPortInIsrContext() != pdTRUE);
|
|
xSemaphoreTake(mutex->handle, portMAX_DELAY);
|
|
}
|
|
|
|
inline static bool mutex_try_lock(struct Mutex* mutex) {
|
|
check(xPortInIsrContext() != pdTRUE);
|
|
return xSemaphoreTake(mutex->handle, 0) == pdTRUE;
|
|
}
|
|
|
|
inline static bool mutex_try_lock_timed(struct Mutex* mutex, TickType_t timeout) {
|
|
check(xPortInIsrContext() != pdTRUE);
|
|
return xSemaphoreTake(mutex->handle, timeout) == pdTRUE;
|
|
}
|
|
|
|
inline static bool mutex_is_locked(struct Mutex* mutex) {
|
|
if (xPortInIsrContext() == pdTRUE) {
|
|
return xSemaphoreGetMutexHolderFromISR(mutex->handle) != NULL;
|
|
} else {
|
|
return xSemaphoreGetMutexHolder(mutex->handle) != NULL;
|
|
}
|
|
}
|
|
|
|
inline static void mutex_unlock(struct Mutex* mutex) {
|
|
check(xPortInIsrContext() != pdTRUE);
|
|
xSemaphoreGive(mutex->handle);
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|