/*******************************************************************/
/* sltdl: a surrogate ltdl implementation */
/* Copyright (C) 2019 Z. Gilboa */
/* Released under the Standard MIT License; see COPYING.SLTDL. */
/*******************************************************************/
#include <limits.h>
#include <pthread.h>
#include <sltdl/sltdl.h>
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;
}
void lt_slock(void)
{
int locked;
do {
locked = pthread_mutex_lock(<_lock);
} while (locked);
}
int lt_sunlock(int ret)
{
pthread_mutex_unlock(<_lock);
return ret;
}