Orfeo Toolbox  4.0
itkTimeStamp.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #include "itkFastMutexLock.h"
29 
30 #if defined( _WIN32 )
31  #include "itkWindows.h"
32 
33 #elif defined( __APPLE__ )
34 // OSAtomic.h optimizations only used in 10.5 and later
35  #include <AvailabilityMacros.h>
36  #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
37  #include <libkern/OSAtomic.h>
38  #endif
39 
40 #elif defined( __GLIBCPP__ ) || defined( __GLIBCXX__ )
41  #if ( __GNUC__ > 4 ) || ( ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ >= 2 ) )
42  #include <ext/atomicity.h>
43  #else
44  #include <bits/atomicity.h>
45  #endif
46 
47 #endif
48 
49 namespace itk
50 {
51 #if defined( __GLIBCXX__ ) // g++ 3.4+
52 
53 using __gnu_cxx::__exchange_and_add;
54 
55 #endif
56 
60 TimeStamp *
63 {
64  return new Self;
65 }
66 
70 const TimeStamp &
71 TimeStamp::operator=( const Self & other )
72 {
73  this->m_ModifiedTime = other.m_ModifiedTime;
74  return *this;
75 }
76 
80 void
83 {
84  // Windows optimization
85 #if defined( WIN32 ) || defined( _WIN32 )
86  static LONG itkTimeStampTime = 0;
87  m_ModifiedTime = (ModifiedTimeType)InterlockedIncrement(&itkTimeStampTime);
88 
89  // Mac optimization
90 #elif defined( __APPLE__ ) && ( MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 )
91  #if __LP64__
92  // "m_ModifiedTime" is "unsigned long", a type that changess sizes
93  // depending on architecture. The atomic increment is safe, since it
94  // operates on a variable of the exact type needed. The cast does not
95  // change the size, but does change signedness, which is not ideal.
96  static volatile int64_t itkTimeStampTime = 0;
97  m_ModifiedTime = (ModifiedTimeType)OSAtomicIncrement64Barrier(&itkTimeStampTime);
98  #else
99  static volatile int32_t itkTimeStampTime = 0;
100  m_ModifiedTime = (ModifiedTimeType)OSAtomicIncrement32Barrier(&itkTimeStampTime);
101  #endif
102 
103 // gcc optimization
104 #elif defined( __GLIBCPP__ ) || defined( __GLIBCXX__ )
105  // We start from 1 since __exchange_and_add returns the old (non-incremented)
106  // value. This is not really necessary but will make the absolute value of the
107  // timestamp more consistent across platforms.
108  static volatile _Atomic_word itkTimeStampTime = 1;
109  m_ModifiedTime = (ModifiedTimeType)__exchange_and_add(&itkTimeStampTime, 1);
110 
111 // General case
112 #else
113 
116  static ModifiedTimeType itkTimeStampTime = 0;
117 
119  static SimpleFastMutexLock TimeStampMutex;
120 
121  TimeStampMutex.Lock();
122  m_ModifiedTime = ++itkTimeStampTime;
123  TimeStampMutex.Unlock();
124 #endif
125 }
126 } // end namespace itk

Generated at Sat Mar 8 2014 15:38:21 for Orfeo Toolbox with doxygen 1.8.3.1