~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to src/mutex.hpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-06-04 21:21:09 UTC
  • Revision ID: package-import@ubuntu.com-20120604212109-b7b3m0rn21o8oo2q
Tags: upstream-3.1.0~beta+dfsg
ImportĀ upstreamĀ versionĀ 3.1.0~beta+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2010-2011 250bpm s.r.o.
 
3
    Copyright (c) 2007-2009 iMatix Corporation
 
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
 
5
 
 
6
    This file is part of 0MQ.
 
7
 
 
8
    0MQ is free software; you can redistribute it and/or modify it under
 
9
    the terms of the GNU Lesser General Public License as published by
 
10
    the Free Software Foundation; either version 3 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    0MQ is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public License
 
19
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#ifndef __ZMQ_MUTEX_HPP_INCLUDED__
 
23
#define __ZMQ_MUTEX_HPP_INCLUDED__
 
24
 
 
25
#include "platform.hpp"
 
26
#include "err.hpp"
 
27
 
 
28
//  Mutex class encapsulates OS mutex in a platform-independent way.
 
29
 
 
30
#ifdef ZMQ_HAVE_WINDOWS
 
31
 
 
32
#include "windows.hpp"
 
33
 
 
34
namespace zmq
 
35
{
 
36
 
 
37
    class mutex_t
 
38
    {
 
39
    public:
 
40
        inline mutex_t ()
 
41
        {
 
42
            InitializeCriticalSection (&cs);
 
43
        }
 
44
 
 
45
        inline ~mutex_t ()
 
46
        {
 
47
            DeleteCriticalSection (&cs);
 
48
        }
 
49
 
 
50
        inline void lock ()
 
51
        {
 
52
            EnterCriticalSection (&cs);
 
53
        }
 
54
 
 
55
        inline void unlock ()
 
56
        {
 
57
            LeaveCriticalSection (&cs);
 
58
        }
 
59
 
 
60
    private:
 
61
 
 
62
        CRITICAL_SECTION cs;
 
63
 
 
64
        //  Disable copy construction and assignment.
 
65
        mutex_t (const mutex_t&);
 
66
        void operator = (const mutex_t&);
 
67
    };
 
68
 
 
69
}
 
70
 
 
71
#else
 
72
 
 
73
#include <pthread.h>
 
74
 
 
75
namespace zmq
 
76
{
 
77
 
 
78
    class mutex_t
 
79
    {
 
80
    public:
 
81
        inline mutex_t ()
 
82
        {
 
83
            int rc = pthread_mutex_init (&mutex, NULL);
 
84
            if (rc)
 
85
                posix_assert (rc);
 
86
        }
 
87
 
 
88
        inline ~mutex_t ()
 
89
        {
 
90
            int rc = pthread_mutex_destroy (&mutex);
 
91
            if (rc)
 
92
                posix_assert (rc);
 
93
        }
 
94
 
 
95
        inline void lock ()
 
96
        {
 
97
            int rc = pthread_mutex_lock (&mutex);
 
98
            if (rc)
 
99
                posix_assert (rc);
 
100
        }
 
101
 
 
102
        inline void unlock ()
 
103
        {
 
104
            int rc = pthread_mutex_unlock (&mutex);
 
105
            if (rc)
 
106
                posix_assert (rc);
 
107
        }
 
108
 
 
109
    private:
 
110
 
 
111
        pthread_mutex_t mutex;
 
112
 
 
113
        // Disable copy construction and assignment.
 
114
        mutex_t (const mutex_t&);
 
115
        const mutex_t &operator = (const mutex_t&);
 
116
    };
 
117
 
 
118
}
 
119
 
 
120
#endif
 
121
 
 
122
#endif