opti: change scheduler to prioritize threads locking mutex

This commit is contained in:
Nemo D'ACREMONT 2025-04-08 18:59:41 +02:00
parent 2fa0c145f2
commit 8d023ed201
No known key found for this signature in database
GPG Key ID: 85F245EC3BB1E022

View File

@ -12,15 +12,16 @@
#define HAS_STATUS(entry, value) ((entry->status) & (value))
#define SET_STATUS(entry, value) ((entry->status) |= (value))
#define UNSET_STATUS(entry, value) ((entry->status) &= ~(value))
#define FINISHED 0x1
#define FINISHED (1 << 0)
#define IS_FINISHED(entry) (HAS_STATUS(entry, FINISHED))
#define ALLOCATED 0x2
#define ALLOCATED (1 << 1)
#define WAS_ALLOCATED(entry) (HAS_STATUS(entry, ALLOCATED))
#define WAITING 0x4
#define WAITING (1 << 2)
#define IS_WAITING(entry) (HAS_STATUS(entry, WAITING))
#define GET_LAST_WAITED_THREAD(entry) (entry->last_waited ? entry->last_waited->last_thread : NULL)
#define WAITED 0x8
#define MUTEX_WAITING (char) 0x10
#define WAITED (1 << 3)
#define MUTEX_WAITING (1 << 4)
#define MUTEX_LOCKING (1 << 5)
#define IS_WAITED(entry) (HAS_STATUS(entry, WAITED))
#define IS_MUTEX_WAITING(entry) (HAS_STATUS(entry, MUTEX_WAITING))
@ -51,7 +52,7 @@ struct context_entry_t {
struct last_thread_t *last_waited;
struct mutex_fifo_entry_t mutex_fifo_entry;
int valgrind_id;
char status;
int status;
char stack[STACK_SIZE];
};
@ -84,6 +85,11 @@ int thread_yield(void)
return 0;
}
if (HAS_STATUS(running, MUTEX_LOCKING)) {
DBG("skip yield : running thread is locking a mutex");
return 0;
}
#ifdef FIBO_STRAT
if (!(IS_YIELD(running)) && !IS_FINISHED(running) && !IS_WAITING(running)) {
SET_STATUS(running, YIELD);
@ -423,6 +429,7 @@ int thread_mutex_lock(thread_mutex_t* mutex)
running->mutex_fifo_entry.thread = running;
thread_yield();
}
SET_STATUS(running, MUTEX_LOCKING);
mutex->dummy = 1;
return 0;
@ -440,6 +447,10 @@ int thread_mutex_unlock(thread_mutex_t* mutex)
TAILQ_INSERT_TAIL(&scheduler_fifo, first->thread, link);
}
return mutex->dummy = 0;
mutex->dummy = 0;
UNSET_STATUS(running, MUTEX_LOCKING);
thread_yield();
return 0;
}
#endif