~cmiller/ubuntu/quantal/deluge/fix-parameter-move-storage

« back to all changes in this revision

Viewing changes to libtorrent/include/asio/detail/win_mutex.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2009-11-13 02:39:45 UTC
  • mfrom: (4.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091113023945-te1bybo2912ejzuc
Tags: 1.2.0~rc3-4
* debian/control: bump build-dep on python-setuptools to (>= 0.6c9).
* debian/patches:
  - 25_r5921_fastresume_files.patch
    new, should fix problems with fresh configs;
  - 30_r5931_ipc_lockfile.patch:
    new, should fix an issue where Deluge will fail to start if there is a
    stale ipc lockfile. (Closes: #555849)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// win_mutex.hpp
3
 
// ~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6
 
//
7
 
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
 
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
 
//
10
 
 
11
 
#ifndef ASIO_DETAIL_WIN_MUTEX_HPP
12
 
#define ASIO_DETAIL_WIN_MUTEX_HPP
13
 
 
14
 
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15
 
# pragma once
16
 
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
 
 
18
 
#include "asio/detail/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <boost/config.hpp>
22
 
#include "asio/detail/pop_options.hpp"
23
 
 
24
 
#if defined(BOOST_WINDOWS)
25
 
 
26
 
#include "asio/error.hpp"
27
 
#include "asio/system_error.hpp"
28
 
#include "asio/detail/noncopyable.hpp"
29
 
#include "asio/detail/socket_types.hpp"
30
 
#include "asio/detail/scoped_lock.hpp"
31
 
 
32
 
#include "asio/detail/push_options.hpp"
33
 
#include <boost/throw_exception.hpp>
34
 
#include "asio/detail/pop_options.hpp"
35
 
 
36
 
namespace asio {
37
 
namespace detail {
38
 
 
39
 
class win_mutex
40
 
  : private noncopyable
41
 
{
42
 
public:
43
 
  typedef asio::detail::scoped_lock<win_mutex> scoped_lock;
44
 
 
45
 
  // Constructor.
46
 
  win_mutex()
47
 
  {
48
 
    int error = do_init();
49
 
    if (error != 0)
50
 
    {
51
 
      asio::system_error e(
52
 
          asio::error_code(error,
53
 
            asio::error::get_system_category()),
54
 
          "mutex");
55
 
      boost::throw_exception(e);
56
 
    }
57
 
  }
58
 
 
59
 
  // Destructor.
60
 
  ~win_mutex()
61
 
  {
62
 
    ::DeleteCriticalSection(&crit_section_);
63
 
  }
64
 
 
65
 
  // Lock the mutex.
66
 
  void lock()
67
 
  {
68
 
    int error = do_lock();
69
 
    if (error != 0)
70
 
    {
71
 
      asio::system_error e(
72
 
          asio::error_code(error,
73
 
            asio::error::get_system_category()),
74
 
          "mutex");
75
 
      boost::throw_exception(e);
76
 
    }
77
 
  }
78
 
 
79
 
  // Unlock the mutex.
80
 
  void unlock()
81
 
  {
82
 
    ::LeaveCriticalSection(&crit_section_);
83
 
  }
84
 
 
85
 
private:
86
 
  // Initialisation must be performed in a separate function to the constructor
87
 
  // since the compiler does not support the use of structured exceptions and
88
 
  // C++ exceptions in the same function.
89
 
  int do_init()
90
 
  {
91
 
#if defined(__MINGW32__)
92
 
    // Not sure if MinGW supports structured exception handling, so for now
93
 
    // we'll just call the Windows API and hope.
94
 
    ::InitializeCriticalSection(&crit_section_);
95
 
    return 0;
96
 
#else
97
 
    __try
98
 
    {
99
 
      ::InitializeCriticalSection(&crit_section_);
100
 
    }
101
 
    __except(GetExceptionCode() == STATUS_NO_MEMORY
102
 
        ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
103
 
    {
104
 
      return ERROR_OUTOFMEMORY;
105
 
    }
106
 
 
107
 
    return 0;
108
 
#endif
109
 
  }
110
 
 
111
 
  // Locking must be performed in a separate function to lock() since the
112
 
  // compiler does not support the use of structured exceptions and C++
113
 
  // exceptions in the same function.
114
 
  int do_lock()
115
 
  {
116
 
#if defined(__MINGW32__)
117
 
    // Not sure if MinGW supports structured exception handling, so for now
118
 
    // we'll just call the Windows API and hope.
119
 
    ::EnterCriticalSection(&crit_section_);
120
 
    return 0;
121
 
#else
122
 
    __try
123
 
    {
124
 
      ::EnterCriticalSection(&crit_section_);
125
 
    }
126
 
    __except(GetExceptionCode() == STATUS_INVALID_HANDLE
127
 
        || GetExceptionCode() == STATUS_NO_MEMORY
128
 
        ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
129
 
    {
130
 
      if (GetExceptionCode() == STATUS_NO_MEMORY)
131
 
        return ERROR_OUTOFMEMORY;
132
 
      return ERROR_INVALID_HANDLE;
133
 
    }
134
 
 
135
 
    return 0;
136
 
#endif
137
 
  }
138
 
 
139
 
  ::CRITICAL_SECTION crit_section_;
140
 
};
141
 
 
142
 
} // namespace detail
143
 
} // namespace asio
144
 
 
145
 
#endif // defined(BOOST_WINDOWS)
146
 
 
147
 
#include "asio/detail/pop_options.hpp"
148
 
 
149
 
#endif // ASIO_DETAIL_WIN_MUTEX_HPP