summaryrefslogtreecommitdiff
path: root/extmod/vfs_posix_file.c
diff options
context:
space:
mode:
authorDavid Lechner <david@pybricks.com>2020-01-23 21:50:53 -0600
committerDamien George <damien.p.george@gmail.com>2020-01-26 23:26:14 +1100
commit35f66d38b8e153399b5d277b6e02ea1a98eccee1 (patch)
treed89007f171800b1823ac792e133b0beef5ea7273 /extmod/vfs_posix_file.c
parentfee7e5617f55b4778de74ee185fcc3950cdc7b6d (diff)
extmod/vfs_posix: Release GIL during system calls.
This releases the GIL during syscalls that could block.
Diffstat (limited to 'extmod/vfs_posix_file.c')
-rw-r--r--extmod/vfs_posix_file.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c
index 87c202e3b..c817e51b8 100644
--- a/extmod/vfs_posix_file.c
+++ b/extmod/vfs_posix_file.c
@@ -24,6 +24,7 @@
* THE SOFTWARE.
*/
+#include "py/mpthread.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "extmod/vfs_posix.h"
@@ -100,7 +101,9 @@ mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_
}
const char *fname = mp_obj_str_get_str(fid);
+ MP_THREAD_GIL_EXIT();
int fd = open(fname, mode_x | mode_rw, 0644);
+ MP_THREAD_GIL_ENTER();
if (fd == -1) {
mp_raise_OSError(errno);
}
@@ -135,7 +138,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vfs_posix_file___exit___obj, 4, 4, vf
STATIC mp_uint_t vfs_posix_file_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
check_fd_is_open(o);
+ MP_THREAD_GIL_EXIT();
mp_int_t r = read(o->fd, buf, size);
+ MP_THREAD_GIL_ENTER();
if (r == -1) {
*errcode = errno;
return MP_STREAM_ERROR;
@@ -159,7 +164,9 @@ STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t
MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
nlr_raise(obj);
}
+ MP_THREAD_GIL_EXIT();
r = write(o->fd, buf, size);
+ MP_THREAD_GIL_ENTER();
}
if (r == -1) {
*errcode = errno;
@@ -173,14 +180,19 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_
check_fd_is_open(o);
switch (request) {
case MP_STREAM_FLUSH:
- if (fsync(o->fd) < 0) {
+ MP_THREAD_GIL_EXIT();
+ int ret = fsync(o->fd);
+ MP_THREAD_GIL_ENTER();
+ if (ret == -1) {
*errcode = errno;
return MP_STREAM_ERROR;
}
return 0;
case MP_STREAM_SEEK: {
struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
+ MP_THREAD_GIL_EXIT();
off_t off = lseek(o->fd, s->offset, s->whence);
+ MP_THREAD_GIL_ENTER();
if (off == (off_t)-1) {
*errcode = errno;
return MP_STREAM_ERROR;
@@ -189,7 +201,9 @@ STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_
return 0;
}
case MP_STREAM_CLOSE:
+ MP_THREAD_GIL_EXIT();
close(o->fd);
+ MP_THREAD_GIL_ENTER();
#ifdef MICROPY_CPYTHON_COMPAT
o->fd = -1;
#endif