py: Release GIL during syscalls in reader and writer code.
This releases the GIL during POSIX system calls that could block.
This commit is contained in:
parent
35f66d38b8
commit
62537a18e3
@ -34,6 +34,7 @@
|
||||
#include "py/persistentcode.h"
|
||||
#include "py/bc0.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/mpthread.h"
|
||||
|
||||
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
|
||||
|
||||
@ -821,15 +822,21 @@ void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
|
||||
|
||||
STATIC void fd_print_strn(void *env, const char *str, size_t len) {
|
||||
int fd = (intptr_t)env;
|
||||
MP_THREAD_GIL_EXIT();
|
||||
ssize_t ret = write(fd, str, len);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) {
|
||||
MP_THREAD_GIL_EXIT();
|
||||
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
mp_print_t fd_print = {(void*)(intptr_t)fd, fd_print_strn};
|
||||
mp_raw_code_save(rc, &fd_print);
|
||||
MP_THREAD_GIL_EXIT();
|
||||
close(fd);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
}
|
||||
|
||||
#else
|
||||
|
10
py/reader.c
10
py/reader.c
@ -29,6 +29,7 @@
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/mpthread.h"
|
||||
#include "py/reader.h"
|
||||
|
||||
typedef struct _mp_reader_mem_t {
|
||||
@ -86,7 +87,9 @@ STATIC mp_uint_t mp_reader_posix_readbyte(void *data) {
|
||||
if (reader->len == 0) {
|
||||
return MP_READER_EOF;
|
||||
} else {
|
||||
MP_THREAD_GIL_EXIT();
|
||||
int n = read(reader->fd, reader->buf, sizeof(reader->buf));
|
||||
MP_THREAD_GIL_ENTER();
|
||||
if (n <= 0) {
|
||||
reader->len = 0;
|
||||
return MP_READER_EOF;
|
||||
@ -101,7 +104,9 @@ STATIC mp_uint_t mp_reader_posix_readbyte(void *data) {
|
||||
STATIC void mp_reader_posix_close(void *data) {
|
||||
mp_reader_posix_t *reader = (mp_reader_posix_t*)data;
|
||||
if (reader->close_fd) {
|
||||
MP_THREAD_GIL_EXIT();
|
||||
close(reader->fd);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
}
|
||||
m_del_obj(mp_reader_posix_t, reader);
|
||||
}
|
||||
@ -110,13 +115,16 @@ void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) {
|
||||
mp_reader_posix_t *rp = m_new_obj(mp_reader_posix_t);
|
||||
rp->close_fd = close_fd;
|
||||
rp->fd = fd;
|
||||
MP_THREAD_GIL_EXIT();
|
||||
int n = read(rp->fd, rp->buf, sizeof(rp->buf));
|
||||
if (n == -1) {
|
||||
if (close_fd) {
|
||||
close(fd);
|
||||
}
|
||||
MP_THREAD_GIL_ENTER();
|
||||
mp_raise_OSError(errno);
|
||||
}
|
||||
MP_THREAD_GIL_ENTER();
|
||||
rp->len = n;
|
||||
rp->pos = 0;
|
||||
reader->data = rp;
|
||||
@ -127,7 +135,9 @@ void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) {
|
||||
#if !MICROPY_VFS_POSIX
|
||||
// If MICROPY_VFS_POSIX is defined then this function is provided by the VFS layer
|
||||
void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
|
||||
MP_THREAD_GIL_EXIT();
|
||||
int fd = open(filename, O_RDONLY, 0644);
|
||||
MP_THREAD_GIL_ENTER();
|
||||
if (fd < 0) {
|
||||
mp_raise_OSError(errno);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user