Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alloc: properly allocate memory handler per thread #2099

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions src/jv_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,40 @@ static void memory_exhausted() {
pthread_key_t nomem_handler_key;
pthread_once_t mem_once = PTHREAD_ONCE_INIT;

static void tsd_fini(void) {
struct nomem_handler *nomem_handler;
nomem_handler = pthread_getspecific(nomem_handler_key);
if (nomem_handler) {
(void) pthread_setspecific(nomem_handler_key, NULL);
free(nomem_handler);
}
static void tsd_fini(void *data) {
pthread_setspecific(nomem_handler_key, NULL);
free(data);
}

static void tsd_init(void) {
if (pthread_key_create(&nomem_handler_key, NULL) != 0) {
if (pthread_key_create(&nomem_handler_key, tsd_fini) != 0) {
fprintf(stderr, "jq: error: cannot create thread specific key");
abort();
}
if (atexit(tsd_fini) != 0) {
fprintf(stderr, "jq: error: cannot set an exit handler");
abort();
}
struct nomem_handler *nomem_handler = calloc(1, sizeof(struct nomem_handler));
if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {
fprintf(stderr, "jq: error: cannot set thread specific data");
abort();
}
}

void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
static struct nomem_handler* ensure_mem_handler(void) {
pthread_once(&mem_once, tsd_init); // cannot fail
struct nomem_handler *nomem_handler;

nomem_handler = pthread_getspecific(nomem_handler_key);
if (nomem_handler == NULL) {
handler(data);
fprintf(stderr, "jq: error: cannot allocate memory\n");
abort();
nomem_handler = calloc(1, sizeof(struct nomem_handler));
if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {
fprintf(stderr, "jq: error: cannot set thread specific data");
abort();
}
}
return nomem_handler;
}

void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {
struct nomem_handler *nomem_handler = ensure_mem_handler();
nomem_handler->handler = handler;
nomem_handler->data = data;
}

static void memory_exhausted() {
struct nomem_handler *nomem_handler;

pthread_once(&mem_once, tsd_init);
nomem_handler = pthread_getspecific(nomem_handler_key);
struct nomem_handler *nomem_handler = ensure_mem_handler();
if (nomem_handler)
nomem_handler->handler(nomem_handler->data); // Maybe handler() will longjmp() to safety
// Or not
Expand Down