#include <stdio.h> // fprintf(), NULL
#include <stdlib.h> // exit(), EXIT_SUCCESS
#include <string.h>
#include <dlfcn.h>
#include <pthread.h>
#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
void* h1 = NULL;
void* h2 = NULL;
static void* work(void* ignore)
{
for (int i=0; i < 10000; ++i) {
h1 = dlopen("libfoo1.dylib", 0);
if ( h1 == NULL ) {
FAIL("dlopen failed: %s", dlerror());
exit(0);
}
dlclose(h2);
h2 = dlopen("libfoo2.dylib", 0);
if ( h2 == NULL ) {
FAIL("dlopen failed: %s", dlerror());
exit(0);
}
dlclose(h1);
}
return NULL;
}
int main()
{
h2 = dlopen("libfoo2.dylib", 0);
if ( h2 == NULL ) {
FAIL("dlopen failed: %s", dlerror());
exit(0);
}
pthread_t other;
if ( pthread_create(&other, NULL, work, NULL) != 0 ) {
FAIL("pthread_create failed");
exit(0);
}
for (int i=0; i < 15000; ++i) {
void* handle = dlopen("libclient.dylib", 0);
if ( handle == NULL ) {
FAIL("dlopen failed: %s", dlerror());
exit(0);
}
typedef void (*proc_t)();
proc_t proc = dlsym(handle, "doit");
if ( proc == NULL ) {
FAIL("dlsym failed: %s", dlerror());
exit(0);
}
(*proc)();
dlclose(handle);
}
void* result;
pthread_join(other, &result);
PASS("threaded-flat-lookup");
return 0;
}