将共享库中定义的 structure/data 的单个副本共享到多个共享库中定义的不同对象

Share single copy of structure/data defined in shared library to different objects defined in multiple shared libraries

语言:C++/C Android 生成文件系统:https://developer.android.com/ndk/guides/android_mk

我有一个打开共享库 foo.so 的应用程序,在 foo.so 中我们打开了另外三个共享库 bar1.sobar2.sobar3.so三个不同的线程 (pthread_create) 但在同一个 application/process 中。 pid 相同,bar1、bar2、bar3 的线程 ID 不同

bar1.sobar2.sobar3.so 中的每一个都有 Entry 函数,它们在 dlopen 之后使用 dlsym 调用,并且不同类型的对象是在 bar1 2 3.so

中创建的

bar1.sobar2.sobar3.so 也有动态链接到另一个名为 baz.so 的共享库,它有一些共同的 structs/routines。 baz.so 未明确 'dlopen' 编辑。在 bar1.so、bar2.so、bar3.so 的 make 文件中,我将 baz.so 指定为它需要的共享库。我想在 baz.so 中创建一个单例结构,其中每个 bar1.sobar2.sobar3.so 都可以写入和读取 to/from.

我尝试在 baz.so 中创建静态对象,但 bar1.sobar2.sobar3.so 似乎获得了结构的不同副本。它不是共享的。 通过不同的副本我的意思是根据下面的代码片段,当我读入 bar2.so 时,sharedData.a 是 10 而不是 22

baz.so: 
Filename: sharedobject.h -> All code is defined in sharedobject.h file in baz.so
struct Config {
    int a;
    bool b;
    // Constructor
    Config (int val1, bool val2) {
        a = val1;
        b = val2;
    }
};
class SharedObject {
public:
    Config sharedData;
    SharedObject() {
       // Initialize some common data here which will be read
       // and written to
       sharedData.a = 10;
       sharedData.b = 20;
    }
    static SharedObject* GetInstance() {
        static SharedObject singletonObjectInstance;
        return &singletonObjectInstance;
    }
    // Data update function called by either of bar1, bar2 or bar3.so 
    static void UpdateData(Config &data) {
        // Lock : Proper Locking is there but not shown in code
        SharedObject::GetInstance()->sharedData.a = data.a;
        // Unlock
    } 
}
Android.mk:
LOCAL_SRC_FILES :=  // Empty fields 
LOCAL_INC_FILES :=  // Empty Fields
LOCAL_MODULE := baz
include $(BUILD_SHARED_LIBRARY) # This will build baz.so
bar1.so: Let's say Thread1 under ProcessX updates some data
Filename: classx.cpp
#include "sharedobject.h"
   void ClassX::WriteToCommonData() {
      SharedObject *pSharedObject= SharedObject::GetInstance();
      print("Object Address:%p", pSharedObject); // These addresses are not same in bar1 and bar2 which is the problem
      Config data(22, false);
      SharedObject::UpdateData(data);
   }
In Android.mk of bar1.so
Linking is done by specifying shared lib: -> https://developer.android.com/ndk/guides/android_mk#local_shared_libraries
LOCAL_SRC_FILES := classx.cpp
LOCAL_SHARED_LIBRARIES := baz.so
LOCAL_CFLAGS :=   # No Special flags used.
LOCAL_MODULE := bar1
include $(BUILD_SHARED_LIBRARY) # This will build bar1.so 

bar2.so: Lets's say Thread2 under same ProcessX wants to read it now
Filename: classy.cpp
#include "sharedobject.h"
   void ClassY::ReadFromCommonData() {
       SharedObject *pSharedObject= SharedObject::GetInstance();
       print("Object Address:%p", pSharedObject); // These addresses are not same in bar1 and bar2 which is the problem
       // Read values updated by bar1.so
       print(pSharedObject->sharedData.a); // It is 10, not 22
   } 
In Android.mk

LOCAL_SHARED_LIBRARIES := baz.so
LOCAL_SRC_FILES := classy.cpp
LOCAL_CFLAGS :=   # No Special flags used.
LOCAL_MODULE := bar2
include $(BUILD_SHARED_LIBRARY) # This will build bar2.so 

即使所有 bar 库都在同一个进程下,我还需要使用共享内存 API 吗?

我检查了这些链接,但它似乎没有回答我的问题: how to share a single shared library(*.so) instance between two applications Shared Library Structure

你需要的是同步。您的 UpdateData 有“锁定”和“解锁”,我认为它们是线程同步的占位符。至少,他们应该是。但是您的示例在读取变量时不会锁定。 reader 和 writer 都需要加锁。除了锁定以确保无竞争访问外,您还需要使用某种机制来确保读取发生在 之后 写入而不是之前。例如,可以使用条件变量。

class __attribute__ ((visibility ("default")) SharedObject

如上所述定义 class 解决了这个问题。似乎没有这个,符号不会被导出并且不能用于动态链接器。