Doubt on thread safety of lookaside lists

What makes this code thread safe???

__inline
PVOID
ExAllocateFromNPagedLookasideList(
IN PNPAGED_LOOKASIDE_LIST Lookaside
)
{

PVOID Entry;

Lookaside->L.TotalAllocates += 1;
Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead,
&Lookaside->Lock);
if (Entry == NULL) {
Lookaside->L.AllocateMisses += 1;
Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
Lookaside->L.Size,
Lookaside->L.Tag);
}

return Entry;
}

Vladimir,

These routines use interlocked instructions, so are inherently thread-safe. ‘Interlocked’ means the
modified data (.e.g. the list head) are guaranteed to be updated in an atomic fashion across all
processors in the system.

Tom Stonecypher
iStreamConsulting.com

“Chtchetkine, Vladimir” wrote in message news:xxxxx@ntdev…
> What makes this code thread safe???
>
> __inline
> PVOID
> ExAllocateFromNPagedLookasideList(
> IN PNPAGED_LOOKASIDE_LIST Lookaside
> )
> {
>
> PVOID Entry;
>
> Lookaside->L.TotalAllocates += 1;
> Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead,
> &Lookaside->Lock);
> if (Entry == NULL) {
> Lookaside->L.AllocateMisses += 1;
> Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
> Lookaside->L.Size,
> Lookaside->L.Tag);
> }
>
> return Entry;
> }
>

Yes, I’ve noticed that routine that pools block from the list has
“interlocked” keyword and takes
a fast mutex as a param. But what about “Lookaside->L.TotalAllocates += 1”?
Doesn’t seem to be
“safe” at all!

Vladimir

-----Original Message-----
From: Tom Stonecypher [mailto:xxxxx@Hotmail.com]
Sent: Friday, May 17, 2002 8:54 AM
To: NT Developers Interest List
Subject: [ntdev] Re: Doubt on thread safety of lookaside lists

Vladimir,

These routines use interlocked instructions, so are inherently thread-safe.
‘Interlocked’ means the
modified data (.e.g. the list head) are guaranteed to be updated in an
atomic fashion across all
processors in the system.

Tom Stonecypher
iStreamConsulting.com

“Chtchetkine, Vladimir” wrote in message
news:xxxxx@ntdev…
> What makes this code thread safe???
>
> __inline
> PVOID
> ExAllocateFromNPagedLookasideList(
> IN PNPAGED_LOOKASIDE_LIST Lookaside
> )
> {
>
> PVOID Entry;
>
> Lookaside->L.TotalAllocates += 1;
> Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead,
> &Lookaside->Lock);
> if (Entry == NULL) {
> Lookaside->L.AllocateMisses += 1;
> Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
> Lookaside->L.Size,
> Lookaside->L.Tag);
> }
>
> return Entry;
> }
>


You are currently subscribed to ntdev as: xxxxx@Starbase.com
To unsubscribe send a blank email to %%email.unsub%%

RE: [ntdev] Re: Doubt on thread safety of lookaside listsIIRC, this member is just used to determine how to size the list by the system and thus it doesn’t really matter if it isn’t strictly accurate. The list will expand dynamically anyway, so no harm no foul.


Bill McKenzie

“Chtchetkine, Vladimir” wrote in message news:xxxxx@ntdev…
Yes, I’ve noticed that routine that pools block from the list has “interlocked” keyword and takes
a fast mutex as a param. But what about “Lookaside->L.TotalAllocates += 1”? Doesn’t seem to be
“safe” at all!

Vladimir

-----Original Message-----
From: Tom Stonecypher [mailto:xxxxx@Hotmail.com]
Sent: Friday, May 17, 2002 8:54 AM
To: NT Developers Interest List
Subject: [ntdev] Re: Doubt on thread safety of lookaside lists

Vladimir,

These routines use interlocked instructions, so are inherently thread-safe. ‘Interlocked’ means the
modified data (.e.g. the list head) are guaranteed to be updated in an atomic fashion across all
processors in the system.

Tom Stonecypher
iStreamConsulting.com

“Chtchetkine, Vladimir” wrote in message news:xxxxx@ntdev…
> What makes this code thread safe???
>
> __inline
> PVOID
> ExAllocateFromNPagedLookasideList(
> IN PNPAGED_LOOKASIDE_LIST Lookaside
> )
> {
>
> PVOID Entry;
>
> Lookaside->L.TotalAllocates += 1;
> Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead,
> &Lookaside->Lock);
> if (Entry == NULL) {
> Lookaside->L.AllocateMisses += 1;
> Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
> Lookaside->L.Size,
> Lookaside->L.Tag);
> }
>
> return Entry;
> }
>


You are currently subscribed to ntdev as: xxxxx@Starbase.com
To unsubscribe send a blank email to %%email.unsub%%

Yes, you have a point, the counter update isn’t interlocked, so it is a race. The counter is a
performance counter. Surely the designer of this interface didn’t want to incur the significant
added expense of an interlocked update on the counter. The list data is the criticial piece of the
structure; some minor data loss in the performance data is probably acceptable for most situations.

Even if the counter were updated interlocked, the queue and all the associated counters wouldn’t be
consistent with each other. The only way to achieve this consistency would be to guard the entire
entity with a spinlock. But, performance (avoiding spinlocks) was the reason for the development of
SLists.

Tom Stonecypher
iStreamConsulting.com

“Chtchetkine, Vladimir” wrote in message news:xxxxx@ntdev…
> Yes, I’ve noticed that routine that pools block from the list has
> “interlocked” keyword and takes
> a fast mutex as a param. But what about “Lookaside->L.TotalAllocates += 1”?
> Doesn’t seem to be
> “safe” at all!
>
> Vladimir
>
> -----Original Message-----
> From: Tom Stonecypher [mailto:xxxxx@Hotmail.com]
> Sent: Friday, May 17, 2002 8:54 AM
> To: NT Developers Interest List
> Subject: [ntdev] Re: Doubt on thread safety of lookaside lists
>
>
> Vladimir,
>
> These routines use interlocked instructions, so are inherently thread-safe.
> ‘Interlocked’ means the
> modified data (.e.g. the list head) are guaranteed to be updated in an
> atomic fashion across all
> processors in the system.
>
> Tom Stonecypher
> iStreamConsulting.com
>
> “Chtchetkine, Vladimir” wrote in message
> news:xxxxx@ntdev…
> > What makes this code thread safe???
> >
> > __inline
> > PVOID
> > ExAllocateFromNPagedLookasideList(
> > IN PNPAGED_LOOKASIDE_LIST Lookaside
> > )
> > {
> >
> > PVOID Entry;
> >
> > Lookaside->L.TotalAllocates += 1;
> > Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead,
> > &Lookaside->Lock);
> > if (Entry == NULL) {
> > Lookaside->L.AllocateMisses += 1;
> > Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
> > Lookaside->L.Size,
> > Lookaside->L.Tag);
> > }
> >
> > return Entry;
> > }
> >
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@Starbase.com
> To unsubscribe send a blank email to %%email.unsub%%
>

Doubt on thread safety of lookaside listsLookasides are not thread safe, they require wrapping them with sync objects.

Max

----- Original Message -----
From: Chtchetkine, Vladimir
To: NT Developers Interest List
Sent: Friday, May 17, 2002 7:15 PM
Subject: [ntdev] Doubt on thread safety of lookaside lists

What makes this code thread safe???

__inline
PVOID
ExAllocateFromNPagedLookasideList(
IN PNPAGED_LOOKASIDE_LIST Lookaside
)
{

PVOID Entry;

Lookaside->L.TotalAllocates += 1;
Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead, &Lookaside->Lock);
if (Entry == NULL) {
Lookaside->L.AllocateMisses += 1;
Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
Lookaside->L.Size,
Lookaside->L.Tag);
}

return Entry;
}


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to %%email.unsub%%

> Lookasides are not thread safe,

any doc states that explicitly?

they require wrapping them with sync objects.

then why the ExInterlocked… functions are used?

Max

----- Original Message -----
From: Chtchetkine, Vladimir mailto:xxxxx
To: NT Developers Interest List mailto:xxxxx
Sent: Friday, May 17, 2002 7:15 PM
Subject: [ntdev] Doubt on thread safety of lookaside lists

What makes this code thread safe???

__inline
PVOID
ExAllocateFromNPagedLookasideList(
IN PNPAGED_LOOKASIDE_LIST Lookaside
)
{

PVOID Entry;

Lookaside->L.TotalAllocates += 1;
Entry = ExInterlockedPopEntrySList(&Lookaside->L.ListHead,
&Lookaside->Lock);
if (Entry == NULL) {
Lookaside->L.AllocateMisses += 1;
Entry = (Lookaside->L.Allocate)(Lookaside->L.Type,
Lookaside->L.Size,
Lookaside->L.Tag);
}

return Entry;
}


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@setengineering.com
To unsubscribe send a blank email to %%email.unsub%%</mailto:xxxxx></mailto:xxxxx>