gloox
1.0.27
src
atomicrefcount.cpp
1
/*
2
Copyright (c) 2007-2023 by Jakob Schröter <js@camaya.net>
3
This file is part of the gloox library. http://camaya.net/gloox
4
5
This software is distributed under a license. The full license
6
agreement can be found in the file LICENSE in this distribution.
7
This software may not be copied, modified, sold or distributed
8
other than expressed in the named license agreement.
9
10
This software is distributed without any warranty.
11
*/
12
13
14
#include "atomicrefcount.h"
15
16
#include "config.h"
17
18
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
19
# include <windows.h>
20
#elif defined( __APPLE__ )
21
# include <libkern/OSAtomic.h>
22
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
23
// Use intrinsic functions - no #include required.
24
#else
25
# include "mutexguard.h"
26
#endif
27
28
#ifdef _WIN32_WCE
29
# include <winbase.h>
30
#endif
31
32
namespace
gloox
33
{
34
35
namespace
util
36
{
37
AtomicRefCount::AtomicRefCount
()
38
: m_count( 0 )
39
{
40
}
41
42
int
AtomicRefCount::increment
()
43
{
44
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
45
return
(
int
) ::InterlockedIncrement( (
volatile
LONG*)&m_count );
46
#elif defined( __APPLE__ )
47
return
(
int
) OSAtomicIncrement32Barrier( (
volatile
int32_t*)&m_count );
48
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
49
// Use the gcc intrinsic for atomic increment if supported.
50
return
static_cast<
int
>
( __sync_add_and_fetch( &m_count, 1 ) );
51
#else
52
// Fallback to using a lock
53
MutexGuard
m( m_lock );
54
return
++m_count;
55
#endif
56
}
57
58
int
AtomicRefCount::decrement
()
59
{
60
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
61
return
(
int
) ::InterlockedDecrement( (
volatile
LONG*)&m_count );
62
#elif defined( __APPLE__ )
63
return
(
int
) OSAtomicDecrement32Barrier( (
volatile
int32_t*)&m_count );
64
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
65
// Use the gcc intrinsic for atomic decrement if supported.
66
return
static_cast<
int
>
( __sync_sub_and_fetch( &m_count, 1 ) );
67
#else
68
// Fallback to using a lock
69
MutexGuard
m( m_lock );
70
return
--m_count;
71
#endif
72
}
73
74
void
AtomicRefCount::reset
()
75
{
76
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
77
::InterlockedExchange( (
volatile
LONG*)&m_count, (
volatile
LONG)0 );
78
#elif defined( __APPLE__ )
79
OSAtomicAnd32Barrier( (uint32_t)0, (
volatile
uint32_t*)&m_count );
80
#elif defined( HAVE_GCC_ATOMIC_BUILTINS )
81
// Use the gcc intrinsic for atomic decrement if supported.
82
__sync_fetch_and_and( &m_count, 0 );
83
#else
84
// Fallback to using a lock
85
MutexGuard
m( m_lock );
86
m_count = 0;
87
#endif
88
}
89
90
}
91
92
}
93
gloox::util::AtomicRefCount::AtomicRefCount
AtomicRefCount()
Definition
atomicrefcount.cpp:37
gloox::util::AtomicRefCount::increment
int increment()
Definition
atomicrefcount.cpp:42
gloox::util::AtomicRefCount::reset
void reset()
Definition
atomicrefcount.cpp:74
gloox::util::AtomicRefCount::decrement
int decrement()
Definition
atomicrefcount.cpp:58
gloox::util::MutexGuard
A simple implementation of a mutex guard.
Definition
mutexguard.h:32
gloox
The namespace for the gloox library.
Definition
adhoc.cpp:28
Generated on Sun Jul 16 2023 08:45:24 for gloox by
1.9.8