1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// GML - OpenGL Multithreading Library
// for Spring http://springrts.com
// Author: Mattias "zerver" Radeskog
// (C) Ware Zerver Tech. http://zerver.net
// Ware Zerver Tech. licenses this library
// to be used, distributed and modified
// freely for any purpose, as long as
// this notice remains unchanged
// This code is largely based on boost::detail::atomic_count
//
// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef GMLCNT_H
#define GMLCNT_H
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#include <boost/config.hpp>
// evaluate which locking method to use
#if !defined(BOOST_HAS_THREADS)
#define GML_THREADS_NO_BOOST_THREADS
#elif defined(BOOST_AC_USE_PTHREADS)
#define GML_THREADS_PTHREADS
#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
#define GML_THREADS_ATOMIC
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#define GML_THREADS_BOOST_INTERLOCK
#elif defined( __GNUC__ ) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 ) && !defined( __arm__ ) && !defined( __hppa ) && ( !defined( __INTEL_COMPILER ) || defined( __ia64__ ) )
#define GML_THREADS_SYNC
#elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
#define GML_THREADS_ATOMICITY
#elif defined(BOOST_HAS_PTHREADS)
#define BOOST_AC_USE_PTHREADS
#define GML_THREADS_PTHREADS
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(GML_THREADS_NO_BOOST_THREADS)
typedef long gmlCount;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#elif defined(GML_THREADS_PTHREADS)
#include <pthread.h>
class gmlCount {
private:
class scoped_lock {
public:
scoped_lock(pthread_mutex_t & m): m_(m) {
pthread_mutex_lock(&m_);
}
~scoped_lock() {
pthread_mutex_unlock(&m_);
}
private:
pthread_mutex_t & m_;
};
public:
explicit gmlCount(long v): value_(v) {
pthread_mutex_init(&mutex_, 0);
}
~gmlCount() {
pthread_mutex_destroy(&mutex_);
}
long operator++() {
scoped_lock lock(mutex_);
return ++value_;
}
long operator--() {
scoped_lock lock(mutex_);
return --value_;
}
operator long() const {
scoped_lock lock(mutex_);
return value_;
}
long value_;
private:
gmlCount(gmlCount const &);
gmlCount & operator=(gmlCount const &);
mutable pthread_mutex_t mutex_;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#elif defined(GML_THREADS_ATOMIC)
class gmlCount {
public:
explicit gmlCount( long v ) : value_( static_cast< int >( v ) ) {}
long operator++() {
return atomic_exchange_and_add( &value_, 1 ) + 1;
}
long operator--() {
return atomic_exchange_and_add( &value_, -1 ) - 1;
}
operator long() const {
return atomic_exchange_and_add( &value_, 0 );
}
mutable int value_;
private:
gmlCount(gmlCount const &);
gmlCount & operator=(gmlCount const &);
private:
static int atomic_exchange_and_add( int * pw, int dv ) {
// int r = *pw;
// *pw += dv;
// return r;
int r;
__asm__ __volatile__
(
"lock\n\t"
"xadd %1, %0":
"+m"( *pw ), "=r"( r ): // outputs (%0, %1)
"1"( dv ): // inputs (%2 == %1)
"memory", "cc" // clobbers
);
return r;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#elif defined(GML_THREADS_BOOST_INTERLOCK)
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#include <boost/detail/interlocked.hpp>
class gmlCount {
public:
explicit gmlCount( long v ): value_( v ) {}
long operator++() {
return BOOST_INTERLOCKED_INCREMENT( &value_ );
}
long operator--() {
return BOOST_INTERLOCKED_DECREMENT( &value_ );
}
operator long() const {
return static_cast<long const volatile &>( value_ );
}
long value_;
private:
gmlCount( gmlCount const & );
gmlCount & operator=( gmlCount const & );
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#elif defined(GML_THREADS_SYNC)
#if defined( __ia64__ ) && defined( __INTEL_COMPILER )
# include <ia64intrin.h>
#endif
class gmlCount {
public:
explicit gmlCount( long v ) : value_( v ) {}
long operator++() {
return __sync_add_and_fetch( &value_, 1 );
}
long operator--() {
return __sync_add_and_fetch( &value_, -1 );
}
operator long() const {
return __sync_fetch_and_add( &value_, 0 );
}
mutable long value_;
private:
gmlCount(gmlCount const &);
gmlCount & operator=(gmlCount const &);
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#elif defined(GML_THREADS_ATOMICITY)
#include <bits/atomicity.h>
#if defined(__GLIBCXX__) // g++ 3.4+
using __gnu_cxx::__atomic_add;
using __gnu_cxx::__exchange_and_add;
#endif
class gmlCount {
public:
explicit gmlCount(long v) : value_(v) {}
long operator++() {
return __exchange_and_add(&value_, 1) + 1;
}
long operator--() {
return __exchange_and_add(&value_, -1) - 1;
}
operator long() const {
return __exchange_and_add(&value_, 0);
}
mutable _Atomic_word value_;
private:
gmlCount(gmlCount const &);
gmlCount & operator=(gmlCount const &);
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////
#else
#error Unrecognized threading platform
#endif
#ifdef BOOST_HAS_THREADS
inline void operator%=(gmlCount& a, long val) {
a.value_=val;
}
#endif
#endif // #ifndef GMLCNT_H
|