Library of Assembled Shared Sources
atomic_poor_man.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "../atomic.h"
45
46#ifdef LASS_UTIL_ATOMIC_HAVE_POOR_MANS_IMPL
47
48#if LASS_HAVE_LDREX_STREX || LASS_KUSER_HELPER_VERSION >= 2
49# if LASS_HAVE_LDREXB_STREXB && LASS_HAVE_LDREXH_STREXH
50# if LASS_HAVE_LDREXD_STREXD || LASS_KUSER_HELPER_VERSION >= 5
51 // all should be ok (except for 8-byte adjacent CAS, which shouldn't be used anyway, can we protect against that?)
52# else
53# warning "[LASS BUILD MSG] Poor man's implementation of 4-byte adjacent CAS (used in TaggedPtr), and all 8-byte atomics. Performance will suffer!"
54# endif
55# elif LASS_HAVE_LDREXD_STREXD || LASS_KUSER_HELPER_VERSION >= 5
56# warning "[LASS BUILD MSG] Poor man's implementation of 1- and 2-byte atomics (except 2-byte adjacent CAS). All 4- and 8-byte atomics are good."
57# else
58# warning "[LASS BUILD MSG] Poor man's implementation of 1-, 2- and 8-byte atomics, and 4-byte adjacent CAS (used in TaggedPtr). Performance will suffer!"
59# endif
60#else
61# warning "[LASS BUILD MSG] Poor man's implementation of ALL atomics using a mutex! performance will SEVERELY suffer!"
62#endif
63
64#if LASS_KUSER_HELPER_VERSION >= 2
65
66namespace
67{
68 typedef int (kuser_cmpxchg_t)(int oldval, int newval, volatile int *ptr);
69 #define kuser_cmpxchg (*(kuser_cmpxchg_t *)0xffff0fc0)
70
71 enum
72 {
73 unlocked = 0,
74 locked = 1,
75 };
76 volatile int globalLock = unlocked;
77}
78
79namespace lass
80{
81namespace util
82{
83namespace impl
84{
85
86PoorMansGlobalAtomicLock::PoorMansGlobalAtomicLock()
87{
88 int old;
89 do
90 {
91 old = globalLock;
92 if ( old == locked )
93 {
94 continue;
95 }
96 }
97 while (kuser_cmpxchg(old, locked, &globalLock));
98}
99
100PoorMansGlobalAtomicLock::~PoorMansGlobalAtomicLock()
101{
102 LASS_ASSERT(globalLock == locked);
103 kuser_cmpxchg(locked, unlocked, &globalLock);
104};
105
106}
107}
108}
109
110#else
111
112#include "../thread.h"
113
114namespace
115{
116
117using ::lass::util::Mutex;
118
119Mutex* globalMutex()
120{
121 static Mutex* mutex = 0;
122 if (!mutex)
123 {
124 mutex = new Mutex;
125 }
126 return mutex;
127}
128
129Mutex* const initializeBeforeMain = globalMutex();
130
131}
132
133namespace lass
134{
135namespace util
136{
137namespace impl
138{
139
140PoorMansGlobalAtomicLock::PoorMansGlobalAtomicLock()
141{
142 globalMutex()->lock();
143}
144
145PoorMansGlobalAtomicLock::~PoorMansGlobalAtomicLock()
146{
147 globalMutex()->unlock();
148};
149
150}
151}
152}
153
154#endif
155
156#endif
157
158// EOF
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53