Search code examples
cpointerscastingvoid-pointersvxworks

Why does TaskSpawn take ints instead of void*?


For the vxWorks operating system, they provide a system library for spawning tasks:

int taskSpawn
    (
    char *  name,             /* name of new task (stored at pStackBase) */
    int     priority,         /* priority of new task */
    int     options,          /* task option word */
    int     stackSize,        /* size (bytes) of stack needed plus name */
    FUNCPTR entryPt,          /* entry point of new task */
    int     arg1,             /* 1st of 10 req'd task args to pass to func */
    int     arg2,
    int     arg3,
    int     arg4,
    int     arg5,
    int     arg6,
    int     arg7,
    int     arg8,
    int     arg9,
    int     arg10
    )

You can see how limited this is compared to something like std::thread.
Normally, I could pass in the appropriate arguments based on the entry function.


Given that this is a C function, I was curious if there was a reason that vxWorks choose to have arguments 1-10 be ints instead of void pointers.

If they were void pointers, I would be able to pass in any type, and then cast it back inside the entry function.

If I wanted to pass a pointer of struct foo into the entry function given to taskSpawn, could it be converted to an integer, and then back to the appropriate pointer?
Based on the design of this function, am I forced to use globals?


Solution

  • I think you will find that on VxWorks an integer is the same size as a pointer, and the common practice is to cast between them. So if you want to pass a pointer via TaskSpawn, you can pass it as (int)ptr.

    No, this isn't portable C, but it is apparently normal practice on this platform with this function.