1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __ZTATOMICCOUNTIMPL_H__ |
---|
24 | #define __ZTATOMICCOUNTIMPL_H__ |
---|
25 | |
---|
26 | #include <windows.h> |
---|
27 | #include <assert.h> |
---|
28 | |
---|
29 | namespace ZThread { |
---|
30 | |
---|
31 | typedef struct atomic_count_t { |
---|
32 | |
---|
33 | CRITICAL_SECTION cs; |
---|
34 | size_t count; |
---|
35 | |
---|
36 | atomic_count_t() : count(0) {} |
---|
37 | |
---|
38 | } ATOMIC_COUNT; |
---|
39 | |
---|
40 | AtomicCount::AtomicCount() { |
---|
41 | |
---|
42 | ATOMIC_COUNT* c = new ATOMIC_COUNT; |
---|
43 | _value = reinterpret_cast<void*>(c); |
---|
44 | ::InitializeCriticalSection(&c->cs); |
---|
45 | |
---|
46 | } |
---|
47 | |
---|
48 | AtomicCount::~AtomicCount() { |
---|
49 | |
---|
50 | ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value); |
---|
51 | assert(c->count == 0); |
---|
52 | ::DeleteCriticalSection(&c->cs); |
---|
53 | delete c; |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | //! Postfix decrement and return the current value |
---|
58 | size_t AtomicCount::operator--(int) { |
---|
59 | |
---|
60 | ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value); |
---|
61 | size_t value; |
---|
62 | |
---|
63 | ::EnterCriticalSection(&c->cs); |
---|
64 | value = c->count--; |
---|
65 | ::LeaveCriticalSection(&c->cs); |
---|
66 | |
---|
67 | return value; |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | //! Postfix increment and return the current value |
---|
72 | size_t AtomicCount::operator++(int) { |
---|
73 | |
---|
74 | ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value); |
---|
75 | size_t value; |
---|
76 | |
---|
77 | ::EnterCriticalSection(&c->cs); |
---|
78 | value = c->count++; |
---|
79 | ::LeaveCriticalSection(&c->cs); |
---|
80 | |
---|
81 | return value; |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | //! Prefix decrement and return the current value |
---|
86 | size_t AtomicCount::operator--() { |
---|
87 | |
---|
88 | ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value); |
---|
89 | size_t value; |
---|
90 | |
---|
91 | ::EnterCriticalSection(&c->cs); |
---|
92 | value = --c->count; |
---|
93 | ::LeaveCriticalSection(&c->cs); |
---|
94 | |
---|
95 | return value; |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | //! Prefix increment and return the current value |
---|
100 | size_t AtomicCount::operator++() { |
---|
101 | |
---|
102 | ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value); |
---|
103 | size_t value; |
---|
104 | |
---|
105 | ::EnterCriticalSection(&c->cs); |
---|
106 | value = ++c->count; |
---|
107 | ::LeaveCriticalSection(&c->cs); |
---|
108 | |
---|
109 | return value; |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | }; |
---|
114 | |
---|
115 | #endif // __ZTATOMICCOUNTIMPL_H__ |
---|