From 564b3cebdb1c8fffd078822e1971b9cfc8b29f23 Mon Sep 17 00:00:00 2001 From: midipix Date: Mon, 28 Oct 2019 03:45:34 +0000 Subject: core: added lt_dlinit(), lt_dlexit() [global reference-counting]. --- src/core/lt_core.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/core/lt_core.c (limited to 'src') 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; +} -- cgit v1.2.3