The critical section is usually  implemented like this:

#include <utils/threads.h>

mutable Mutex mLock;

foo()
{
Mutex::Autolock l(mLock);
// critical section here
}


Mathias


On May 20, 8:11 am, Dave Sparks <davidspa...@android.com> wrote:
> I inserted comments below. You need to make sure that the critical
> section cannot be entered by another thread at the same time.
> Otherwise a race condition can occur that result in two camera objects
> being created.
>
> Typically this is done by having a mutex associated with the singleton
> that you lock at the start of the critical section, and unlock at the
> end. Don't forget to unlock before the return inside the critical
> section or you will deadlock the third time you call this function.
>
> However, the mutex is not necessary if there is already a mutex around
> the call to createInstance() and there is no other code that accesses
> the singleton.
>
> sp<CameraHardwareInterface> CameraHal::createInstance()
> {
>     LOG_FUNCTION_NAME
>
>     // BEGIN CRITICAL SECTION
>     if (singleton != 0) {
>         sp<CameraHardwareInterface> hardware = singleton.promote();
>         if (hardware != 0) {
>             return hardware;
>         }
>     }
>
>     sp<CameraHardwareInterface> hardware(new CameraHal());
>
>     singleton = hardware;
>     // END CRITICAL SECTION
>
>     return hardware;
>
> }
>
> On May 19, 6:42 pm, xie <yili....@gmail.com> wrote:
>
> > thanks very much ,there must have a mutex to protect the singleton~~~
>
> > ye, it actually has 2 references : the singleton and the fuction return
> > value ,but the singeleton is a wp pointer . It just add a weak
> > reference , not a strong reference.Do you mean that the caller to
> > createInstance() will hold a strong reference when the return value goes
> > out of scope ?
>
> > 在 2009-05-19二的 08:00 -0700,Dave Sparks写道:
>
> > > Yes, it actually has 2 references upon exit: The singleton and the
> > > function return value. Presumably the caller to createInstance() also
> > > holds onto a reference when the return value goes out of scope.
>
> > > I hope there is a mutex protecting the singleton somewhere because
> > > this code has a race condition otherwise.
>
> > > On May 19, 5:15 am, Freepine <freep...@gmail.com> wrote:
> > > > Its reference count gets increased by copy constructor & operator = 
> > > > while
> > > > returned from createInstance().
>
> > > > On Tue, May 19, 2009 at 4:15 PM, xie <yili....@gmail.com> wrote:
>
> > > > > hi Dianne~~
>
> > > > > thanks for you answer~~ i have implmented the camera hal with v4l2,
> > > > > although i used the below source, i still have some confusion. I think
> > > > > the below source is a singleton mode, if the object have been freed we
> > > > > will creat a new object . when i creat a new object with this code :
>
> > > > > sp<CameraHardwareInterface> hardware(new CameraHal());
>
> > > > > i think the "hardware" is a temp variable, it will be deleted when the
> > > > > function return. And the sp is a smartpointer ,it will free the 
> > > > > object.
> > > > > But i am wrong in fact , the object have not been deleted. Can you 
> > > > > tell
> > > > > my where i am wrong ~~
>
> > > > > thanks very much
>
> > > > > sp<CameraHardwareInterface> CameraHal::createInstance()
> > > > > {
> > > > >    LOG_FUNCTION_NAME
>
> > > > >    if (singleton != 0) {
> > > > >        sp<CameraHardwareInterface> hardware = singleton.promote();
> > > > >        if (hardware != 0) {
> > > > >            return hardware;
> > > > >        }
> > > > >    }
>
> > > > >    sp<CameraHardwareInterface> hardware(new CameraHal());
>
> > > > >    singleton = hardware;
> > > > >    return hardware;
> > > > > }
>
> > > > > 在 2009-05-19二的 00:43 -0700,Dianne Hackborn写道:
> > > > > > sp == strong pointer, wp == weak pointer.
>
> > > > > > The object will remain around while there are strong pointers; it is
> > > > > > destroyed once the last one is released.  All you can do with a weak
> > > > > > pointer is comparison and attempting to promote to a strong pointer;
> > > > > > the latter will fail if there are no other strong pointers on the
> > > > > > object.
>
> > > > > > On Mon, May 18, 2009 at 11:56 PM, xie <yili....@gmail.com> wrote:
>
> > > > > >         Dear all :
>
> > > > > >         when i read the android source, i find that "<sp>" is not 
> > > > > > only
> > > > > >         a smart
> > > > > >         pointer,it manage a strong ref count and i also find that 
> > > > > > the
> > > > > >         <wp>
> > > > > >         manage a weak ref count .
>
> > > > > >         who can tell me how the two kinds of pointer works together,
> > > > > >         when will
> > > > > >         the object be freed?
>
> > > > > >         thanks a lot
>
> > > > > > --
> > > > > > Dianne Hackborn
> > > > > > Android framework engineer
> > > > > > hack...@android.com
>
> > > > > > Note: please don't send private questions to me, as I don't have 
> > > > > > time
> > > > > > to provide private support, and so won't reply to such e-mails.  All
> > > > > > such questions should be posted on public forums, where I and others
> > > > > > can see and answer them.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"android-framework" group.
To post to this group, send email to android-framework@googlegroups.com
To unsubscribe from this group, send email to 
android-framework+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/android-framework?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to