Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making thread_local fiber_local #618

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

lihuiba
Copy link
Collaborator

@lihuiba lihuiba commented Nov 6, 2024

No description provided.

ATTR must be non-NULL and point to a valid pthread_attr.
PDP must be non-NULL. */
-static int
+int
Copy link
Collaborator Author

@lihuiba lihuiba Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making the internal function allocate_stack() in lib pthread public and accessible by, say, fiber_create().

@@ -534,14 +540,7 @@ namespace photon
vcpu = current->get_vcpu();
(plock = &vcpu->runq_lock) -> foreground_lock();
}
mutable bool update_current = false;
void set_current(thread* th) const {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no longer the need to set/switch the thread-local variable CURRENT as part of context switching, given that it has become a fiber-local varible.

#if defined(__x86_64__)
#if !defined(_WIN64)
asm(
DEF_ASM_FUNC(_photon_switch_context) // (void** rdi_to, void** rsi_from)
R"(
rdfsbase %rax
Copy link
Collaborator Author

@lihuiba lihuiba Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save and restore fs register as part of context switching. It points to thread control block (TCB) and thread-local storage (TLS).

X86_64 only, for now.

void _photon_thread_die(thread* th) {
static __attribute__((noreturn))
void _photon_thread_stub() {
register thread* th asm("rbp");
Copy link
Collaborator Author

@lihuiba lihuiba Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On entry rbp points to struct thread.

size_t pstacksize;
pthread_attr_t attr;
pthread_attr_init(&attr);
allocate_stack((struct pthread_attr*)&attr, &pd, (void**)&ptr, &pstacksize);
Copy link
Collaborator Author

@lihuiba lihuiba Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must make use of allocate_thread() in lib pthread to allocate the stack, TCB (that compatible to threading facilities) and TLS (for all modules, no matter loaded at startup time or run time).

+#if TLS_TCB_AT_TP
+ /* Reference to the TCB itself. */
+ pd->header.self = pd;
+
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are moved from pthread_create(), because fiber_create() doesn't have the definition of *pd, and can not do it outside.

jmp *%rsi
)"

DEF_ASM_FUNC(_photon_thread_stub)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_photon_thread_stub() becomes more complex and would better be implemented in C++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant