From 761405592bb0abd971f077314f5e906422a7fe08 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 29 Oct 2018 16:58:07 -0500 Subject: [PATCH] Added mutex to DataFlash for thread safety --- .../DataFlashBlockDevice.cpp | 31 +++++++++++++++---- .../DataFlashBlockDevice.h | 3 ++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp b/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp index c71a3c3054..4dc5542c58 100644 --- a/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp +++ b/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp @@ -164,6 +164,7 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi, int DataFlashBlockDevice::init() { + _mutex.lock(); DEBUG_PRINTF("init\r\n"); if (!_is_initialized) { @@ -173,6 +174,7 @@ int DataFlashBlockDevice::init() uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1); if (val != 1) { + _mutex.unlock(); return BD_ERROR_OK; } @@ -281,33 +283,40 @@ int DataFlashBlockDevice::init() _is_initialized = true; } + _mutex.unlock(); return result; } int DataFlashBlockDevice::deinit() { + _mutex.lock(); DEBUG_PRINTF("deinit\r\n"); if (!_is_initialized) { _init_ref_count = 0; + _mutex.unlock(); return BD_ERROR_OK; } uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1); if (val) { + _mutex.unlock(); return BD_ERROR_OK; } _is_initialized = false; + _mutex.unlock(); return BD_ERROR_OK; } int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) { + _mutex.lock(); DEBUG_PRINTF("read: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size); if (!_is_initialized) { + _mutex.unlock(); return BD_ERROR_DEVICE_ERROR; } @@ -350,6 +359,7 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size) int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size) { + _mutex.lock(); DEBUG_PRINTF("program: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size); if (!_is_initialized) { @@ -416,6 +426,7 @@ int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t int DataFlashBlockDevice::erase(bd_addr_t addr, bd_size_t size) { + _mutex.lock(); DEBUG_PRINTF("erase: %" PRIX64 " %" PRIX64 "\r\n", addr, size); if (!_is_initialized) { @@ -484,23 +495,29 @@ bd_size_t DataFlashBlockDevice::get_program_size() const bd_size_t DataFlashBlockDevice::get_erase_size() const { + _mutex.lock(); DEBUG_PRINTF("erase size: %" PRIX16 "\r\n", _block_size); - - return _block_size; + bd_size_t block_size = _block_size; + _mutex.unlock(); + return block_size; } bd_size_t DataFlashBlockDevice::get_erase_size(bd_addr_t addr) const { + _mutex.lock(); DEBUG_PRINTF("erase size: %" PRIX16 "\r\n", _block_size); - - return _block_size; + bd_size_t block_size = _block_size; + _mutex.unlock(); + return block_size; } bd_size_t DataFlashBlockDevice::size() const { + _mutex.lock(); DEBUG_PRINTF("device size: %" PRIX32 "\r\n", _device_size); - - return _device_size; + bd_size_t device_size = _device_size; + _mutex.unlock(); + return device_size; } /** @@ -512,6 +529,7 @@ bd_size_t DataFlashBlockDevice::size() const */ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode) { + _mutex.lock(); DEBUG_PRINTF("_get_register: %" PRIX8 "\r\n", opcode); /* activate device */ @@ -527,6 +545,7 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode) /* deactivate device */ _cs = 1; + _mutex.unlock(); return status; } diff --git a/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h b/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h index 52e2600e09..bd447e7439 100644 --- a/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h +++ b/components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h @@ -173,6 +173,9 @@ private: int _sync(void); int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size); uint32_t _translate_address(bd_addr_t addr); + + // Mutex for thread safety + mutable PlatformMutex _mutex; };