将堆栈指针移动到 C 中线程堆栈内存的顶部(在 xv6 中测试)

问题描述 投票:0回答:1
  1. 我可以通过以下代码更改堆栈指针的值吗?
  2. 如果不是这样,如何更改?
  3. 我们为什么需要这样做?我想当我通过
    main()
    调用函数时。这意味着我实际上正在移动 sp,不是吗?

(ps.env是jmp_buf的一种类型)

类似这样的:

if (setjmp(current_thread->env)==0){
     longjmp(current_thread->env,*((int*)current_thread->stack_p));
}

结构线程:

struct thread {
    void (*fp)(void *arg);
    void *arg;
    void *stack;
    void *stack_p;
    jmp_buf env; // for thread function
    int buf_set; // 1: indicate jmp_buf (env) has been set, 0: indicate jmp_buf (env) not set
    int ID;
    struct thread *previous;
    struct thread *next;
};
c multithreading operating-system xv6
1个回答
0
投票

看看 swtch.S

extern void thread_switch(uint64 old, uint64 new);
thread_switch((uint64) &current_thread->previous->ctx, (uint64) &current_thread->ctx);
© www.soinside.com 2019 - 2024. All rights reserved.