#include <utils.h>
#include <sys/time.h>
#include <time.h>
#include <pthread.h>

// mutex for locking parallelism in large memory transfers
static pthread_mutex_t memorymut;

unsigned int (*localcount)(void);
int64_t (*longcount)(void);

/*
 * A thread-safe usec sleep
 * (Note: on solaris, usleep is not thread-safe)
 */
#ifndef WIN32
int avi_usleep(unsigned long delay)
{
#if   HAVE_NANOSLEEP
    struct timespec tsp;
    tsp.tv_sec  =  delay / 1000000;
    tsp.tv_nsec = (delay % 1000000) * 1000;
    return nanosleep(&tsp, NULL);
#else
    return usleep(delay);
#endif
}
#endif

void memory_lock(void)
{
    pthread_mutex_lock(&memorymut);
}

void memory_unlock(void)
{
    pthread_mutex_unlock(&memorymut);
}

void memory_lock_init(void)
{
    pthread_mutex_init(&memorymut, NULL);
}

