Skip to content

Commit 33ec49f

Browse files
authored
Merge pull request #4165 from Hxinrong/rt_mutex_takeBranch
add error checks of rt_mutex_take()
2 parents b8bd21f + 3f14078 commit 33ec49f

File tree

5 files changed

+110
-11
lines changed

5 files changed

+110
-11
lines changed

bsp/CME_M7/drivers/emac.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,19 @@ struct pbuf *rt_cme_eth_rx(rt_device_t dev)
312312
ETH_RX_DESC *desc;
313313
uint32_t framelength;
314314
struct rt_cme_eth * cme_eth = (struct rt_cme_eth *)dev;
315+
rt_err_t result;
315316

316-
rt_mutex_take(&cme_eth->lock, RT_WAITING_FOREVER);
317+
result = rt_mutex_take(&cme_eth->lock, RT_WAITING_FOREVER);
318+
if (result == -RT_ETIMEOUT)
319+
{
320+
rt_kprintf("Take mutex time out.\n");
321+
goto _exit;
322+
}
323+
else if (result == -RT_ERROR)
324+
{
325+
rt_kprintf("Take mutex error.\n");
326+
goto _exit;
327+
}
317328

318329
desc = ETH_AcquireFreeRxDesc();
319330
if(desc == RT_NULL)

bsp/fh8620/drivers/i2c.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ static rt_size_t fh_i2c_xfer(struct rt_i2c_bus_device *dev,
8787

8888
rt_completion_init(&i2c_drv->transfer_completion);
8989

90-
ret = rt_mutex_take(i2c_drv->lock, RT_WAITING_FOREVER );
90+
ret = rt_mutex_take(i2c_drv->lock, RT_WAITING_FOREVER);
91+
if (ret != RT_EOK) {
92+
goto done;
93+
}
9194

9295
i2c_drv->msgs = msgs;
9396
i2c_drv->msgs_num = num;

bsp/lpc55sxx/Libraries/drivers/drv_sd.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,19 @@ static rt_size_t rt_mci_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_siz
5252
{
5353
rt_uint8_t status = kStatus_Success;
5454
struct mci_device *mci = (struct mci_device *)dev;
55+
int ret;
5556

56-
rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
57+
ret = rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
58+
if (ret == -RT_ETIMEOUT)
59+
{
60+
rt_kprintf("Take mutex time out.\n");
61+
return ret;
62+
}
63+
else if (ret == -RT_ERROR)
64+
{
65+
rt_kprintf("Take mutex error.\n");
66+
return ret;
67+
}
5768

5869
{
5970
/* non-aligned. */
@@ -66,7 +77,7 @@ static rt_size_t rt_mci_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_siz
6677

6778
for(i=0; i<size; i++)
6879
{
69-
status=SD_ReadBlocks(&mci->card, sdio_buffer, sector_adr, 1);
80+
status = SD_ReadBlocks(&mci->card, sdio_buffer, sector_adr, 1);
7081

7182
memcpy(copy_buffer, sdio_buffer, mci->card.blockSize);
7283
sector_adr ++;
@@ -85,8 +96,19 @@ static rt_size_t rt_mci_write(rt_device_t dev, rt_off_t pos, const void *buffer,
8596
{
8697
rt_uint8_t status = kStatus_Success;
8798
struct mci_device *mci = (struct mci_device *)dev;
99+
int ret;
88100

89-
rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
101+
ret = rt_mutex_take(&mci->lock, RT_WAITING_FOREVER);
102+
if (ret == -RT_ETIMEOUT)
103+
{
104+
rt_kprintf("Take mutex time out.\n");
105+
return ret;
106+
}
107+
else if (ret == -RT_ERROR)
108+
{
109+
rt_kprintf("Take mutex error.\n");
110+
return ret;
111+
}
90112

91113
{
92114
/* non-aligned. */

bsp/simulator/drivers/sst25vfxx_mtd_sim.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,17 @@ static int sst25vfxx_read(struct rt_mtd_nor_device *device, rt_off_t position, r
5858
sst25 = SST25_MTD(device);
5959
RT_ASSERT(sst25 != RT_NULL);
6060

61-
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
61+
result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
62+
if (result == -RT_ETIMEOUT)
63+
{
64+
rt_kprintf("Take mutex time out.\n");
65+
return result;
66+
}
67+
else if (result == -RT_ERROR)
68+
{
69+
rt_kprintf("Take mutex error.\n");
70+
return result;
71+
}
6272

6373
fseek(sst25->file, position, SEEK_SET);
6474
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,
7888
sst25 = SST25_MTD(device);
7989
RT_ASSERT(sst25 != RT_NULL);
8090

81-
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
91+
result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
92+
if (result == -RT_ETIMEOUT)
93+
{
94+
rt_kprintf("Take mutex time out.\n");
95+
return result;
96+
}
97+
else if (result == -RT_ERROR)
98+
{
99+
rt_kprintf("Take mutex error.\n");
100+
return result;
101+
}
82102

83103
fseek(sst25->file, position, SEEK_SET);
84104
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
99119

100120
RT_ASSERT(sst25 != RT_NULL);
101121

102-
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
122+
result = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
123+
if (result == -RT_ETIMEOUT)
124+
{
125+
rt_kprintf("Take mutex time out.\n");
126+
return -RT_ETIMEOUT;
127+
}
128+
else if (result == -RT_ERROR)
129+
{
130+
rt_kprintf("Take mutex error.\n");
131+
return -RT_ERROR;
132+
}
103133

104134
memset(block_buffer, 0xFF, BLOCK_SIZE);
105135
fseek(sst25->file, offset, SEEK_SET);

bsp/swm320-lq100/drivers/drv_nor_flash.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ static rt_size_t swm320_read(struct rt_mtd_nor_device *device,
3232
rt_uint8_t *data,
3333
rt_size_t size)
3434
{
35-
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
35+
int ret = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
36+
if (ret == -RT_ETIMEOUT)
37+
{
38+
rt_kprintf("Take mutex time out.\n");
39+
return ret;
40+
}
41+
else if (ret == -RT_ERROR)
42+
{
43+
rt_kprintf("Take mutex error.\n");
44+
return ret;
45+
}
46+
3647
memcpy(data, ((const void *)(NORFLM_BASE + position)), size);
3748
rt_mutex_release(&flash_lock);
3849
return size;
@@ -45,7 +56,18 @@ static rt_size_t swm320_write(struct rt_mtd_nor_device *device,
4556
{
4657
rt_size_t i;
4758
const rt_uint16_t *hwdata = (const rt_uint16_t *)data;
48-
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
59+
int ret = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
60+
if (ret == -RT_ETIMEOUT)
61+
{
62+
rt_kprintf("Take mutex time out.\n");
63+
return ret;
64+
}
65+
else if (ret == -RT_ERROR)
66+
{
67+
rt_kprintf("Take mutex error.\n");
68+
return ret;
69+
}
70+
4971
for (i = 0; i < size / 2; i++)
5072
{
5173
NORFL_Write(position, hwdata[i]);
@@ -59,7 +81,18 @@ static rt_err_t swm320_erase_block(struct rt_mtd_nor_device *device,
5981
rt_off_t offset,
6082
rt_uint32_t length)
6183
{
62-
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
84+
rt_err_t ret = rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
85+
if (ret == -RT_ETIMEOUT)
86+
{
87+
rt_kprintf("Take mutex time out.\n");
88+
return ret;
89+
}
90+
else if (ret == -RT_ERROR)
91+
{
92+
rt_kprintf("Take mutex error.\n");
93+
return ret;
94+
}
95+
6396
NORFL_SectorErase(offset);
6497
rt_mutex_release(&flash_lock);
6598
return RT_EOK;

0 commit comments

Comments
 (0)