Blame src/core/lt_core.c

564b3c
/*******************************************************************/
564b3c
/*  sltdl: a surrogate ltdl implementation                         */
564b3c
/*  Copyright (C) 2019 Z. Gilboa                                   */
564b3c
/*  Released under the Standard MIT License; see COPYING.SLTDL.    */
564b3c
/*******************************************************************/
564b3c
564b3c
#include <limits.h>
564b3c
#include <pthread.h>
564b3c
#include <sltdl/sltdl.h>
564b3c
564b3c
static int             lt_refs = 0;
564b3c
static pthread_mutex_t lt_lock = PTHREAD_MUTEX_INITIALIZER;
564b3c
564b3c
int lt_dlinit(void)
564b3c
{
564b3c
	if (pthread_mutex_lock(&lt_lock))
564b3c
		return 1;
564b3c
564b3c
	lt_refs++;
564b3c
	pthread_mutex_unlock(&lt_lock);
564b3c
564b3c
	return 0;
564b3c
}
564b3c
564b3c
int lt_dlexit(void)
564b3c
{
564b3c
	if (pthread_mutex_lock(&lt_lock))
564b3c
		return 1;
564b3c
564b3c
	if (!lt_refs) {
564b3c
		pthread_mutex_unlock(&lt_lock);
564b3c
		return 1;
564b3c
	}
564b3c
564b3c
	lt_refs--;
564b3c
564b3c
	pthread_mutex_unlock(&lt_lock);
564b3c
564b3c
	return 0;
564b3c
}
c42bd5
c42bd5
void lt_slock(void)
c42bd5
{
c42bd5
	int locked;
c42bd5
c42bd5
	do {
c42bd5
		locked = pthread_mutex_lock(&lt_lock);
c42bd5
	} while (locked);
c42bd5
}
c42bd5
c42bd5
int lt_sunlock(int ret)
c42bd5
{
c42bd5
	pthread_mutex_unlock(&lt_lock);
c42bd5
	return ret;
c42bd5
}