* @brief Creates a mutex. * * This system call creates a new mutex with the specified `name` and initializes it * with the given `flag`. The mutex is used for synchronizing access to shared resources * between tasks. Mutexes are typically used to ensure that only one task can access * a critical section of code or a resource at a time. The `flag` parameter allows * for setting certain proper
| 1821 | * @see sys_mutex_delete(), sys_mutex_lock(), sys_mutex_unlock() |
| 1822 | */ |
| 1823 | rt_mutex_t sys_mutex_create(const char *name, rt_uint8_t flag) |
| 1824 | { |
| 1825 | int len = 0; |
| 1826 | char *kname = RT_NULL; |
| 1827 | rt_mutex_t mutex = RT_NULL; |
| 1828 | |
| 1829 | len = lwp_user_strlen(name); |
| 1830 | if (len <= 0) |
| 1831 | { |
| 1832 | return RT_NULL; |
| 1833 | } |
| 1834 | |
| 1835 | kname = (char *)kmem_get(len + 1); |
| 1836 | if (!kname) |
| 1837 | { |
| 1838 | return RT_NULL; |
| 1839 | } |
| 1840 | |
| 1841 | if (lwp_get_from_user(kname, (void *)name, len + 1) != (len + 1)) |
| 1842 | { |
| 1843 | kmem_put(kname); |
| 1844 | return RT_NULL; |
| 1845 | } |
| 1846 | |
| 1847 | mutex = rt_mutex_create(kname, flag); |
| 1848 | if(mutex == RT_NULL) |
| 1849 | return RT_NULL; |
| 1850 | |
| 1851 | if (lwp_user_object_add(lwp_self(), (rt_object_t)mutex) != 0) |
| 1852 | { |
| 1853 | rt_mutex_delete(mutex); |
| 1854 | mutex = RT_NULL; |
| 1855 | } |
| 1856 | |
| 1857 | kmem_put(kname); |
| 1858 | |
| 1859 | return mutex; |
| 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * @brief Deletes a mutex and releases associated resources. |
nothing calls this directly
no test coverage detected