简体   繁体   中英

Function pointer typedef in c

I have some embedded OS functions that I need to simulate on a linux machine. The approach I've been instructed to take is to overload the embedded OS functions and wrap them around POSIX threads so the linux machine can handle the embedded OS functions during unit tests and whatnot.

The embedded OS function to create a new thread is: OSCreateTask(OStypeTFP functionPointer, OSTypeTcbP taskId, OStypePrio priority)

I need to convert that OStypeTFP type into the void function pointer that pthread_create expects: ( void * (*)(void *) is what the compiler tells me it's expecting)

I was hoping to create a typedef that I could use it like:

typedef void (*OStypeTFP)(void);

// Function to run task/thread in
void taskFunction(void) { while(1); }

// Overloaded Embedded OS function
void OSCreateTask(OStypeTFP tFP, OStypeTcbP tcbP, OStypePrio prio)
{
  pthread_attr_t threadAttrs;
  pthread_t thread;

  pthread_attr_init(&threadAttributes);
  pthread_create(&thread, &threadAttributes, &tFP, NULL);
}

// Creates a task that runs in taskFunction
OSCreateTask (taskFunction, id, prio);

but the compiler complains that functionPointer is of type void (**)(void) when pthread_create expects void * (*)(void *)

Do I need to change my typedef somehow, or do I need to do some typecasting? Both?

You need an adapter function:

typedef void (*OStypeTFP)(void);

// Function to run task/thread in
void taskFunction(void) { while(1); }

void *trampoline(void *arg) 
{
    OStypeTFP task = (OStypeTFP)arg;
    task();
    return NULL;
}    

// Overloaded Embedded OS function
void OSCreateTask(OStypeTFP tFP, OStypeTcbP tcbP, OStypePrio prio)
{
  pthread_attr_t threadAttrs;
  pthread_t thread;

  pthread_attr_init(&threadAttrs);
  pthread_create(&thread, &threadAttrs, trampoline, tFP);
}

// Creates a task that runs in taskFunction
OSCreateTask (taskFunction, id, prio);

Of course it is safe only if you system allows casts from void * to function pointer. But since we are at POSIX environment - it should be OK.

If I understand correctly, the signature of a thread procedure on the embedded OS is void thread_proc(void) . For POSIX threads, on the other hand, it is void *thread_proc(void *) .

You can't paper over this difference with casts and typedefs: you need to arrange for an appropriate return value. You need a shim function:

typedef void (*OStypeTFP)(void);
struct emu_OSCreateTask_thread_start_data
{
    OStypeTFP real_thread_proc;
    // possibly other stuff
};

void *emu_OSCreateTask_shim_thread_proc (void *xctx)
{
    struct emu_OSCreateTask_thread_start_data *ctx = xctx;

    ctx->real_thread_proc();
    return 0;
}

void OSCreateTask(OStypeTFP tFP, OStypeTcbP tcbP, OStypePrio prio)
{
    pthread_attr_t threadAttrs;
    pthread_t thread;
    struct emu_OSCreateTask_thread_start_data *ctx =
        malloc(sizeof(struct emu_OSCreateTask_thread_start_data));

    ctx->real_thread_proc = tFP;

    pthread_attr_init(&threadAttributes);
    pthread_create(&thread, &threadAttributes,
                   emu_OSCreateTask_shim_thread_proc, ctx);
}

Note: ctx is allocated on the heap, and leaked, because it needs to live until after emu_OSCreateTask_shim_thread_proc returns, which may be indefinitely later than when OSCreateTask returns. Without knowing more about the API you're trying to emulate, I can't tell you where you ought to be stashing it so that it can get freed when appropriate, but there's probably somewhere . Maybe in tcbP ?

Note 2: I use a context object instead of just stuffing the " real_thread_proc " in pthread_create 's context pointer (as in Sergio's answer) because I suspect you will wind up needing to do more stuff in the shim, and needing more data from the outer context to do it. (You're on a POSIX system, so it is safe to stuff function pointers into a void * .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM