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.
This commit is contained in:
John McCall
2024-03-14 00:50:13 -04:00
parent 590643ce5e
commit b0cee67d04
2 changed files with 22 additions and 7 deletions

View File

@@ -115,6 +115,15 @@ public:
T get() { return value; }
void set(T newValue) { value = newValue; }
T swap(T newValue) {
// There's an implicit optimization here because we implicitly
// materialize the address of the thread-local once instead of
// separately for two calls to get and set.
auto curValue = get();
set(newValue);
return curValue;
}
};
#else
// A wrapper around a TLS key that is lazily initialized using swift::once.
@@ -168,6 +177,12 @@ public:
memcpy(&storedValue, &newValue, sizeof(T));
tls_set(key.getKey(), storedValue);
}
T swap(T newValue) {
auto curValue = get();
set(newValue);
return curValue;
}
};
#endif