Merge pull request #4165 from Hxinrong/rt_mutex_takeBranch

add error checks of rt_mutex_take()
This commit is contained in:
Bernard Xiong 2021-01-05 08:57:12 +08:00 committed by GitHub
commit 33ec49fcc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 11 deletions

View File

@ -312,8 +312,19 @@ struct pbuf *rt_cme_eth_rx(rt_device_t dev)
ETH_RX_DESC *desc; ETH_RX_DESC *desc;
uint32_t framelength; uint32_t framelength;
struct rt_cme_eth * cme_eth = (struct rt_cme_eth *)dev; struct rt_cme_eth * cme_eth = (struct rt_cme_eth *)dev;
rt_err_t result;
rt_mutex_take(&cme_eth->lock, RT_WAITING_FOREVER); result = rt_mutex_take(&cme_eth->lock, RT_WAITING_FOREVER);
if (result == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
goto _exit;
}
else if (result == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
goto _exit;
}
desc = ETH_AcquireFreeRxDesc(); desc = ETH_AcquireFreeRxDesc();
if(desc == RT_NULL) if(desc == RT_NULL)

View File

@ -88,6 +88,9 @@ static rt_size_t fh_i2c_xfer(struct rt_i2c_bus_device *dev,
rt_completion_init(&i2c_drv->transfer_completion); rt_completion_init(&i2c_drv->transfer_completion);
ret = rt_mutex_take(i2c_drv->lock, RT_WAITING_FOREVER); ret = rt_mutex_take(i2c_drv->lock, RT_WAITING_FOREVER);
if (ret != RT_EOK) {
goto done;
}
i2c_drv->msgs = msgs; i2c_drv->msgs = msgs;
i2c_drv->msgs_num = num; i2c_drv->msgs_num = num;

View File

@ -52,8 +52,19 @@ static rt_size_t rt_mci_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_siz
{ {
rt_uint8_t status = kStatus_Success; rt_uint8_t status = kStatus_Success;
struct mci_device *mci = (struct mci_device *)dev; struct mci_device *mci = (struct mci_device *)dev;
int ret;
rt_mutex_take(&mci->lock, RT_WAITING_FOREVER); ret = rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return ret;
}
else if (ret == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return ret;
}
{ {
/* non-aligned. */ /* non-aligned. */
@ -85,8 +96,19 @@ static rt_size_t rt_mci_write(rt_device_t dev, rt_off_t pos, const void *buffer,
{ {
rt_uint8_t status = kStatus_Success; rt_uint8_t status = kStatus_Success;
struct mci_device *mci = (struct mci_device *)dev; struct mci_device *mci = (struct mci_device *)dev;
int ret;
rt_mutex_take(&mci->lock, RT_WAITING_FOREVER); ret = rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return ret;
}
else if (ret == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return ret;
}
{ {
/* non-aligned. */ /* non-aligned. */

View File

@ -58,7 +58,17 @@ static int sst25vfxx_read(struct rt_mtd_nor_device *device, rt_off_t position, r
sst25 = SST25_MTD(device); sst25 = SST25_MTD(device);
RT_ASSERT(sst25 != RT_NULL); RT_ASSERT(sst25 != RT_NULL);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
if (result == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return result;
}
else if (result == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return result;
}
fseek(sst25->file, position, SEEK_SET); fseek(sst25->file, position, SEEK_SET);
result = fread(data, size, 1, sst25->file); result = fread(data, size, 1, sst25->file);
@ -78,7 +88,17 @@ static int sst25vfxx_write(struct rt_mtd_nor_device *device, rt_off_t position,
sst25 = SST25_MTD(device); sst25 = SST25_MTD(device);
RT_ASSERT(sst25 != RT_NULL); RT_ASSERT(sst25 != RT_NULL);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
if (result == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return result;
}
else if (result == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return result;
}
fseek(sst25->file, position, SEEK_SET); fseek(sst25->file, position, SEEK_SET);
result = fwrite(data, size, 1, sst25->file); result = fwrite(data, size, 1, sst25->file);
@ -99,7 +119,17 @@ static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t
RT_ASSERT(sst25 != RT_NULL); RT_ASSERT(sst25 != RT_NULL);
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
if (result == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return -RT_ETIMEOUT;
}
else if (result == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return -RT_ERROR;
}
memset(block_buffer, 0xFF, BLOCK_SIZE); memset(block_buffer, 0xFF, BLOCK_SIZE);
fseek(sst25->file, offset, SEEK_SET); fseek(sst25->file, offset, SEEK_SET);

View File

@ -32,7 +32,18 @@ static rt_size_t swm320_read(struct rt_mtd_nor_device *device,
rt_uint8_t *data, rt_uint8_t *data,
rt_size_t size) rt_size_t size)
{ {
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); int ret = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return ret;
}
else if (ret == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return ret;
}
memcpy(data, ((const void *)(NORFLM_BASE + position)), size); memcpy(data, ((const void *)(NORFLM_BASE + position)), size);
rt_mutex_release(&flash_lock); rt_mutex_release(&flash_lock);
return size; return size;
@ -45,7 +56,18 @@ static rt_size_t swm320_write(struct rt_mtd_nor_device *device,
{ {
rt_size_t i; rt_size_t i;
const rt_uint16_t *hwdata = (const rt_uint16_t *)data; const rt_uint16_t *hwdata = (const rt_uint16_t *)data;
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); int ret = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return ret;
}
else if (ret == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return ret;
}
for (i = 0; i < size / 2; i++) for (i = 0; i < size / 2; i++)
{ {
NORFL_Write(position, hwdata[i]); NORFL_Write(position, hwdata[i]);
@ -59,7 +81,18 @@ static rt_err_t swm320_erase_block(struct rt_mtd_nor_device *device,
rt_off_t offset, rt_off_t offset,
rt_uint32_t length) rt_uint32_t length)
{ {
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); rt_err_t ret = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
if (ret == -RT_ETIMEOUT)
{
rt_kprintf("Take mutex time out.\n");
return ret;
}
else if (ret == -RT_ERROR)
{
rt_kprintf("Take mutex error.\n");
return ret;
}
NORFL_SectorErase(offset); NORFL_SectorErase(offset);
rt_mutex_release(&flash_lock); rt_mutex_release(&flash_lock);
return RT_EOK; return RT_EOK;