diff --git a/include/sltdl/sltdl.h b/include/sltdl/sltdl.h index f2898e0..ea72338 100644 --- a/include/sltdl/sltdl.h +++ b/include/sltdl/sltdl.h @@ -22,6 +22,10 @@ extern "C" { typedef struct lt_modctx * lt_dlhandle; +/* global reference-counting */ +lt_api int lt_dlinit(void); +lt_api int lt_dlexit(void); + #ifdef __cplusplus } #endif diff --git a/project/common.mk b/project/common.mk index 4b72987..0c11081 100644 --- a/project/common.mk +++ b/project/common.mk @@ -1,4 +1,5 @@ API_SRCS = \ + src/core/lt_core.c \ INTERNAL_SRCS = \ diff --git a/project/tree.mk b/project/tree.mk index 20f0861..aa7580b 100644 --- a/project/tree.mk +++ b/project/tree.mk @@ -1,3 +1,4 @@ tree.tag: mkdir -p src + mkdir -p src/core touch tree.tag diff --git a/src/core/lt_core.c b/src/core/lt_core.c new file mode 100644 index 0000000..b0c3952 --- /dev/null +++ b/src/core/lt_core.c @@ -0,0 +1,40 @@ +/*******************************************************************/ +/* sltdl: a surrogate ltdl implementation */ +/* Copyright (C) 2019 Z. Gilboa */ +/* Released under the Standard MIT License; see COPYING.SLTDL. */ +/*******************************************************************/ + +#include +#include +#include + +static int lt_refs = 0; +static pthread_mutex_t lt_lock = PTHREAD_MUTEX_INITIALIZER; + +int lt_dlinit(void) +{ + if (pthread_mutex_lock(<_lock)) + return 1; + + lt_refs++; + pthread_mutex_unlock(<_lock); + + return 0; +} + +int lt_dlexit(void) +{ + if (pthread_mutex_lock(<_lock)) + return 1; + + if (!lt_refs) { + pthread_mutex_unlock(<_lock); + return 1; + } + + lt_refs--; + + pthread_mutex_unlock(<_lock); + + return 0; +}