Commit Graph

26 Commits

Author SHA1 Message Date
Andreas Kling
50677bf806 Kernel: Refactor scheduler to use dynamic thread priorities
Threads now have numeric priorities with a base priority in the 1-99
range.

Whenever a runnable thread is *not* scheduled, its effective priority
is incremented by 1. This is tracked in Thread::m_extra_priority.
The effective priority of a thread is m_priority + m_extra_priority.

When a runnable thread *is* scheduled, its m_extra_priority is reset to
zero and the effective priority returns to base.

This means that lower-priority threads will always eventually get
scheduled to run, once its effective priority becomes high enough to
exceed the base priority of threads "above" it.

The previous values for ThreadPriority (Low, Normal and High) are now
replaced as follows:

    Low -> 10
    Normal -> 30
    High -> 50

In other words, it will take 20 ticks for a "Low" priority thread to
get to "Normal" effective priority, and another 20 to reach "High".

This is not perfect, and I've used some quite naive data structures,
but I think the mechanism will allow us to build various new and
interesting optimizations, and we can figure out better data structures
later on. :^)
2019-12-30 18:46:17 +01:00
Valtteri Koskivuori
2b9c9bf663 LibPthread: Log debug info to debug console instead of stdout (#931) 2019-12-27 15:53:02 +01:00
Andreas Kling
4a8683ea68 Kernel+LibPthread+LibC: Add a naive futex and use it for pthread_cond_t
This patch implements a simple version of the futex (fast userspace
mutex) API in the kernel and uses it to make the pthread_cond_t API's
block instead of busily sched_yield().

An arbitrary userspace address is passed to the kernel as a "token"
that identifies the futex and you can then FUTEX_WAIT and FUTEX_WAKE
that specific userspace address.

FUTEX_WAIT corresponds to pthread_cond_wait() and FUTEX_WAKE is used
for pthread_cond_signal() and pthread_cond_broadcast().

I'm pretty sure I'm missing something in this implementation, but it's
hopefully okay for a start. :^)
2019-12-25 23:54:06 +01:00
Andreas Kling
a12c2df43f LibPthread: Okay I'm dumb, let's convert mutex locks into Atomic<u32>& 2019-12-22 20:25:09 +01:00
Andreas Kling
9e3e13e410 LibPthread: Fix typo in pthread_mutex_lock()
We were casting the pthread_mutex_t* instead of pthread_mutex_t::lock
to an Atomic<u32>. This still worked fine, since "lock" is the first
member of pthread_mutex_t.
2019-12-22 17:25:55 +01:00
Andreas Kling
0c97380ee6 LibPthread+LibC: Support PTHREAD_MUTEX_RECURSIVE
This allows SDL to build against our native recursive mutex instead
of providing its own. Also it's just a nice feature to have. :^)
2019-12-22 14:25:41 +01:00
joshua stein
ac25438d54 Build: clean up build system, use one shared Makefile
Allow everything to be built from the top level directory with just
'make', cleaned with 'make clean', and installed with 'make
install'.  Also support these in any particular subdirectory.

Specifying 'make VERBOSE=1' will print each ld/g++/etc. command as
it runs.

Kernel and early host tools (IPCCompiler, etc.) are built as
object.host.o so that they don't conflict with other things built
with the cross-compiler.
2019-12-20 20:20:54 +01:00
Andrew Kaster
baf7e247e3 LibPThread: Add pthread_set/getname_np
These wrappers call the set_thread_name and get_thread_name syscalls
respectively.
2019-12-08 14:09:29 +01:00
Andreas Kling
1670ee5aba LibPthread: Condition variables should use CLOCK_MONOTONIC by default
It's the only clock we have at the moment, so it's a logical choice :^)
2019-12-07 16:07:48 +01:00
Andreas Kling
e7dfd40dc3 LibPthread: Mark the pthread_cond_t "waiting" flag as volatile
Oops, this is not gonna work if the compiler can optimize out all the
reads from this flag. :^)
2019-12-07 15:34:00 +01:00
Andreas Kling
96e8c8a4e5 LibPthread: Add stubs for pthread_{get,set}schedparam()
These should be the last thing needed to make SDL build with threads
support. I think we can survive just fine with stubs of these for now,
especially given that the kernel doesn't care super much about thread
priorities anyway.
2019-12-07 15:32:48 +01:00
Andreas Kling
615553be5f LibPthread: Implement simple thread-specific keys
This patch adds pthread_key_create() and pthread_{get,set}specific().
There's a maximum of 64 thread-specific keys for simplicity.

Key destructors are not invoked on thread exit.
2019-12-07 15:21:18 +01:00
Andreas Kling
2b45b7a45c LibPthread: Implement pthread_sigmask() 2019-12-07 14:52:27 +01:00
Andreas Kling
95b086f47f Kernel+LibPthread: Implement pthread_detach() 2019-12-07 14:52:27 +01:00
Andreas Kling
9ddfe694f2 LibPthread: Implement pthread_mutexattr_init() and _destroy() 2019-12-07 14:52:27 +01:00
Andreas Kling
eaab7f5672 LibPthread: Don't set errno in pthread functions
POSIX says that pthread API's don't set errno directly. If there is an
error, it should be the return value from the function instead.
2019-12-07 14:52:27 +01:00
Andreas Kling
babb726212 LibPthread: Implement pthread_mutex_trylock() 2019-12-07 14:52:27 +01:00
Andreas Kling
594c7f2d83 LibPthread: Implement pthread_self() 2019-12-07 14:52:27 +01:00
Andreas Kling
d08061103d LibPthread: Implement pthread_mutex_destroy() 2019-12-07 14:52:27 +01:00
Andreas Kling
cc1ef6dadb LibPthread: Implement condition variables
This feels like a pretty naive implementation, but I think it can work.
Basically each waiter creates an object on its stack that is then
added to a linked list inside by the pthread_cond_t.

Signalling is then done by walking the list and unsetting the "waiting"
flag on as many of the waiters as you like.
2019-12-07 14:52:27 +01:00
Andreas Kling
cada332e95 LibPthread: Remove some duplicate declarations in pthread.h 2019-12-01 11:52:52 +01:00
Andrew Kaster
618aebdd8a Kernel+LibPthread: pthread_create handles pthread_attr_t
Add an initial implementation of pthread attributes for:
  * detach state (joinable, detached)
  * schedule params (just priority)
  * guard page size (as skeleton) (requires kernel support maybe?)
  * stack size and user-provided stack location (4 or 8 MB only, must be aligned)

Add some tests too, to the thread test program.

Also, LibC: Move pthread declarations to sys/types.h, where they belong.
2019-11-18 09:04:32 +01:00
Andreas Kling
e34ed04d1e Kernel+LibPthread+LibC: Create secondary thread stacks in userspace
Have pthread_create() allocate a stack and passing it to the kernel
instead of this work happening in the kernel. The more of this we can
do in userspace, the better.

This patch also unexposes the raw create_thread() and exit_thread()
syscalls since they are now only used by LibPthread anyway.
2019-11-17 17:29:20 +01:00
Andreas Kling
66a2b582c3 LibPthread: Implement a basic first pthread mutex
This patch adds these API's:

- pthread_mutex_init()
- pthread_mutex_lock()
- pthread_mutex_unlock()

No mutex attributes are supported yet, so we only do the simplest mutex
wihout recursive locking.
2019-11-16 12:23:36 +01:00
Andreas Kling
69efa3f630 Kernel+LibPthread: Implement pthread_join()
It's now possible to block until another thread in the same process has
exited. We can also retrieve its exit value, which is whatever value it
passed to pthread_exit(). :^)
2019-11-14 20:58:23 +01:00
Andreas Kling
69ca9cfd78 LibPthread: Start working on a POSIX threading library
This patch adds pthread_create() and pthread_exit(), which currently
simply wrap our existing create_thread() and exit_thread() syscalls.

LibThread is also ported to using LibPthread.
2019-11-13 21:49:24 +01:00