Commit Graph

65 Commits

Author SHA1 Message Date
Alastair Houghton
c75781ae0e [Threading] Use tss_dtor_t and not just void(*)(void *).
Although the C11 spec says `tss_dtor_t` is `void(*)(void *)`, there
may be modifiers on the former typedef, so we should use it instead
of directly writing the type.

rdar://144220714
2025-02-05 10:12:58 +00:00
John McCall
b0cee67d04 Properly save and restore the current task in the runtime so that tasks
can be reentrantly executed.

I don't think doing this is *actually a good idea*, but corrupting the
runtime is an even worse idea, and the overhead here is very low.
2024-03-15 00:40:54 -04:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Alastair Houghton
f8f2751e1b [Threading] Add the correct scope for lazy_mutex_handle.
Because we're mentioning the type in a macro and it gets used elsewhere,
we need to be explicit that the type is inside threading_impl.

rdar://121122202
2024-01-17 17:19:08 +00:00
Evan Wilde
154e98706c Use a macro to initialize mutex
Copying a mutex isn't really a supported thing. Use a macro to
initialize the mutex directly rather than a function that returns the
mutex.
2024-01-10 12:44:30 -08:00
Alastair Houghton
e5ece81cc9 [Freestanding] Remove uses of stat() and dlsym().
We shouldn't be using stat() or dlsym() in the freestanding
runtime.

rdar://111214571
rdar://106555012
2023-06-23 17:05:59 +01:00
Alastair Houghton
f38b9a960d [Threading][TSan] Update comments.
Updated the comments for tsan::acquire and tsan::release to better
reflect what TSan is actually doing.

rdar://110665213
2023-06-22 12:28:06 +01:00
Alastair Houghton
18b359ffcf [Threading][TSan] Fix builds where TSan isn't supported.
On builds where TSan isn't supported, don't include any code in
ThreadSanitizer.cpp.

rdar://110665213
2023-06-20 09:12:29 +01:00
Alastair Houghton
3f0018df97 [Threading][TSan] Fix linkage issue.
We need to pick up the `_swift_tsan_xxx` symbols from libswiftCore in
most cases, but sometimes we're statically linked and in that case we
want to use a local copy.

rdar://1106655213
2023-06-19 17:24:35 +01:00
Alastair Houghton
8ed8a28478 [Threading][TSan] Move ThreadSanitizer.cpp into the main runtime.
On Darwin, `RTLD_NEXT` doesn't do what we need it to here, with the
result that if `libswiftCore`'s TSan initializer gets found first,
then `libswift_Concurrency` won't have its initializer called at all,
in spite of us using `RTLD_NEXT` to find the next definition.

Fix this by centralising the initializer in `libswiftCore` instead.

rdar://110665213
2023-06-19 15:33:36 +01:00
Alastair Houghton
ee5efff5b1 [Threading][TSan] Update after review comments.
* Use the longer name ThreadSanitizer rather than TSan for the new files.
* Don't implement `tsan::consume` at all for now.
* Do the `tsan::release` for `ulock_unlock()` at the head of the function,
  not at the tail.
* Add a comment to test/Sanitizers/tsan/once.swift to explain the test a
  little more clearly.

rdar://110665213
2023-06-19 15:32:50 +01:00
Alastair Houghton
41f46ec085 [Threading][TSan] Fix TSan errors from lazy init on Linux.
Move the TSan functionality from Concurrency into Threading.  Use it
in the Linux `ulock` implementation so that TSan knows about `ulock`
and will tolerate the newer `swift_once` implementation that uses it.

rdar://110665213
2023-06-19 15:32:50 +01:00
Kuba (Brecka) Mracek
c86aa48bb4 Explicitly #include <errno.h>, avoid depending on implicit inclusion by other headers (#66060) 2023-05-22 20:38:40 -07:00
Philippe Hausler
40c277b245 Observation and associated macros 2023-03-02 15:30:59 -08:00
Alastair Houghton
b068194195 Don't use tsd_private.h in compatibility libraries for ARM64_32.
Apparently this issue also affects ARM64_32.

rdar://105768086
2023-02-22 08:36:25 +00:00
Alastair Houghton
3d6f2036b3 [Threading][Win32] Fix a warning.
The `RTL_SRWLOCK_INIT` declaration got reformatted and doesn't exactly
match what's in `windows.h`, so we're getting warnings during builds.
Fix to match what it says in the system header.

rdar://105400572
2023-02-13 17:43:01 +00:00
Alastair Houghton
21ac6149e3 Don't use tsd_private.h in compatibility libraries for ARMv7k.
If we include this file, it generates inline assembly, which would be fine
except that when apps are being built with bitcode, that isn't allowed.

rdar://103274667
2023-01-19 16:48:42 +00:00
Alastair Houghton
d0f42cad11 [Threading][Win32] Fix the wait functions for Win32 to always over-wait.
Rather than bodging the test to make it more robust, fix the functions
in the Threading layer to behave the same as they do on other platforms,
i.e. to guarantee that they always wait *at least* the amount of time
you asked for.

rdar://100236038
2022-09-29 17:01:59 +01:00
Alastair Houghton
33e74cd1d8 [Threading] Rename chrono.h to match the namespace name.
rdar://100236038
2022-09-29 15:44:29 +01:00
Alastair Houghton
fcf3bfa4a6 [Threading] Always round up when converting std::chrono::durations.
We were inadvertently rounding down, which was fine almost 100% of the
time, but on Windows where the timer resolution is only 1ms, it made
the test _very slightly_ flaky because we were asking to wait for
9ms rather than 10ms, then checking we'd waited at least 10ms.

We should explicitly round up, for that reason.

rdar://100236038
2022-09-29 10:31:26 +01:00
Alastair Houghton
4495d63c12 [Threading] Add ConditionVariable support.
Swift Concurrency would like to be able to use condition variables.
Add support to the threading packages.

rdar://100236038
2022-09-27 16:21:13 +01:00
Alastair Houghton
141f00be64 [Threading] Support 32-bit platforms for C11 threads and pthreads.
We just need to change the type from `int64_t` to `intptr_t`.

rdar://95523756
2022-06-20 10:08:27 +01:00
Saleem Abdulrasool
1db88858b2 Threading: repair the 32-bit Windows builds
This repairs the Windows x86 SDK build after #59287.  Use `intptr_t`
rather than the explicitly sized integer as this is always guaranteed to
be the size of the pointer width (irrespective of LP64/LP32 or
LLP64/LLP32).  Since this impacts only the once predicate, this
shouldn't impact any assumptions around structure sizes.
2022-06-18 09:36:27 -07:00
Alastair Houghton
7404ca9e62 [Threading] Fix 32-bit Linux.
`swift::once` needs to be 32-bit on 32-bit Linux, which means we have to
revert to using a global lock.

rdar://94831034
2022-06-10 20:11:28 +01:00
Alastair Houghton
863ab00c24 [Threading] Close and re-open namespace around include.
We need an extra namespace close/open around the include in `Darwin.h`.

rdar://90776105
2022-06-10 10:32:13 +01:00
Yuta Saito
aa0265b03e [Threading] Use raw gettid syscall for older glibc compatibility
`gettid` wrapper was added in glibc 2.30, and Ubuntu 18.04 uses glibc
2.27, so use raw syscall instead of it.
2022-06-07 07:39:54 +01:00
Alastair Houghton
c401cc34fd [Threading] Use llvm::Optional<> rather than making a zero lower bound special.
While most systems aren't going to have their stack bottom at zero,
using llvm::Optional<> here is cleaner.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
6962758c8c [Threading] Add the ability to use any C++ callable in swift::once().
This allows the use of C++ lambdas and functors, in addition to plain
old functions.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
b5c8b79556 [Threading] Move stack bounds fetching into the threading library.
Fetching the current stack bounds is done using threading functions, so
should be in the threading library.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
eb4c81d60e [Threading] Don't use TLS keys as template arguments.
There's no guarantee that e.g. pthread_key_t is an integral type.  It could
be some kind of struct, or some other thing that isn't valid as a template
argument.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
c7c1c1be80 [Threading] Fix some problems with the C11 threading code.
These changes are needed to get things building with a C11 threads shim
header on macOS.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
0e9318cec5 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
f3a412dd53 [Threading] Remove thread_get_main().
Removing thread_get_main() means we don't need a static initializer on
Darwin, which means we can delete Darwin.cpp as well.  We can also delete
Nothreads.cpp while we're about it, because there's nothing in that file.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
d6cff94e7f [Threading][Windows] Try to avoid declaring _RTL_SRWLOCK
Declaring _RTL_SRWLOCK ourselves causes clashes with <windows.h>.
Rather than doing that, declare an equivalent struct, and some overloads.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
14a4bd45b6 [Concurrency][Threading] Remove use of platform thread functions.
The concurrency library can use the new threading library functions,
which avoids the problem of including <windows.h>.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
2e28ac4f01 [Threading][Windows] Don't include <windows.h>
We can't safely include <windows.h> because it defines a large number
of macros, some of which clash with things in the Swift source tree,
and others of which clash with things in the LLVM source tree.  Sadly
we *also* can't just include the Windows headers we need, because they
pull in some of the problematic macros.

In this instance, the best thing seems to be to grab the definitions
for the types and functions we are going to use and put them in their
own header file.  If we define them correctly, then #including
<windows.h> before or after this header won't have any adverse effects.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
a7e42d041a [Threading][Windows] Undefine Yield and ERROR.
`windows.h` defines a couple of very unhelpfully named macros.
Undefine them after including it.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
d76f80d638 [Threading][Windows] More header file fixes.
rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
9d5736e1e1 [Threading][Windows] Fix another Windows niggle.
Looks like I forgot to update the Win32.h implementation.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
4eca62a5ea [Threading][Windows] Fix missing close brace.
Apparently I missed a close brace from the end of Win32.h.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
6d1b6dbd1c [Threading] Fix the Linux build.
A few fixes specifically for the Linux build.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
e0d23ed6a8 [Threading] A couple of header file fixes.
Some declarations were missing from non-Darwin sources, and the Linux
header was slightly broken.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alastair Houghton
a6996333e8 [Threading] Auto-detect threading in the preprocessor.
Some things (LLDB's language plugin, for instance), include Swift headers
from outside of our build system, and may not know to define one of the
threading package defines.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alastair Houghton
f5bdb858e0 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen
4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alex Hoppen
6ad771351f Revert "Merge pull request #59220 from kateinoigakukun/katei/dont-use-gettid-wrapper"
This reverts commit 58081c80f6, reversing
changes made to 94c9ce6473.
2022-06-02 18:03:01 +02:00
Yuta Saito
ead6e3dca7 [Threading] Use raw gettid syscall for older glibc compatibility
`gettid` wrapper was added in glibc 2.30, and Ubuntu 18.04 uses glibc
2.27, so use raw syscall instead of it.
2022-06-02 06:25:46 +00:00
Alastair Houghton
43cc1f97ba [Threading] Use llvm::Optional<> rather than making a zero lower bound special.
While most systems aren't going to have their stack bottom at zero,
using llvm::Optional<> here is cleaner.

rdar://90776105
2022-05-24 17:46:33 +01:00
Alastair Houghton
901ceaef13 [Threading] Add the ability to use any C++ callable in swift::once().
This allows the use of C++ lambdas and functors, in addition to plain
old functions.

rdar://90776105
2022-05-24 14:57:42 +01:00
Alastair Houghton
fc4f941185 [Threading] Move stack bounds fetching into the threading library.
Fetching the current stack bounds is done using threading functions, so
should be in the threading library.

rdar://90776105
2022-05-24 14:57:42 +01:00