Re: [PATCH 1/5] lib: introduce call_once()

From: Andrew Morton
Date: Tue Mar 11 2008 - 13:35:59 EST


On Tue, 11 Mar 2008 21:27:30 +0900
"Akinobu Mita" <akinobu.mita@xxxxxxxxx> wrote:

> 2008/3/11, Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>:
> > On Tue, 11 Mar 2008 15:10:52 +1100 Nick Piggin <nickpiggin@xxxxxxxxxxxx> wrote:
> >
> > > > #define ONCE() \
> > > > ({ \
> > > > static long flag; \
> > > > \
> > > > return !test_and_set_bit(0, flag); \
> > > > })
> > > >
> > > > and then callers can do
> > > >
> > > > if (ONCE())
> > > > do_something();
> > > >
> > > > ?
>
> This cannot be used directly in the conversions of patch 2 - 5.
> Because the callers of call_once() in patch 2 - 5 need to wait for the
> complition of "init_routine". Some blocking mechanism is needed.

Of course it can be used in those places. Here's the idr.c conversion:

--- a/lib/idr.c~a
+++ a/lib/idr.c
@@ -585,14 +585,6 @@ static void idr_cache_ctor(struct kmem_c
memset(idr_layer, 0, sizeof(struct idr_layer));
}

-static int init_id_cache(void)
-{
- if (!idr_layer_cache)
- idr_layer_cache = kmem_cache_create("idr_layer_cache",
- sizeof(struct idr_layer), 0, 0, idr_cache_ctor);
- return 0;
-}
-
/**
* idr_init - initialize idr handle
* @idp: idr handle
@@ -602,7 +594,9 @@ static int init_id_cache(void)
*/
void idr_init(struct idr *idp)
{
- init_id_cache();
+ if (ONCE())
+ idr_layer_cache = kmem_cache_create("idr_layer_cache",
+ sizeof(struct idr_layer), 0, 0, idr_cache_ctor);
memset(idp, 0, sizeof(struct idr));
spin_lock_init(&idp->lock);
}


--
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/