Use usleep rather than nanosleep

This commit is contained in:
Kovid Goyal 2021-01-03 05:00:40 +05:30
parent b3ed4c3f40
commit 18679348b2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 33 additions and 4 deletions

View File

@ -20,7 +20,7 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#ifdef HAS_SENDFILE
#include <sys/sendfile.h>
#endif
@ -147,6 +147,11 @@ size_of_cache_file(DiskCache *self) {
return lseek(self->cache_file_fd, 0, SEEK_END);
}
size_t
disk_cache_size_on_disk(PyObject *self) {
off_t ans = size_of_cache_file((DiskCache*)self);
return MAX(0, ans);
}
typedef struct {
uint8_t hash_key[MAX_KEY_SIZE];
@ -561,7 +566,7 @@ read_from_disk_cache(PyObject *self_, const void *key, size_t key_sz, void*(allo
if (!data) { PyErr_NoMemory(); goto end; }
if (s->data) { memcpy(data, s->data, s->data_sz); }
else if (self->currently_writing.hash_key && self->currently_writing.hash_keylen == key_sz && memcmp(self->currently_writing.hash_key, key, key_sz) == 0) {
else if (self->currently_writing.data && self->currently_writing.hash_key && self->currently_writing.hash_keylen == key_sz && memcmp(self->currently_writing.hash_key, key, key_sz) == 0) {
memcpy(data, self->currently_writing.data, s->data_sz);
xor_data(s->encryption_key, sizeof(s->encryption_key), data, s->data_sz);
}
@ -591,8 +596,7 @@ disk_cache_wait_for_write(PyObject *self_, monotonic_t timeout) {
mutex(unlock);
if (!pending) return true;
wakeup_write_loop(self);
struct timespec a = { .tv_nsec = 50000000L}, b;
nanosleep(&a, &b);
usleep(100 * 1000);
}
return false;
}
@ -626,6 +630,12 @@ wait_for_write(PyObject *self, PyObject *args) {
Py_RETURN_FALSE;
}
static PyObject*
size_on_disk(PyObject *self, PyObject *args UNUSED) {
unsigned long long ans = disk_cache_size_on_disk(self);
return PyLong_FromUnsignedLongLong(ans);
}
static PyObject*
add(PyObject *self, PyObject *args) {
const char *key, *data;
@ -677,6 +687,7 @@ static PyMethodDef methods[] = {
{"remove", pyremove, METH_VARARGS, NULL},
{"get", get, METH_VARARGS, NULL},
{"wait_for_write", wait_for_write, METH_VARARGS, NULL},
{"size_on_disk", size_on_disk, METH_NOARGS, NULL},
{NULL} /* Sentinel */
};

View File

@ -13,6 +13,7 @@ bool add_to_disk_cache(PyObject *self, const void *key, size_t key_sz, const voi
bool remove_from_disk_cache(PyObject *self_, const void *key, size_t key_sz);
void* read_from_disk_cache(PyObject *self_, const void *key, size_t key_sz, void*(allocator)(void*, size_t), void*);
bool disk_cache_wait_for_write(PyObject *self, monotonic_t timeout);
size_t disk_cache_size_on_disk(PyObject *self);
static inline void* disk_cache_malloc_allocator(void *x, size_t sz) {
*((size_t*)x) = sz;

View File

@ -172,11 +172,28 @@ class TestGraphics(BaseTest):
data[key] = key_as_bytes(val)
dc.add(bkey, data[key])
def remove(key):
bkey = key_as_bytes(key)
data.pop(key, None)
dc.remove(bkey)
def check_data():
for key, val in data.items():
self.ae(dc.get(key_as_bytes(key)), val)
for i in range(25):
self.assertIsNone(add(i, f'{i}' * i))
self.assertEqual(dc.total_size, sum(map(len, data.values())))
self.assertTrue(dc.wait_for_write())
check_data()
sz = dc.size_on_disk()
self.assertEqual(sz, sum(map(len, data.values())))
for x in (2, 4, 6, 8):
remove(x)
check_data()
self.assertRaises(KeyError, dc.get, key_as_bytes(x))
self.assertEqual(sz, dc.size_on_disk())
def test_load_images(self):
s, g, l, sl = load_helpers(self)