libbe.so: Avoid static BLockers.

They don't work properly after a fork, and if exit() is called
from the forked child, the parent won't be able to use them
anymore.

In BBitmap and BPicture, replace them with mutexes. For AppServerLink,
make the private ServerLink inherit from BLocker, and then use
that one directly.

This also is a slight efficiency gain, as we don't need to create
multiple semaphores from the static initializers now, which is
just a waste of time in the case of applications that don't
even need these locks but link to libbe.so (e.g. CLI tools.)

Part of #18576.
This commit is contained in:
Augustin Cavalier
2026-02-26 17:15:53 -05:00
parent b43289d544
commit 9cada02ee0
6 changed files with 20 additions and 31 deletions
+4 -1
View File
@@ -12,13 +12,16 @@
#include <OS.h> #include <OS.h>
#include <Locker.h>
#include <LinkReceiver.h> #include <LinkReceiver.h>
#include <LinkSender.h> #include <LinkSender.h>
class BShape; class BShape;
class BString; class BString;
class BGradient; class BGradient;
/* /*
* Error checking rules: (for if you don't want to check every return code) * Error checking rules: (for if you don't want to check every return code)
* - Calling EndMessage() is optional, implied by Flush() or StartMessage(). * - Calling EndMessage() is optional, implied by Flush() or StartMessage().
@@ -31,7 +34,7 @@ class BGradient;
namespace BPrivate { namespace BPrivate {
class ServerLink { class ServerLink : public BLocker {
public: public:
ServerLink(); ServerLink();
virtual ~ServerLink(); virtual ~ServerLink();
+4 -9
View File
@@ -9,7 +9,6 @@
#include <Application.h> #include <Application.h>
#include <Locker.h>
#include <ApplicationPrivate.h> #include <ApplicationPrivate.h>
#include <AppServerLink.h> #include <AppServerLink.h>
@@ -23,17 +22,13 @@
*/ */
static BLocker sLock("AppServerLink_sLock");
namespace BPrivate { namespace BPrivate {
AppServerLink::AppServerLink(void) AppServerLink::AppServerLink()
{ {
sLock.Lock();
// if there is no be_app, we can't do a whole lot, anyway // if there is no be_app, we can't do a whole lot, anyway
if (be_app) { if (be_app != NULL) {
BApplication::Private::ServerLink()->Lock();
fReceiver = &BApplication::Private::ServerLink()->Receiver(); fReceiver = &BApplication::Private::ServerLink()->Receiver();
fSender = &BApplication::Private::ServerLink()->Sender(); fSender = &BApplication::Private::ServerLink()->Sender();
} else { } else {
@@ -44,7 +39,7 @@ AppServerLink::AppServerLink(void)
AppServerLink::~AppServerLink() AppServerLink::~AppServerLink()
{ {
sLock.Unlock(); BApplication::Private::ServerLink()->Unlock();
} }
} // namespace BPrivate } // namespace BPrivate
-11
View File
@@ -40,17 +40,6 @@
#define DBG(x) ; #define DBG(x) ;
#define PRINT(x) DBG({ printf("[%6" B_PRId32 "] ", find_thread(NULL)); printf x; }) #define PRINT(x) DBG({ printf("[%6" B_PRId32 "] ", find_thread(NULL)); printf x; })
/*
#include <Autolock.h>
#include <Locker.h>
static BLocker sDebugPrintLocker("BLooper debug print");
#define PRINT(x) DBG({ \
BAutolock _(sDebugPrintLocker); \
debug_printf("[%6ld] ", find_thread(NULL)); \
debug_printf x; \
})
*/
#define FILTER_LIST_BLOCK_SIZE 5 #define FILTER_LIST_BLOCK_SIZE 5
#define DATA_BLOCK_SIZE 5 #define DATA_BLOCK_SIZE 5
+2
View File
@@ -24,6 +24,8 @@ namespace BPrivate {
ServerLink::ServerLink() ServerLink::ServerLink()
:
BLocker()
{ {
} }
+5 -5
View File
@@ -32,10 +32,10 @@
#include <ApplicationPrivate.h> #include <ApplicationPrivate.h>
#include <AppServerLink.h> #include <AppServerLink.h>
#include <Autolock.h>
#include <ObjectList.h> #include <ObjectList.h>
#include <ServerMemoryAllocator.h> #include <ServerMemoryAllocator.h>
#include <ServerProtocol.h> #include <ServerProtocol.h>
#include <locks.h>
#include "ColorConversion.h" #include "ColorConversion.h"
#include "BitmapPrivate.h" #include "BitmapPrivate.h"
@@ -45,13 +45,13 @@ using namespace BPrivate;
static BObjectList<BBitmap> sBitmapList; static BObjectList<BBitmap> sBitmapList;
static BLocker sBitmapListLock; static mutex sBitmapListLock = MUTEX_INITIALIZER("BBitmap list");
void void
reconnect_bitmaps_to_app_server() reconnect_bitmaps_to_app_server()
{ {
BAutolock _(sBitmapListLock); MutexLocker _(sBitmapListLock);
for (int32 i = 0; i < sBitmapList.CountItems(); i++) { for (int32 i = 0; i < sBitmapList.CountItems(); i++) {
BBitmap::Private bitmap(sBitmapList.ItemAt(i)); BBitmap::Private bitmap(sBitmapList.ItemAt(i));
bitmap.ReconnectToAppServer(); bitmap.ReconnectToAppServer();
@@ -1198,7 +1198,7 @@ BBitmap::_InitObject(BRect bounds, color_space colorSpace, uint32 flags,
// NOTE: why not "0" in case of error? // NOTE: why not "0" in case of error?
fFlags = flags; fFlags = flags;
} else { } else {
BAutolock _(sBitmapListLock); MutexLocker _(sBitmapListLock);
sBitmapList.AddItem(this); sBitmapList.AddItem(this);
} }
} }
@@ -1276,7 +1276,7 @@ BBitmap::_CleanUp()
fServerToken = -1; fServerToken = -1;
fAreaOffset = -1; fAreaOffset = -1;
BAutolock _(sBitmapListLock); MutexLocker _(sBitmapListLock);
sBitmapList.RemoveItem(this); sBitmapList.RemoveItem(this);
} }
fBasePointer = NULL; fBasePointer = NULL;
+5 -5
View File
@@ -24,22 +24,22 @@
#include <Message.h> #include <Message.h>
#include <AppServerLink.h> #include <AppServerLink.h>
#include <Autolock.h>
#include <ObjectList.h> #include <ObjectList.h>
#include <PicturePlayer.h> #include <PicturePlayer.h>
#include <ServerProtocol.h> #include <ServerProtocol.h>
#include <locks.h>
#include "PicturePrivate.h" #include "PicturePrivate.h"
static BObjectList<BPicture> sPictureList; static BObjectList<BPicture> sPictureList;
static BLocker sPictureListLock; static mutex sPictureListLock = MUTEX_INITIALIZER("BPicture list");
void void
reconnect_pictures_to_app_server() reconnect_pictures_to_app_server()
{ {
BAutolock _(sPictureListLock); MutexLocker _(sPictureListLock);
for (int32 i = 0; i < sPictureList.CountItems(); i++) { for (int32 i = 0; i < sPictureList.CountItems(); i++) {
BPicture::Private picture(sPictureList.ItemAt(i)); BPicture::Private picture(sPictureList.ItemAt(i));
picture.ReconnectToAppServer(); picture.ReconnectToAppServer();
@@ -215,14 +215,14 @@ BPicture::_InitData()
fExtent = new (std::nothrow) _BPictureExtent_; fExtent = new (std::nothrow) _BPictureExtent_;
BAutolock _(sPictureListLock); MutexLocker _(sPictureListLock);
sPictureList.AddItem(this); sPictureList.AddItem(this);
} }
BPicture::~BPicture() BPicture::~BPicture()
{ {
BAutolock _(sPictureListLock); MutexLocker _(sPictureListLock);
sPictureList.RemoveItem(this, false); sPictureList.RemoveItem(this, false);
_DisposeData(); _DisposeData();
} }