root/trunk/dep/src/zthread/vanilla/SimpleAtomicCount.cxx @ 2

Revision 2, 2.5 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
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 "zthread/Guard.h"
27#include "../FastLock.h"
28
29#include <assert.h>
30
31namespace ZThread {
32
33typedef struct atomic_count_t {
34
35  FastLock lock;
36  unsigned long count;
37 
38  atomic_count_t() : count(0) {}
39
40} ATOMIC_COUNT;
41
42AtomicCount::AtomicCount() {
43
44  ATOMIC_COUNT* c = new ATOMIC_COUNT;
45  _value = reinterpret_cast<void*>(c);
46 
47}
48
49AtomicCount::~AtomicCount() {
50
51  ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value);
52  assert(c->count == 0);
53
54  delete c;
55
56}
57 
58//! Postfix decrement and return the current value
59size_t AtomicCount::operator--(int) {
60
61  ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value);
62 
63  Guard<FastLock> g(c->lock);
64  return c->count--;
65
66}
67 
68//! Postfix increment and return the current value
69size_t AtomicCount::operator++(int) {
70
71  ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value);
72 
73  Guard<FastLock> g(c->lock);
74  return c->count++;
75
76}
77
78//! Prefix decrement and return the current value
79size_t AtomicCount::operator--() {
80
81  ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value);
82 
83  Guard<FastLock> g(c->lock);
84  return --c->count;
85
86}
87 
88//! Prefix increment and return the current value
89size_t AtomicCount::operator++() {
90 
91  ATOMIC_COUNT* c = reinterpret_cast<ATOMIC_COUNT*>(_value);
92 
93  Guard<FastLock> g(c->lock);
94  return ++c->count;
95
96}
97
98};
99
100#endif // __ZTATOMICCOUNTIMPL_H__
Note: See TracBrowser for help on using the browser.