~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/detail/lwm_win32.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
 
2
#define BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED
 
3
 
 
4
#if _MSC_VER >= 1020
 
5
#pragma once
 
6
#endif
 
7
 
 
8
//
 
9
//  boost/detail/lwm_win32.hpp
 
10
//
 
11
//  Copyright (c) 2002, 2003 Peter Dimov
 
12
//
 
13
//  Permission to copy, use, modify, sell and distribute this software
 
14
//  is granted provided this copyright notice appears in all copies.
 
15
//  This software is provided "as is" without express or implied
 
16
//  warranty, and with no claim as to its suitability for any purpose.
 
17
//
 
18
 
 
19
#ifdef BOOST_USE_WINDOWS_H
 
20
#  include <windows.h>
 
21
#endif
 
22
 
 
23
#ifdef __BORLANDC__
 
24
# pragma warn -8027     // Functions containing while are not expanded inline
 
25
#endif
 
26
 
 
27
namespace boost
 
28
{
 
29
 
 
30
namespace detail
 
31
{
 
32
 
 
33
#ifndef BOOST_USE_WINDOWS_H
 
34
 
 
35
#ifdef _WIN64
 
36
 
 
37
// Intel 6.0 on Win64 version, posted by Tim Fenders to [boost-users]
 
38
 
 
39
extern "C" long_type __cdecl _InterlockedExchange(long volatile *, long);
 
40
 
 
41
#pragma intrinsic(_InterlockedExchange)
 
42
 
 
43
inline long InterlockedExchange(long volatile* lp, long l)
 
44
{
 
45
    return _InterlockedExchange(lp, l);
 
46
}
 
47
 
 
48
#else  // _WIN64
 
49
 
 
50
extern "C" __declspec(dllimport) long __stdcall InterlockedExchange(long volatile *, long);
 
51
 
 
52
#endif // _WIN64
 
53
 
 
54
extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
 
55
 
 
56
#endif // #ifndef BOOST_USE_WINDOWS_H
 
57
 
 
58
class lightweight_mutex
 
59
{
 
60
private:
 
61
 
 
62
    long l_;
 
63
 
 
64
    lightweight_mutex(lightweight_mutex const &);
 
65
    lightweight_mutex & operator=(lightweight_mutex const &);
 
66
 
 
67
public:
 
68
 
 
69
    lightweight_mutex(): l_(0)
 
70
    {
 
71
    }
 
72
 
 
73
    class scoped_lock;
 
74
    friend class scoped_lock;
 
75
 
 
76
    class scoped_lock
 
77
    {
 
78
    private:
 
79
 
 
80
        lightweight_mutex & m_;
 
81
 
 
82
        scoped_lock(scoped_lock const &);
 
83
        scoped_lock & operator=(scoped_lock const &);
 
84
 
 
85
    public:
 
86
 
 
87
        explicit scoped_lock(lightweight_mutex & m): m_(m)
 
88
        {
 
89
            while( InterlockedExchange(&m_.l_, 1) )
 
90
            {
 
91
                // Note: changed to Sleep(1) from Sleep(0).
 
92
                // According to MSDN, Sleep(0) will never yield
 
93
                // to a lower-priority thread, whereas Sleep(1)
 
94
                // will. Performance seems not to be affected.
 
95
 
 
96
                Sleep(1);
 
97
            }
 
98
        }
 
99
 
 
100
        ~scoped_lock()
 
101
        {
 
102
            InterlockedExchange(&m_.l_, 0);
 
103
 
 
104
            // Note: adding a yield here will make
 
105
            // the spinlock more fair and will increase the overall
 
106
            // performance of some applications substantially in
 
107
            // high contention situations, but will penalize the
 
108
            // low contention / single thread case up to 5x
 
109
        }
 
110
    };
 
111
};
 
112
 
 
113
} // namespace detail
 
114
 
 
115
} // namespace boost
 
116
 
 
117
#ifdef __BORLANDC__
 
118
# pragma warn .8027     // Functions containing while are not expanded inline
 
119
#endif
 
120
 
 
121
#endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED