Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass in function's argument a pointer function using wrapper function?

I have this function:

void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args)
{

 for(int i=0; i<MAX_PROC;i++) {
    PT[i].ppid = NOPROC;
 }
 nextproc = 0;

 curproc = NOPROC;
 Exec(boot_task, argl, args);
}

and and I want instead of using Exec() to use pthread, so I have to call the cpu_boot:

void cpu_boot(uint cores, interrupt_handler bootfunc, uint serialno)
{
 //I cannot change this function 
}

These are the types of the arguments

typedef void interrupt_handler();
typedef int (* Task)(int, void*);

I've tried:

void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args)
{
    void my_wrapper()
    {

        int y;
        y= boot_task(argl, args);
    }

    cpu_boot(ncores, my_wrapper , nterm);
}

But this is wrong. How can I implement this?

like image 303
Arman Krikorian Avatar asked Oct 20 '22 20:10

Arman Krikorian


1 Answers

You'll want something like this:

void some_interrupt_handler(){
    /* code here */
    return;
}

interrupt_handler* my_wrapper(Task boot_task, int argl, void* args)
{
    /* 
      this is where you run boot task
    */
    boot_task(argl, args);

    /* then pick an interrupt_handler to return... */
    void (*function_ptr)() = some_interrupt_handler;

    return function_ptr;
}

Then you can use your wrapper like this:

void boot(uint ncores, uint nterm, Task boot_task, int argl, void* args)
{
    cpu_boot(ncores, my_wrapper(boot_task, argl, args) , nterm);
}
like image 170
d.j.yotta Avatar answered Nov 01 '22 12:11

d.j.yotta