Blame lib/locks.h

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