[haiku-commits] r34366 - in haiku/trunk: headers/private/interface src/kits/interface

Author: bonefish
Date: 2009-11-30 12:06:51 +0100 (Mon, 30 Nov 2009)
New Revision: 34366
Changeset: http://dev.haiku-os.org/changeset/34366/haiku

Modified:
   haiku/trunk/headers/private/interface/ToolTipManager.h
   haiku/trunk/src/kits/interface/ToolTip.cpp
   haiku/trunk/src/kits/interface/ToolTipManager.cpp
Log:
Made the BToolTipManager lock non-static. Create the singleton via
pthread_once().


Modified: haiku/trunk/headers/private/interface/ToolTipManager.h
===================================================================
--- haiku/trunk/headers/private/interface/ToolTipManager.h      2009-11-30 
10:48:36 UTC (rev 34365)
+++ haiku/trunk/headers/private/interface/ToolTipManager.h      2009-11-30 
11:06:51 UTC (rev 34366)
@@ -26,19 +26,22 @@
                        void                            SetHideDelay(bigtime_t 
time);
                        bigtime_t                       HideDelay() const;
 
-       static  bool                            Lock() { return sLock.Lock(); }
-       static  void                            Unlock() { sLock.Unlock(); }
+                       bool                            Lock()          { 
return fLock.Lock(); }
+                       void                            Unlock()        { 
fLock.Unlock(); }
 
 private:
                                                                
BToolTipManager();
        virtual                                         ~BToolTipManager();
 
+       static  void                            _InitSingleton();
+
+private:
+                       BLocker                         fLock;
                        BMessenger                      fWindow;
 
                        bigtime_t                       fShowDelay;
                        bigtime_t                       fHideDelay;
 
-       static  BLocker                         sLock;
        static  BToolTipManager*        sDefaultInstance;
 };
 

Modified: haiku/trunk/src/kits/interface/ToolTip.cpp
===================================================================
--- haiku/trunk/src/kits/interface/ToolTip.cpp  2009-11-30 10:48:36 UTC (rev 
34365)
+++ haiku/trunk/src/kits/interface/ToolTip.cpp  2009-11-30 11:06:51 UTC (rev 
34366)
@@ -110,10 +110,11 @@
        while (true) {
                lockedLooper = View()->LockLooper();
                if (!lockedLooper) {
-                       BToolTipManager::Lock();
+                       BToolTipManager* manager = BToolTipManager::Manager();
+                       manager->Lock();
 
                        if (View()->Window() != NULL) {
-                               BToolTipManager::Unlock();
+                               manager->Unlock();
                                continue;
                        }
                }
@@ -131,7 +132,7 @@
        if (fLockedLooper)
                View()->UnlockLooper();
        else
-               BToolTipManager::Unlock();
+               BToolTipManager::Manager()->Unlock();
 }
 
 

Modified: haiku/trunk/src/kits/interface/ToolTipManager.cpp
===================================================================
--- haiku/trunk/src/kits/interface/ToolTipManager.cpp   2009-11-30 10:48:36 UTC 
(rev 34365)
+++ haiku/trunk/src/kits/interface/ToolTipManager.cpp   2009-11-30 11:06:51 UTC 
(rev 34366)
@@ -8,6 +8,8 @@
 #include <ToolTipManager.h>
 #include <ToolTipWindow.h>
 
+#include <pthread.h>
+
 #include <Autolock.h>
 #include <LayoutBuilder.h>
 #include <MessageRunner.h>
@@ -17,7 +19,7 @@
 #include <ToolTip.h>
 
 
-BLocker BToolTipManager::sLock("tool tip manager");
+static pthread_once_t sManagerInitOnce = PTHREAD_ONCE_INIT;
 BToolTipManager* BToolTipManager::sDefaultInstance;
 
 static const uint32 kMsgHideToolTip = 'hide';
@@ -59,13 +61,14 @@
 
        virtual void DetachedFromWindow()
        {
-               BToolTipManager::Lock();
+               BToolTipManager* manager = BToolTipManager::Manager();
+               manager->Lock();
 
                RemoveChild(fToolTip->View());
                        // don't delete this one!
                fToolTip->DetachedFromWindow();
 
-               BToolTipManager::Unlock();
+               manager->Unlock();
        }
 
        virtual void MouseMoved(BPoint where, uint32 transit,
@@ -117,9 +120,10 @@
 {
        SetLayout(new BGroupLayout(B_VERTICAL));
 
-       BToolTipManager::Lock();
+       BToolTipManager* manager = BToolTipManager::Manager();
+       manager->Lock();
        AddChild(new ToolTipView(tip));
-       BToolTipManager::Unlock();
+       manager->Unlock();
 
        BSize size = ChildAt(0)->PreferredSize();
        ResizeTo(size.width, size.height);
@@ -194,15 +198,23 @@
 /*static*/ BToolTipManager*
 BToolTipManager::Manager()
 {
-       BAutolock _(sLock);
-
+       // Note: The check is not necessary; it's just faster than always 
calling
+       // pthread_once(). It requires reading/writing of pointers to be atomic
+       // on the architecture.
        if (sDefaultInstance == NULL)
-               sDefaultInstance = new BToolTipManager();
+               pthread_once(&sManagerInitOnce, &_InitSingleton);
 
        return sDefaultInstance;
 }
 
 
+/*static*/ void
+BToolTipManager::_InitSingleton()
+{
+       sDefaultInstance = new BToolTipManager();
+}
+
+
 void
 BToolTipManager::ShowTip(BToolTip* tip, BPoint point)
 {
@@ -282,6 +294,7 @@
 
 BToolTipManager::BToolTipManager()
        :
+       fLock("tool tip manager"),
        fShowDelay(750000),
        fHideDelay(50000)
 {


Other related posts:

  • » [haiku-commits] r34366 - in haiku/trunk: headers/private/interface src/kits/interface - ingo_weinhold