lib/os: fdtable: add locking to posix api
Added locking to posix read(), write(), close() for additional protection. In read() missing lock would create uneven calls to locking mechanism in sockets.c after k_condvar_wait(). That results in socket lock not ever being unlocked Signed-off-by: Daniel Nejezchleb <dnejezchleb@hwg.cz>
This commit is contained in:
parent
1f22a2bc3c
commit
b68bdf20cf
1 changed files with 22 additions and 2 deletions
|
@ -224,21 +224,37 @@ int z_alloc_fd(void *obj, const struct fd_op_vtable *vtable)
|
|||
|
||||
ssize_t read(int fd, void *buf, size_t sz)
|
||||
{
|
||||
ssize_t res;
|
||||
|
||||
if (_check_fd(fd) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fdtable[fd].vtable->read(fdtable[fd].obj, buf, sz);
|
||||
(void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER);
|
||||
|
||||
res = fdtable[fd].vtable->read(fdtable[fd].obj, buf, sz);
|
||||
|
||||
k_mutex_unlock(&fdtable[fd].lock);
|
||||
|
||||
return res;
|
||||
}
|
||||
FUNC_ALIAS(read, _read, ssize_t);
|
||||
|
||||
ssize_t write(int fd, const void *buf, size_t sz)
|
||||
{
|
||||
ssize_t res;
|
||||
|
||||
if (_check_fd(fd) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fdtable[fd].vtable->write(fdtable[fd].obj, buf, sz);
|
||||
(void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER);
|
||||
|
||||
res = fdtable[fd].vtable->write(fdtable[fd].obj, buf, sz);
|
||||
|
||||
k_mutex_unlock(&fdtable[fd].lock);
|
||||
|
||||
return res;
|
||||
}
|
||||
FUNC_ALIAS(write, _write, ssize_t);
|
||||
|
||||
|
@ -250,8 +266,12 @@ int close(int fd)
|
|||
return -1;
|
||||
}
|
||||
|
||||
(void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER);
|
||||
|
||||
res = fdtable[fd].vtable->close(fdtable[fd].obj);
|
||||
|
||||
k_mutex_unlock(&fdtable[fd].lock);
|
||||
|
||||
z_free_fd(fd);
|
||||
|
||||
return res;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue