From dc366ecf7a2946c8f071735f5f2065d51000240a Mon Sep 17 00:00:00 2001 From: Grissiom Date: Wed, 18 Jun 2014 11:16:21 +0800 Subject: [PATCH 1/2] device: only increase the ref_count when device is truly opened --- src/device.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/device.c b/src/device.c index 75b88bf6e..7ee130041 100644 --- a/src/device.c +++ b/src/device.c @@ -239,11 +239,6 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) return -RT_EBUSY; } - dev->ref_count++; - /* don't let bad things happen silently. If you are bitten by this assert, - * please set the ref_count to a bigger type. */ - RT_ASSERT(dev->ref_count != 0); - /* call device open interface */ if (dev->open != RT_NULL) { @@ -252,8 +247,15 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) /* set open flag */ if (result == RT_EOK || result == -RT_ENOSYS) + { dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN; + dev->ref_count++; + /* don't let bad things happen silently. If you are bitten by this assert, + * please set the ref_count to a bigger type. */ + RT_ASSERT(dev->ref_count != 0); + } + return result; } RTM_EXPORT(rt_device_open); From aca8f8eb2d732af34be31b7c9dce43de5187496e Mon Sep 17 00:00:00 2001 From: Grissiom Date: Wed, 18 Jun 2014 11:18:43 +0800 Subject: [PATCH 2/2] shell: print the ref count in `list_device` --- components/finsh/cmd.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 9a79ba396..e449e0c2d 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -471,17 +471,18 @@ static long _list_device(struct rt_list_node *list) "Unknown" }; - rt_kprintf("device type \n"); - rt_kprintf("-------- ---------- \n"); + rt_kprintf("device type ref count\n"); + rt_kprintf("-------- -------------------- ----------\n"); for (node = list->next; node != list; node = node->next) { device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list)); - rt_kprintf("%-8.*s %-8s \n", + rt_kprintf("%-8.*s %-20s %-8d\n", RT_NAME_MAX, device->parent.name, (device->type <= RT_Device_Class_Unknown) ? device_type_str[device->type] : - device_type_str[RT_Device_Class_Unknown]); + device_type_str[RT_Device_Class_Unknown], + device->ref_count); } return 0;