Blame src/thread-utils.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_thread_utils_h__
Packit Service 20376f
#define INCLUDE_thread_utils_h__
Packit Service 20376f
Packit Service 20376f
#if defined(__GNUC__) && defined(GIT_THREADS)
Packit Service 20376f
# if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1))
Packit Service 20376f
#  error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DTHREADSAFE=OFF
Packit Service 20376f
# endif
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
/* Common operations even if threading has been disabled */
Packit Service 20376f
typedef struct {
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	volatile long val;
Packit Service 20376f
#else
Packit Service 20376f
	volatile int val;
Packit Service 20376f
#endif
Packit Service 20376f
} git_atomic;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_ARCH_64
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	__int64 val;
Packit Service 20376f
#else
Packit Service 20376f
	int64_t val;
Packit Service 20376f
#endif
Packit Service 20376f
} git_atomic64;
Packit Service 20376f
Packit Service 20376f
typedef git_atomic64 git_atomic_ssize;
Packit Service 20376f
Packit Service 20376f
#define git_atomic_ssize_add git_atomic64_add
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
typedef git_atomic git_atomic_ssize;
Packit Service 20376f
Packit Service 20376f
#define git_atomic_ssize_add git_atomic_add
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_THREADS
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
#   include "win32/thread.h"
Packit Service 20376f
#else
Packit Service 20376f
#   include "unix/pthread.h"
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
Packit Service 20376f
{
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	InterlockedExchange(&a->val, (LONG)val);
Packit Service 20376f
#elif defined(__GNUC__)
Packit Service 20376f
	__sync_lock_test_and_set(&a->val, val);
Packit Service 20376f
#else
Packit Service 20376f
#	error "Unsupported architecture for atomic operations"
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_inc(git_atomic *a)
Packit Service 20376f
{
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	return InterlockedIncrement(&a->val);
Packit Service 20376f
#elif defined(__GNUC__)
Packit Service 20376f
	return __sync_add_and_fetch(&a->val, 1);
Packit Service 20376f
#else
Packit Service 20376f
#	error "Unsupported architecture for atomic operations"
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
Packit Service 20376f
{
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	return InterlockedExchangeAdd(&a->val, addend);
Packit Service 20376f
#elif defined(__GNUC__)
Packit Service 20376f
	return __sync_add_and_fetch(&a->val, addend);
Packit Service 20376f
#else
Packit Service 20376f
#	error "Unsupported architecture for atomic operations"
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_dec(git_atomic *a)
Packit Service 20376f
{
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	return InterlockedDecrement(&a->val);
Packit Service 20376f
#elif defined(__GNUC__)
Packit Service 20376f
	return __sync_sub_and_fetch(&a->val, 1);
Packit Service 20376f
#else
Packit Service 20376f
#	error "Unsupported architecture for atomic operations"
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git___compare_and_swap(
Packit Service 20376f
	void * volatile *ptr, void *oldval, void *newval)
Packit Service 20376f
{
Packit Service 20376f
	volatile void *foundval;
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	foundval = InterlockedCompareExchangePointer((volatile PVOID *)ptr, newval, oldval);
Packit Service 20376f
#elif defined(__GNUC__)
Packit Service 20376f
	foundval = __sync_val_compare_and_swap(ptr, oldval, newval);
Packit Service 20376f
#else
Packit Service 20376f
#	error "Unsupported architecture for atomic operations"
Packit Service 20376f
#endif
Packit Service 20376f
	return (foundval == oldval) ? oldval : newval;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(volatile void *) git___swap(
Packit Service 20376f
	void * volatile *ptr, void *newval)
Packit Service 20376f
{
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	return InterlockedExchangePointer(ptr, newval);
Packit Service 20376f
#else
Packit Service 20376f
	return __sync_lock_test_and_set(ptr, newval);
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_ARCH_64
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
Packit Service 20376f
{
Packit Service 20376f
#if defined(GIT_WIN32)
Packit Service 20376f
	return InterlockedExchangeAdd64(&a->val, addend);
Packit Service 20376f
#elif defined(__GNUC__)
Packit Service 20376f
	return __sync_add_and_fetch(&a->val, addend);
Packit Service 20376f
#else
Packit Service 20376f
#	error "Unsupported architecture for atomic operations"
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
#define git_thread unsigned int
Packit Service 20376f
#define git_thread_create(thread, start_routine, arg) 0
Packit Service 20376f
#define git_thread_join(id, status) (void)0
Packit Service 20376f
Packit Service 20376f
/* Pthreads Mutex */
Packit Service 20376f
#define git_mutex unsigned int
Packit Service 20376f
GIT_INLINE(int) git_mutex_init(git_mutex *mutex) \
Packit Service 20376f
	{ GIT_UNUSED(mutex); return 0; }
Packit Service 20376f
GIT_INLINE(int) git_mutex_lock(git_mutex *mutex) \
Packit Service 20376f
	{ GIT_UNUSED(mutex); return 0; }
Packit Service 20376f
#define git_mutex_unlock(a) (void)0
Packit Service 20376f
#define git_mutex_free(a) (void)0
Packit Service 20376f
Packit Service 20376f
/* Pthreads condition vars */
Packit Service 20376f
#define git_cond unsigned int
Packit Service 20376f
#define git_cond_init(c, a)	(void)0
Packit Service 20376f
#define git_cond_free(c) (void)0
Packit Service 20376f
#define git_cond_wait(c, l)	(void)0
Packit Service 20376f
#define git_cond_signal(c) (void)0
Packit Service 20376f
#define git_cond_broadcast(c) (void)0
Packit Service 20376f
Packit Service 20376f
/* Pthreads rwlock */
Packit Service 20376f
#define git_rwlock unsigned int
Packit Service 20376f
#define git_rwlock_init(a)		0
Packit Service 20376f
#define git_rwlock_rdlock(a)	0
Packit Service 20376f
#define git_rwlock_rdunlock(a)	(void)0
Packit Service 20376f
#define git_rwlock_wrlock(a)	0
Packit Service 20376f
#define git_rwlock_wrunlock(a)	(void)0
Packit Service 20376f
#define git_rwlock_free(a)		(void)0
Packit Service 20376f
#define GIT_RWLOCK_STATIC_INIT	0
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
Packit Service 20376f
{
Packit Service 20376f
	a->val = val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_inc(git_atomic *a)
Packit Service 20376f
{
Packit Service 20376f
	return ++a->val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_add(git_atomic *a, int32_t addend)
Packit Service 20376f
{
Packit Service 20376f
	a->val += addend;
Packit Service 20376f
	return a->val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_dec(git_atomic *a)
Packit Service 20376f
{
Packit Service 20376f
	return --a->val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git___compare_and_swap(
Packit Service 20376f
	void * volatile *ptr, void *oldval, void *newval)
Packit Service 20376f
{
Packit Service 20376f
	if (*ptr == oldval)
Packit Service 20376f
		*ptr = newval;
Packit Service 20376f
	else
Packit Service 20376f
		oldval = newval;
Packit Service 20376f
	return oldval;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(volatile void *) git___swap(
Packit Service 20376f
	void * volatile *ptr, void *newval)
Packit Service 20376f
{
Packit Service 20376f
	volatile void *old = *ptr;
Packit Service 20376f
	*ptr = newval;
Packit Service 20376f
	return old;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_ARCH_64
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
Packit Service 20376f
{
Packit Service 20376f
	a->val += addend;
Packit Service 20376f
	return a->val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git_atomic_get(git_atomic *a)
Packit Service 20376f
{
Packit Service 20376f
	return (int)a->val;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Atomically replace oldval with newval
Packit Service 20376f
 * @return oldval if it was replaced or newval if it was not
Packit Service 20376f
 */
Packit Service 20376f
#define git__compare_and_swap(P,O,N) \
Packit Service 20376f
	git___compare_and_swap((void * volatile *)P, O, N)
Packit Service 20376f
Packit Service 20376f
#define git__swap(ptr, val) (void *)git___swap((void * volatile *)&ptr, val)
Packit Service 20376f
Packit Service 20376f
extern int git_online_cpus(void);
Packit Service 20376f
Packit Service 20376f
#if defined(GIT_THREADS) && defined(_MSC_VER)
Packit Service 20376f
# define GIT_MEMORY_BARRIER MemoryBarrier()
Packit Service 20376f
#elif defined(GIT_THREADS)
Packit Service 20376f
# define GIT_MEMORY_BARRIER __sync_synchronize()
Packit Service 20376f
#else
Packit Service 20376f
# define GIT_MEMORY_BARRIER /* noop */
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#endif /* INCLUDE_thread_utils_h__ */