Blame lib/locks.h

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#ifndef GNUTLS_LOCKS_H
Packit 549fdc
#define GNUTLS_LOCKS_H
Packit 549fdc
Packit 549fdc
#include <gnutls/gnutls.h>
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include <system.h>
Packit 549fdc
Packit 549fdc
#ifdef HAVE_STDATOMIC_H
Packit 549fdc
# include <stdatomic.h>
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
extern mutex_init_func gnutls_mutex_init;
Packit 549fdc
extern mutex_deinit_func gnutls_mutex_deinit;
Packit 549fdc
extern mutex_lock_func gnutls_mutex_lock;
Packit 549fdc
extern mutex_unlock_func gnutls_mutex_unlock;
Packit 549fdc
Packit 549fdc
#if defined(HAVE_WIN32_LOCKS)
Packit 549fdc
# include <windows.h>
Packit 549fdc
Packit 549fdc
/* Idea based based on comment 2 at:
Packit 549fdc
 * http://stackoverflow.com/questions/3555859/is-it-possible-to-do-static-initialization-of-mutexes-in-windows
Packit 549fdc
 */
Packit 549fdc
# define GNUTLS_STATIC_MUTEX(mutex) \
Packit 549fdc
	static CRITICAL_SECTION *mutex = NULL
Packit 549fdc
Packit 549fdc
# define GNUTLS_STATIC_MUTEX_LOCK(mutex) \
Packit 549fdc
	if (mutex == NULL) { \
Packit 549fdc
		CRITICAL_SECTION *mutex##tmp = malloc(sizeof(CRITICAL_SECTION)); \
Packit 549fdc
		InitializeCriticalSection(mutex##tmp); \
Packit 549fdc
		if (InterlockedCompareExchangePointer((PVOID*)&mutex, (PVOID)mutex##tmp, NULL) != NULL) { \
Packit 549fdc
			DeleteCriticalSection(mutex##tmp); \
Packit 549fdc
			free(mutex##tmp); \
Packit 549fdc
		} \
Packit 549fdc
	} \
Packit 549fdc
	EnterCriticalSection(mutex)
Packit 549fdc
Packit 549fdc
# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex) \
Packit 549fdc
	LeaveCriticalSection(mutex)
Packit 549fdc
Packit 549fdc
#elif defined(HAVE_PTHREAD_LOCKS)
Packit 549fdc
# include <pthread.h>
Packit 549fdc
# define GNUTLS_STATIC_MUTEX(mutex) \
Packit 549fdc
	static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
Packit 549fdc
Packit 549fdc
# define GNUTLS_STATIC_MUTEX_LOCK(mutex) \
Packit 549fdc
	pthread_mutex_lock(&mutex)
Packit 549fdc
Packit 549fdc
# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex) \
Packit 549fdc
	pthread_mutex_unlock(&mutex)
Packit 549fdc
Packit 549fdc
#else
Packit 549fdc
# define GNUTLS_STATIC_MUTEX(mutex)
Packit 549fdc
# define GNUTLS_STATIC_MUTEX_LOCK(mutex)
Packit 549fdc
# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex)
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
#endif