Re: [PATCH 1/2] Make kthread_create and kthread_run typesafe

From: WANG Cong
Date: Fri Jan 18 2008 - 00:19:39 EST


On Fri, Jan 18, 2008 at 11:51:39AM +1100, Rusty Russell wrote:
>With a little macro ugliness, we can make kthread_create() and
>kthread_run() typesafe: avoid the casts to and from void *. To do
>this we use a temporary function pointer which takes the type of the
>data as a callback: if the function doesn't match, we get:
>
> warning: initialization from incompatible pointer type
>
>It's actually slightly over-strict, since a void * would be compatible
>with any function type, but there's only one such case in the kernel
>anyway.

{snip}

>+#define kthread_create(threadfn, data, namefmt...) ({ \
>+ int (*_threadfn)(typeof(data)) = (threadfn); \
>+ __kthread_create((void *)_threadfn, (data), namefmt); \
>+})
>+
>+struct task_struct *__kthread_create(int (*threadfn)(void *data),
>+ void *data,
>+ const char namefmt[], ...);

Rusty, excellent work!!

I really like this. It is a good trick, neat and clean.
Thanks.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/