Thread: I need help with finishing this Context Switch

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    1

    I need help with finishing this Context Switch

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef void (*Ptr)(int);
    
    int numThr = 0;
    
    long r0[10], r1[10], r2[10]; // struct with all registered named
    long *stack0, *stack1, *stack2;
    
    
    void save(long *rLocation) {
        asm(//"pushq %rdi\n"
            "mov %rax, (%rdi)\n"
            "mov %rbx, 8(%rdi)\n"
            "mov %rcx, 16(%rdi)\n"
            "mov %rdx, 24(%rdi)\n"
            "mov %rsi, 32(%rdi)\n"
            "mov %r8, 40(%rdi)\n"
            "mov %rbp, 48(%rdi)\n"); //...
    }
    
    
    void load(long *rLocation) {  // similar to the book
        asm(//"popq %rdi\n"
            "mov (%rdi), %rax\n"
            "mov 48(%rdi), %rbp\n"
            "mov 40(%rdi), %r8\n"
            "mov 32(%rdi), %rsi\n"
            "mov 24(%rdi), %rdx\n"
            "mov 16(%rdi), %rcx\n"
            "mov 8(%rdi), %rbx\n"
            /*"mov 16(%rdi),%rsp\n"*/);    //....
    }
    
    
    int currentThr;
    
    
    void setStack(long *s) {
        asm("mov %rdi,%rsp\n");
    }
    
    
    void setStackAndRun(long *s, Ptr ptr) {
        asm("mov %rdi,%rsp\n");
        ptr(currentThr);
    }
    
    
    void getStack(long **s) {
        asm("mov %rsp,(%rdi)");
    }
    
    
    void startThr(Ptr ptr) {
        numThr++;
        currentThr = numThr;
        if (numThr == 1) {
            stack1 = ((long *)malloc(sizeof(long) * 64000)) + 64000;
            save(r0);
            getStack(&stack0);
            load(r1);
            setStackAndRun(stack1, ptr);
        }
        else if (numThr == 2) {
            stack2 = ((long *)malloc(sizeof(long) * 64000)) + 64000;
            save(r0);
            getStack(&stack0);
            load(r2);
            setStackAndRun(stack2, ptr);
        }
        //ptr(numThreads);
    }
    
    
    void shareCPU(int thread) {
        if (numThr == 1) {
            save(r1); // Save out created thread registers
            load(r0); // Load the main thread registers
            setStack(stack0);
    
            // do "opposite" of what we did on lines 36-39
        }
        // Switch stacks back and swap registers
    }
    
    
    void main1(int whoami) {
        while (true) {
            cout << "Main 1 says Hello" << endl;
            shareCPU(whoami);
        }
    }
    
    
    void main2(int whoami) {
        while (true) {
            cout << "Main 2 says Hello" << endl;
            shareCPU(whoami);
        }
    }
    
    
    int main() {
        startThr(main1);
        startThr(main2);
        while (true) {
            shareCPU(0);
        };
        return 0;
    }
    Last edited by laserlight; 10-05-2020 at 03:44 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,092
    I have moved this from the C programming forum to the C++ programming forum since you appear to be programming in C++ even though the code could just have easily been written in C, and removed the extra font formatting you put in so that the forum code highlighting would work properly. I also added in indentation that could have been lost in translation (hopefully you did format your code properly originally).

    Anyway, the usual applies: you should explain what help is it that you need, e.g., by describing what the program is supposed to do, what error messages the compiler reported, or if it compiles, what was the test input, expected output, and actual output.

    You might also want to write comments that are helpful to the reader, e.g., "similar to the book" means nothing when you don't make reference to any book (and even if you did, there's no guarantee the reader has access to a copy of the book), and be wary of comments that make reference to "lines 36-39" because line numbers are bound to change as the code is modified.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    No context? Nothing? You may get better advice if you add some details about what specifically you need help with and what you are attempting to create.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,922
    When you use "asm" in a program you likely should mention which of the several hundred (or more) assemblies you are using.
    At least mention the CPU or MCU information!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Measure Time spent in thread context switch
    By n.a.s in forum Linux Programming
    Replies: 3
    Last Post: 03-23-2014, 01:33 PM
  2. Finishing Off My Program
    By Steveqb14 in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2014, 10:10 AM
  3. help finishing this program
    By j.ward91 in forum C Programming
    Replies: 16
    Last Post: 04-26-2012, 10:44 AM
  4. Context Switch benchmark for real time linux.
    By odysseas in forum C Programming
    Replies: 5
    Last Post: 05-26-2011, 11:45 AM
  5. Finishing up
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2002, 06:18 AM

Tags for this Thread