~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// posix_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_POSIX_MUTEX_HPP
 
12
#define ASIO_DETAIL_POSIX_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_HAS_PTHREADS)
 
25
 
 
26
#include "asio/detail/push_options.hpp"
 
27
#include <boost/throw_exception.hpp>
 
28
#include <pthread.h>
 
29
#include "asio/detail/pop_options.hpp"
 
30
 
 
31
#include "asio/error.hpp"
 
32
#include "asio/system_error.hpp"
 
33
#include "asio/detail/noncopyable.hpp"
 
34
#include "asio/detail/scoped_lock.hpp"
 
35
 
 
36
namespace asio {
 
37
namespace detail {
 
38
 
 
39
class posix_event;
 
40
 
 
41
class posix_mutex
 
42
  : private noncopyable
 
43
{
 
44
public:
 
45
  typedef asio::detail::scoped_lock<posix_mutex> scoped_lock;
 
46
 
 
47
  // Constructor.
 
48
  posix_mutex()
 
49
  {
 
50
    int error = ::pthread_mutex_init(&mutex_, 0);
 
51
    if (error != 0)
 
52
    {
 
53
      asio::system_error e(
 
54
          asio::error_code(error,
 
55
            asio::error::get_system_category()),
 
56
          "mutex");
 
57
      boost::throw_exception(e);
 
58
    }
 
59
  }
 
60
 
 
61
  // Destructor.
 
62
  ~posix_mutex()
 
63
  {
 
64
    ::pthread_mutex_destroy(&mutex_);
 
65
  }
 
66
 
 
67
  // Lock the mutex.
 
68
  void lock()
 
69
  {
 
70
    int error = ::pthread_mutex_lock(&mutex_);
 
71
    if (error != 0)
 
72
    {
 
73
      asio::system_error e(
 
74
          asio::error_code(error,
 
75
            asio::error::get_system_category()),
 
76
          "mutex");
 
77
      boost::throw_exception(e);
 
78
    }
 
79
  }
 
80
 
 
81
  // Unlock the mutex.
 
82
  void unlock()
 
83
  {
 
84
    int error = ::pthread_mutex_unlock(&mutex_);
 
85
    if (error != 0)
 
86
    {
 
87
      asio::system_error e(
 
88
          asio::error_code(error,
 
89
            asio::error::get_system_category()),
 
90
          "mutex");
 
91
      boost::throw_exception(e);
 
92
    }
 
93
  }
 
94
 
 
95
private:
 
96
  friend class posix_event;
 
97
  ::pthread_mutex_t mutex_;
 
98
};
 
99
 
 
100
} // namespace detail
 
101
} // namespace asio
 
102
 
 
103
#endif // defined(BOOST_HAS_PTHREADS)
 
104
 
 
105
#include "asio/detail/pop_options.hpp"
 
106
 
 
107
#endif // ASIO_DETAIL_POSIX_MUTEX_HPP