0003-Pthread-once-for-funcs-init.patch 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. diff --git a/third_party/ashmem/ashmem-dev.c b/third_party/ashmem/ashmem-dev.c
  2. index 25a33cdcd0c8..399ea36ce382 100644
  3. --- a/third_party/ashmem/ashmem-dev.c
  4. +++ b/third_party/ashmem/ashmem-dev.c
  5. @@ -18,6 +18,7 @@
  6. #include <dlfcn.h>
  7. #include <errno.h>
  8. +#include <pthread.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. @@ -190,26 +191,30 @@ typedef struct {
  13. ASharedMemory_setProtFunc setProt;
  14. } ASharedMemoryFuncs;
  15. -const ASharedMemoryFuncs* ashmem_get_funcs() {
  16. - static ASharedMemoryFuncs s_ashmem_funcs = {};
  17. +static ASharedMemoryFuncs s_ashmem_funcs = {};
  18. +static pthread_once_t s_ashmem_funcs_once = PTHREAD_ONCE_INIT;
  19. +
  20. +static void ashmem_init_funcs() {
  21. ASharedMemoryFuncs* funcs = &s_ashmem_funcs;
  22. - if (funcs->create == NULL) {
  23. - if (device_api_level() >= __ANDROID_API_O__) {
  24. - /* Leaked intentionally! */
  25. - void* lib = dlopen("libandroid.so", RTLD_NOW);
  26. - funcs->create = (ASharedMemory_createFunc)
  27. - dlsym(lib, "ASharedMemory_create");
  28. - funcs->getSize = (ASharedMemory_getSizeFunc)
  29. - dlsym(lib, "ASharedMemory_getSize");
  30. - funcs->setProt = (ASharedMemory_setProtFunc)
  31. - dlsym(lib, "ASharedMemory_setProt");
  32. - } else {
  33. - funcs->create = &ashmem_dev_create_region;
  34. - funcs->getSize = &ashmem_dev_get_size_region;
  35. - funcs->setProt = &ashmem_dev_set_prot_region;
  36. - }
  37. + if (device_api_level() >= __ANDROID_API_O__) {
  38. + /* Leaked intentionally! */
  39. + void* lib = dlopen("libandroid.so", RTLD_NOW);
  40. + funcs->create =
  41. + (ASharedMemory_createFunc)dlsym(lib, "ASharedMemory_create");
  42. + funcs->getSize =
  43. + (ASharedMemory_getSizeFunc)dlsym(lib, "ASharedMemory_getSize");
  44. + funcs->setProt =
  45. + (ASharedMemory_setProtFunc)dlsym(lib, "ASharedMemory_setProt");
  46. + } else {
  47. + funcs->create = &ashmem_dev_create_region;
  48. + funcs->getSize = &ashmem_dev_get_size_region;
  49. + funcs->setProt = &ashmem_dev_set_prot_region;
  50. }
  51. - return funcs;
  52. +}
  53. +
  54. +static const ASharedMemoryFuncs* ashmem_get_funcs() {
  55. + pthread_once(&s_ashmem_funcs_once, ashmem_init_funcs);
  56. + return &s_ashmem_funcs;
  57. }
  58. int ashmem_create_region(const char* name, size_t size) {