feat: add cache for last_thread_t structure

This commit is contained in:
Alessandre Laguierce 2025-04-02 08:35:57 +02:00
parent b25ae2cf69
commit 3475ce4e8f

View File

@ -51,6 +51,8 @@ struct context_entry {
}; };
struct last_thread_t { struct last_thread_t {
STAILQ_ENTRY(last_thread_t)
link;
struct context_entry * last_thread; struct context_entry * last_thread;
int ref; // number of reference to this struct (for free) int ref; // number of reference to this struct (for free)
}; };
@ -61,6 +63,7 @@ static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(hea
static struct context_entry* running = NULL; static struct context_entry* running = NULL;
static TAILQ_HEAD(freed_context_head, context_entry) context_to_freed = TAILQ_HEAD_INITIALIZER(context_to_freed); static TAILQ_HEAD(freed_context_head, context_entry) context_to_freed = TAILQ_HEAD_INITIALIZER(context_to_freed);
static STAILQ_HEAD(last_thread_head, last_thread_t) last_thread_freed = STAILQ_HEAD_INITIALIZER(last_thread_freed);
struct mutex_fifo_entry_t { struct mutex_fifo_entry_t {
@ -195,7 +198,12 @@ int thread_join(thread_t thread, void** retval)
running->last_waited = entry->last_waited; running->last_waited = entry->last_waited;
} }
else { // the thread we want to join is solo else { // the thread we want to join is solo
if (STAILQ_EMPTY(&last_thread_freed)) {
running->last_waited = malloc(sizeof(struct last_thread_t)); running->last_waited = malloc(sizeof(struct last_thread_t));
} else {
running->last_waited = STAILQ_FIRST(&last_thread_freed);
STAILQ_REMOVE_HEAD(&last_thread_freed, link);
}
running->last_waited->ref = 0 ; running->last_waited->ref = 0 ;
entry->last_waited = running->last_waited; entry->last_waited = running->last_waited;
running->last_waited->last_thread = entry; running->last_waited->last_thread = entry;
@ -231,7 +239,8 @@ int thread_join(thread_t thread, void** retval)
// Release the last waited thread if no one use it anymore // Release the last waited thread if no one use it anymore
DBG("Last waited ref : %d", running->last_waited->ref); DBG("Last waited ref : %d", running->last_waited->ref);
if (--running->last_waited->ref == 0) { if (--running->last_waited->ref == 0) {
free(running->last_waited); STAILQ_INSERT_TAIL(&last_thread_freed, running->last_waited, link);
// free(running->last_waited);
} }
running->last_waited = NULL; running->last_waited = NULL;
} }
@ -301,6 +310,13 @@ void clear_context(void)
free(last); free(last);
} }
struct last_thread_t* last_thread;
while (!STAILQ_EMPTY(&last_thread_freed)) {
last_thread = STAILQ_FIRST(&last_thread_freed);
STAILQ_REMOVE_HEAD(&last_thread_freed, link);
free(last_thread);
}
// Free all the fifo that might have been allocated // Free all the fifo that might have been allocated
for (int i = 0 ; i < HASHMAP_SIZE ; ++i) for (int i = 0 ; i < HASHMAP_SIZE ; ++i)
free(mutex_fifo_hashmap[i]); free(mutex_fifo_hashmap[i]);