godot/thirdparty/zstd/common/threading.h

/**
 * Copyright (c) 2016 Tino Reichardt
 * All rights reserved.
 *
 * You can contact the author at:
 * - zstdmt source repository: https://github.com/mcmilk/zstdmt
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 * You may select, at your option, one of the above-listed licenses.
 */

#ifndef THREADING_H_938743
#define THREADING_H_938743

#include "debug.h"

#if defined (__cplusplus)
extern "C" {
#endif

#if defined(ZSTD_MULTITHREAD) && defined(_WIN32)

/**
 * Windows minimalist Pthread Wrapper
 */
#ifdef WINVER
#  undef WINVER
#endif
#define WINVER

#ifdef _WIN32_WINNT
#  undef _WIN32_WINNT
#endif
#define _WIN32_WINNT

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#undef ERROR   /* reported already defined on VS 2015 (Rich Geldreich) */
#include <windows.h>
#undef ERROR
#define ERROR


/* mutex */
#define ZSTD_pthread_mutex_t
#define ZSTD_pthread_mutex_init
#define ZSTD_pthread_mutex_destroy
#define ZSTD_pthread_mutex_lock
#define ZSTD_pthread_mutex_unlock

/* condition variable */
#define ZSTD_pthread_cond_t
#define ZSTD_pthread_cond_init
#define ZSTD_pthread_cond_destroy
#define ZSTD_pthread_cond_wait
#define ZSTD_pthread_cond_signal
#define ZSTD_pthread_cond_broadcast

/* ZSTD_pthread_create() and ZSTD_pthread_join() */
typedef HANDLE ZSTD_pthread_t;

int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
                   void* (*start_routine) (void*), void* arg);

int ZSTD_pthread_join(ZSTD_pthread_t thread);

/**
 * add here more wrappers as required
 */


#elif defined(ZSTD_MULTITHREAD)    /* posix assumed ; need a better detection method */
/* ===   POSIX Systems   === */
#  include <pthread.h>

#if DEBUGLEVEL < 1

#define ZSTD_pthread_mutex_t
#define ZSTD_pthread_mutex_init
#define ZSTD_pthread_mutex_destroy
#define ZSTD_pthread_mutex_lock
#define ZSTD_pthread_mutex_unlock

#define ZSTD_pthread_cond_t
#define ZSTD_pthread_cond_init
#define ZSTD_pthread_cond_destroy
#define ZSTD_pthread_cond_wait
#define ZSTD_pthread_cond_signal
#define ZSTD_pthread_cond_broadcast

#define ZSTD_pthread_t
#define ZSTD_pthread_create
#define ZSTD_pthread_join

#else /* DEBUGLEVEL >= 1 */

/* Debug implementation of threading.
 * In this implementation we use pointers for mutexes and condition variables.
 * This way, if we forget to init/destroy them the program will crash or ASAN
 * will report leaks.
 */

#define ZSTD_pthread_mutex_t
int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* mutex, pthread_mutexattr_t const* attr);
int ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* mutex);
#define ZSTD_pthread_mutex_lock
#define ZSTD_pthread_mutex_unlock

#define ZSTD_pthread_cond_t
int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* cond, pthread_condattr_t const* attr);
int ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* cond);
#define ZSTD_pthread_cond_wait
#define ZSTD_pthread_cond_signal
#define ZSTD_pthread_cond_broadcast

#define ZSTD_pthread_t
#define ZSTD_pthread_create
#define ZSTD_pthread_join

#endif

#else  /* ZSTD_MULTITHREAD not defined */
/* No multithreading support */

ZSTD_pthread_mutex_t;
#define ZSTD_pthread_mutex_init(a, b)
#define ZSTD_pthread_mutex_destroy(a)
#define ZSTD_pthread_mutex_lock(a)
#define ZSTD_pthread_mutex_unlock(a)

ZSTD_pthread_cond_t;
#define ZSTD_pthread_cond_init(a, b)
#define ZSTD_pthread_cond_destroy(a)
#define ZSTD_pthread_cond_wait(a, b)
#define ZSTD_pthread_cond_signal(a)
#define ZSTD_pthread_cond_broadcast(a)

/* do not use ZSTD_pthread_t */

#endif /* ZSTD_MULTITHREAD */

#if defined (__cplusplus)
}
#endif

#endif /* THREADING_H_938743 */