How to do proper locking
Max Laier
max at love2party.net
Thu Aug 4 12:08:33 GMT 2005
On Thursday 04 August 2005 13:40, Hans Petter Selasky wrote:
> This is a copy and paste from the kernel sources:
>
> struct ucred *
> crhold(struct ucred *cr)
> {
> The problem is, what happens if the kernel switches thread right here, and
> then the other thread calls "crfree()" on this structure, that will
> "free()" memory pointed to by "cr". Then the first line of code below will
> access freed memory, and then we are going for a segment fault or worse.
>
> mtx_lock(cr->cr_mtxp);
> cr->cr_ref++;
> mtx_unlock(cr->cr_mtxp);
> return (cr);
> }
It seems that you are misunderstanding the concept of reference counting. The
idea is to have a reference as long as you are handing around the object. In
order to call crhold() you must already hold a reference to the credential.
This will prevent the credential structure to vanish and allow you to pass
the credential to another consumer that now can assume to hold a reference of
it's own.
If you can access your objects from a external list, you have to hold a global
lock that protects:
1) Sanity of the list
2) Free operations to the member objects
If you access the objects via the list/tree/whatsoever "most of the time" -
refcounting isn't for you.
--
/"\ Best regards, | mlaier at freebsd.org
\ / Max Laier | ICQ #67774661
X http://pf4freebsd.love2party.net/ | mlaier at EFnet
/ \ ASCII Ribbon Campaign | Against HTML Mail and News
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20050804/495d502a/attachment.bin
More information about the freebsd-hackers
mailing list