Commit Graph

62 Commits

Author SHA1 Message Date
Herman S.
6f18c98502 [Thread/Posix] Fix thread suspend deadlock
Replace std::mutex/condition_variable with sem_wait/sem_post for thread
suspension, as WaitSuspended is called from the SIGRTMIN signal handler
where pthread_mutex_lock is not async-signal-safe.
2026-03-10 23:55:27 +09:00
Herman S.
4f1394bbaa [Kernel/Threading] Fix some kernel threading issues
- broken atomic exchange on linux
- spinlock release memory buffer
- race in thread suspension
- removed misleading volatile qualifiers
2026-03-10 23:53:03 +09:00
Herman S.
ae1706d1dc [Threads] Ensure proper RTTI/vtable emission for PosixWaitHandle 2026-03-09 13:43:56 +09:00
Herman S.
174d2d4205 [Posix Threading] Robust mutexes 2026-03-09 13:43:56 +09:00
Herman S.
1ae82023ea [Posix Threads] Refactored condition variables from static to per-instance members and replaced deadlock-prone predicate-based WaitMultiple with polling using try_lock. 2026-03-09 13:43:56 +09:00
Herman S.
c9eba5daf8 [Threading] Signal handler / self-suspension improvements 2026-03-09 13:43:56 +09:00
Herman S.
81dd887e74 [Kernel] Thread suspend for posix to match windows semantics 2026-03-09 13:43:56 +09:00
Herman S.
32b8c341ee [Kernel] ReleaseSemaphore for Posix to match Windows semantics 2026-03-09 13:43:56 +09:00
Margen67
4cb783bf22 Header cleanup 2025-07-19 14:42:21 -07:00
Adrian
fd1abfe6aa [Lint/CI] Use LLVM v19 for Linux & Lint 2025-06-21 00:25:27 +02:00
Marco Rodolfi
753698ea20 [linux_platform] Implement a bunch of missing functions necessaries to make Xenia build correctly under Linux
With this part fixed and a function readapted from the original codebase to fix the different signature, we should have a compiling Linux build now.
2025-01-20 18:03:23 +01:00
Marco Rodolfi
f58fab1d2c [compiler] Misc changes to make this emulator properly compile under Linux with Clang 2025-01-19 21:37:29 +01:00
Marco Rodolfi
55bbb28a80 [threading] Linux fixes on setting an incorrect priority
This call was failing since SCHED_FIFO doesn't support negative priorities, but only positive ones, see third paragraph of scheduling policies: https://man7.org/linux/man-pages/man7/sched.7.html.

Additionally Linux do provice up to 99 levels, but I've limited myself to the required UNIX standard of 32, and split the priority levels evenly from that.

I've also added a couple of rows to debug additional issues in the future like this.
2025-01-17 09:56:43 +01:00
Gliniak
09be7e874a [All] Fixed multiple issues during build on Linux
- Added some fixes introduced by RodoMa92 in PR198

- Lack of AVX2 extension (should be done differently in the future)
- Disable deprecated-volatile warning
- Added missing override in posix EventInfo, ImGui notification class and XContent class
- Removed not used XAudio2.h include in XMP
- Fixed missing switch-case in XObject
- Added fugly template in native_list.h
- Fixed multiple smaller issues
2025-01-10 21:41:45 +01:00
Gliniak
b9061e6292 [LINT] Linted files + Added lint job to CI 2024-03-12 19:19:30 +01:00
chss95cs@gmail.com
ecf6bfbbdf Stub event query for linux, fix missing semicolon in linux SetEventBoostPriority 2022-10-09 12:30:18 -07:00
Joel Linn
4a36a7962c [Base] Remove unneeded delay scheduler 2022-04-26 13:56:11 -05:00
Joel Linn
15950eec37 [Base] Use chrono APIs for Timers 2022-04-26 13:56:11 -05:00
Joel Linn
9b4168cce9 [Base] Make HighResolutionTimer platform agnostic 2022-04-26 13:56:11 -05:00
Joel Linn
b72ab7b4a4 [Base] Refactor POSIX timers, fix user-after-free
Since timer_delete does not clean up already queued signals, signal info
data needs to be retained after timer deletion and object destruction in
order to circumvent use-after-free bugs.
2022-03-08 12:17:57 -06:00
Joel Linn
fb741db2fe [Base] Fix callback threads for POSIX timers 2022-03-08 12:17:57 -06:00
Joel Linn
e75e0eb39c [Base] Fix Semaphore::Create invalid parameters 2022-03-08 12:17:57 -06:00
Joel Linn
bb42829308 [Base] Fix WaitMultiple on POSIX
- Never use `cond_.notify_one()` because it may wake a thread that is
  unrelated to the signalled wait handle, resulting in a lost wake and
  possible deadlock. Wait conditions are to be checked by the threads
  themselves.
- Refactor and simplify `WaitMultiple`
2022-03-08 12:17:57 -06:00
Triang3l
701300e8e9 [Linux] Use sched_yield instead of the deprecated pthread_yield 2021-12-18 19:43:17 +03:00
Triang3l
7aeac37eb6 [Base/UI] Android globals initialization + WindowedAppContext parts 2021-09-13 23:09:28 +03:00
Joel Linn
0e019f96b4 [Base, Linux] Make thread exit non returning
- Use pthread_exit() instead of pthread_cancel() if current thread
- Modify tests to ensure Exit does not return
2021-06-02 22:28:43 -05:00
Joel Linn
28ca58c0e9 [Base] Fix HighResolutionTimer
- Test was failing on Linux 5.11 and GLIBC 2.33
- `timer_t(0)` is a valid handle, so a `valid_` flag was added to guard
  destruction
- Similar behaviour on Windows was fixed as well. The invalid values for
  `HANDLE` are API dependent.
2021-06-02 22:28:43 -05:00
Triang3l
63ff758049 [Threading] Android thread naming and other cleanup 2020-11-22 20:03:45 +03:00
Triang3l
a39266b71e [Threading] Android thread termination signal 2020-11-22 18:48:55 +03:00
Triang3l
0ebf073385 [Base] Affinity and yield to sched on Android 2020-11-22 16:56:04 +03:00
Sandy Carter
5fa59fc4a9 threading_posix: don't delete thread_local thread object
Disabling on exit thread delete as it causes an assert fail.
There isn't a leak here because current_thread_ is a thread_local static.
2020-11-21 22:30:43 -06:00
Sandy Carter
aa332dcc8e threading_posix: Increase stack size in test
16 KB is not enough for the linux thread to be spawned so bump up to 16MB
2020-11-21 22:30:43 -06:00
Sandy Carter
d7094fae52 [threading linux] Implement native_handle
Move wait implementation to not use native_handle.
Implement native_handle for each primitive using posix natives.
2020-11-15 11:12:05 -06:00
Sandy Carter
e945a13957 [threading linux] Implement suspend count
Add suspend count to thread implementation.
Increment suspend count on suspend and decrement on resume.
Wait on suspend count to be decremented to 0.
Return suspend count on suspend and on resume before incr/decr.
Fix naming of resume suspend count to make clear that suspend count is
before incr/decr.
Add test.
2020-11-15 11:12:05 -06:00
Sandy Carter
a503b6222f [threads linux] Free and signal suspended threads
Give other threads access to initially suspended threads by signalling
conditional variable before waiting for state to be changed again.
2020-11-15 11:12:05 -06:00
Sandy Carter
e11fa0372d [threading linux] Implement Signal and Wait
Add Signal abstract function to handles.
Test SignalAndWait.
2020-11-15 11:12:05 -06:00
Sandy Carter
e9e269622b [threading linux] Implement TLS
Implement TLSHandle with pthread_key_t.

Test Alloc, Free, Get and Set.
2020-11-15 11:12:05 -06:00
Sandy Carter
634f87f63b [threading linux] Implement Callback Queuing
Add thread local bool for alertable state.
Use real-time event interrupt to run callback.
Fix sleep duration from miliseconds (microseconds / 1000) to seconds in sleep
command.
Add note for future implementation.

Ignore real-time event 37 in .gdbinit which is used to signal callback.

Test AlertableSleep
Test Thread QueueUserCallback.
TODO: Test alerted wait result when using IO functions.
2020-11-15 11:12:05 -06:00
Sandy Carter
4397f25325 [threading linux] Implement suspendable pthreads
Use real-time event interrupt to communicate suspend in timely manner.
Use conditional_variable to implement suspend wait and resume trigger.

Ignore real-time event 36 in .gdbinit which is used to signal suspend.

Test suspending threads.
2020-11-15 11:12:05 -06:00
Sandy Carter
b2912e7891 [threading linux] Wait for thread start 2020-11-15 11:12:05 -06:00
Sandy Carter
b91203a0b5 [threading linux] Implement basic Thread function
Add Basic Tests on Threads
2020-11-15 11:12:05 -06:00
Sandy Carter
c2de074d5c [threading linux] Implement Timer
Test Manual Reset and Synchronization timers single threaded.
Test Cancelling timers.
Test WaitMultiple.
Ignore real-time event 35 in .gdbinit which is used to signal timer.

Callbacks don't seem to be called so testing them is difficult.
2020-11-15 11:12:05 -06:00
Sandy Carter
331bb0ea9a [threading linux] Implement Mutant
Keep track of recursive locks with owner and count of locks.
Only allow recursive locks from same thread and increment count.
Only allow first locks from when count is zero.

Test acquiring and releasing mutant on same and on different threads.
Test Release return values.
Test WaitAll and WaitAny.
2020-11-15 11:12:05 -06:00
Sandy Carter
5d0efedaf4 [threading linux] Implement Semaphore
Test acquiring and releasing semaphores on same and on different threads.
Test previous_count values.
Test WaitAll and WaitAny.

Add tests for invalid semaphore creation parameters but disactivated as
they do not pass on any platform. These should be enabled and the
implementations fixed to match documentation.
2020-11-15 11:12:05 -06:00
Sandy Carter
75d54e2fa2 [threading linux] Make PosixCondition base class
Add PosixConditionBase as base class for Waitables to use common
primitives mutex and conditional variable
Add abstract signaled() and post_execution() to use single WaitMultiple
implementation.
2020-11-15 11:12:05 -06:00
Sandy Carter
6e13a38cad [threading linux] Implement WaitMultiple
Make conditional_variable and mutex static and create generalisation of
Wait for vector of handles.
Use std::any for waitany and std::all for waitall
2020-11-15 11:12:05 -06:00
Sandy Carter
9d20adfa77 [threading linux] Implement Events
Remove file-descriptor specific wait implementation to PosixFdHandle class
which breaks on waits of non-fd handles.
Replace with PosixConditionHandle and extend to support auto reset and
initial values.
Simplify mutex and conditional variable use with stdlib versions which
wrap these primitives but provide better C++ interface.
Test Event and Reset
2020-11-15 11:12:05 -06:00
Sandy Carter
f9d708265f [threading linux] Fix events with closed handles
Linux: Remove copy and destroy call in make_unique invokation which closes
handles on all events.
Testing: Add Wait test for Events set and unset.
2020-11-15 11:12:05 -06:00
Sandy Carter
b5ea686475 [threading] Implement Posix HighResolutionTimer
Implement HighResolutionTimer for Posix by using native timers.
Callbacks are triggered with realtime interrupts if they are supported.
Create an enum to track user-defined interrupts as well as an initializer and
handler to register these interrupts per thread.
Add test cases for timers for both single and multiple.
Fix Sleep function to continue sleeping if interrupted by system.
Add local .gdbinit to ignore signal 34 which is used by high res timer
2020-11-15 11:12:05 -06:00
Sandy Carter
d8d8a7dbb8 [threading linux] Fix nanosleep using microseconds
Add Sleep Test for 50ms.
Fix Sleep under linux that was using microseconds as nanoseconds.
Factor timespec creation to template function using div/mod and nanoseconds
from duration cast.
2020-11-15 11:12:05 -06:00