Release GIL when calling C++ params functions (#21257)

* Release GIL when calling C++ params functions

* small cleanup
This commit is contained in:
Willem Melching 2021-06-15 12:36:01 +02:00 committed by GitHub
parent e28b5552e7
commit 2f759893ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 18 deletions

View File

@ -17,12 +17,12 @@ cdef extern from "selfdrive/common/params.h":
ALL
cdef cppclass Params:
Params(bool)
Params(string)
Params(bool) nogil
Params(string) nogil
string get(string, bool) nogil
bool getBool(string)
int remove(string)
int put(string, string)
int putBool(string, bool)
bool checkKey(string)
bool getBool(string) nogil
int remove(string) nogil
int put(string, string) nogil
int putBool(string, bool) nogil
bool checkKey(string) nogil
void clearAll(ParamKeyType)

View File

@ -31,10 +31,14 @@ cdef class Params:
cdef c_Params* p
def __cinit__(self, d=None, bool persistent_params=False):
cdef string path
if d is None:
self.p = new c_Params(persistent_params)
with nogil:
self.p = new c_Params(persistent_params)
else:
self.p = new c_Params(<string>d.encode())
path = <string>d.encode()
with nogil:
self.p = new c_Params(path)
def __dealloc__(self):
del self.p
@ -53,13 +57,12 @@ cdef class Params:
return key
def get(self, key, block=False, encoding=None):
def get(self, key, bool block=False, encoding=None):
cdef string k = self.check_key(key)
cdef bool b = block
cdef string val
with nogil:
val = self.p.get(k, b)
val = self.p.get(k, block)
if val == b"":
if block:
@ -76,7 +79,10 @@ cdef class Params:
def get_bool(self, key):
cdef string k = self.check_key(key)
return self.p.getBool(k)
cdef bool r
with nogil:
r = self.p.getBool(k)
return r
def put(self, key, dat):
"""
@ -86,16 +92,19 @@ cdef class Params:
in general try to avoid writing params as much as possible.
"""
cdef string k = self.check_key(key)
dat = ensure_bytes(dat)
self.p.put(k, dat)
cdef string dat_bytes = ensure_bytes(dat)
with nogil:
self.p.put(k, dat_bytes)
def put_bool(self, key, val):
def put_bool(self, key, bool val):
cdef string k = self.check_key(key)
self.p.putBool(k, val)
with nogil:
self.p.putBool(k, val)
def delete(self, key):
cdef string k = self.check_key(key)
self.p.remove(k)
with nogil:
self.p.remove(k)
def put_nonblocking(key, val, d=None):