dlopen_mt.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <stdlib.h>
  2. #include <dlfcn.h>
  3. #include <stdio.h>
  4. #include "vtv_utils.h"
  5. #include "vtv_rts.h"
  6. #include "pthread.h"
  7. #define NUM_REPEATS 10
  8. #define NUM_THREADS 10
  9. #define NUM_SOS 100
  10. #define NUM_SOS_PER_THREAD (NUM_SOS/NUM_THREADS)
  11. typedef void (*voidfn)(void);
  12. int failures = 0;
  13. void
  14. __vtv_verify_fail (void **data_set_ptr, const void *vtbl_pointer)
  15. {
  16. failures++;
  17. return;
  18. }
  19. void do_dlopen(int so_num)
  20. {
  21. char so_name [sizeof("soxxx.so")];
  22. sprintf(so_name, "so%d.so", so_num);
  23. // printf("dl-opening %s\n", so_name);
  24. void * dlhandle = dlopen(so_name, RTLD_NOW);
  25. if (!dlhandle)
  26. {
  27. fprintf(stderr, "dlopen so:%s error: %s\n", so_name, dlerror());
  28. exit(1);
  29. }
  30. char so_entry [sizeof("so_entry_xxx")];
  31. sprintf(so_entry, "so_entry_%d", so_num);
  32. voidfn so_entry_fn = (voidfn)dlsym(dlhandle, so_entry);
  33. if (!so_entry_fn)
  34. {
  35. fprintf(stderr, "so:%s dlsym error: %s\n", so_name, dlerror());
  36. exit(2);
  37. }
  38. so_entry_fn();
  39. dlclose(dlhandle);
  40. }
  41. volatile int threads_completed_it = 0;
  42. volatile int current_wave = -1;
  43. void * do_dlopens(void * ptid)
  44. {
  45. for (int k = 0; k < NUM_REPEATS; k++)
  46. {
  47. for (int i = 0; i < NUM_SOS_PER_THREAD; i++)
  48. {
  49. while (current_wave < (k*NUM_SOS_PER_THREAD + i)) /* from 0 to 99 */
  50. ;
  51. do_dlopen((NUM_SOS_PER_THREAD * *(int *)ptid) + i);
  52. int old_value;
  53. do {
  54. old_value = threads_completed_it;
  55. } while (!__sync_bool_compare_and_swap(&threads_completed_it, old_value, old_value + 1));
  56. if (old_value == (NUM_THREADS - 1)) // Only one thread will do this.
  57. {
  58. threads_completed_it = 0;
  59. printf("%c%d", 13, current_wave + 1);
  60. fflush(stdout);
  61. current_wave++;
  62. }
  63. }
  64. }
  65. return NULL;
  66. }
  67. int main()
  68. {
  69. pthread_t thread_ids[NUM_THREADS];
  70. int thread_nids[NUM_THREADS];
  71. for (int t = 0; t < NUM_THREADS; t++ )
  72. {
  73. thread_nids[t] = t;
  74. if (pthread_create(&thread_ids[t], NULL, do_dlopens, &thread_nids[t]) != 0)
  75. {
  76. printf("failed pthread_create\n");
  77. exit(1);
  78. }
  79. }
  80. current_wave = 0; // start the work on the other threads
  81. for (int t = 0; t < NUM_THREADS; t++)
  82. if (pthread_join(thread_ids[t], NULL) != 0)
  83. {
  84. printf("failed pthread_join\n");
  85. exit(2);
  86. }
  87. printf("\n");
  88. return 0;
  89. }