blob: 7bf0c36b5192478fa58ee1efabf3bc1f5dca7a90 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __PJPP_LOCK_HPP__
21#define __PJPP_LOCK_HPP__
22
23#include <pj++/types.hpp>
24#include <pj/lock.h>
25#include <pj++/pool.hpp>
26
27//////////////////////////////////////////////////////////////////////////////
28// Lock object.
29//
30class Pj_Lock : public Pj_Object
31{
32public:
33 //
34 // Constructor.
35 //
36 explicit Pj_Lock(pj_lock_t *lock)
37 : lock_(lock)
38 {
39 }
40
41 //
42 // Destructor.
43 //
44 ~Pj_Lock()
45 {
46 if (lock_)
47 pj_lock_destroy(lock_);
48 }
49
50 //
51 // Get pjlib compatible lock object.
52 //
53 pj_lock_t *pj_lock_t_()
54 {
55 return lock_;
56 }
57
58 //
59 // acquire lock.
60 //
61 pj_status_t acquire()
62 {
63 return pj_lock_acquire(lock_);
64 }
65
66 //
67 // release lock,.
68 //
69 pj_status_t release()
70 {
71 return pj_lock_release(lock_);
72 }
73
74protected:
75 pj_lock_t *lock_;
76};
77
78
79//////////////////////////////////////////////////////////////////////////////
80// Null lock object.
81//
82class Pj_Null_Lock : public Pj_Lock
83{
84public:
85 //
86 // Default constructor.
87 //
88 explicit Pj_Null_Lock(Pj_Pool *pool, const char *name = NULL)
89 : Pj_Lock(NULL)
90 {
91 pj_lock_create_null_mutex(pool->pool_(), name, &lock_);
92 }
93};
94
95//////////////////////////////////////////////////////////////////////////////
96// Simple mutex lock object.
97//
98class Pj_Simple_Mutex_Lock : public Pj_Lock
99{
100public:
101 //
102 // Default constructor.
103 //
104 explicit Pj_Simple_Mutex_Lock(Pj_Pool *pool, const char *name = NULL)
105 : Pj_Lock(NULL)
106 {
107 pj_lock_create_simple_mutex(pool->pool_(), name, &lock_);
108 }
109};
110
111//////////////////////////////////////////////////////////////////////////////
112// Recursive mutex lock object.
113//
114class Pj_Recursive_Mutex_Lock : public Pj_Lock
115{
116public:
117 //
118 // Default constructor.
119 //
120 explicit Pj_Recursive_Mutex_Lock(Pj_Pool *pool, const char *name = NULL)
121 : Pj_Lock(NULL)
122 {
123 pj_lock_create_recursive_mutex(pool->pool_(), name, &lock_);
124 }
125};
126
127//////////////////////////////////////////////////////////////////////////////
128// Semaphore lock object.
129//
130class Pj_Semaphore_Lock : public Pj_Lock
131{
132public:
133 //
134 // Default constructor.
135 //
136 explicit Pj_Semaphore_Lock(Pj_Pool *pool,
137 unsigned max=PJ_MAXINT32,
138 unsigned initial=0,
139 const char *name=NULL)
140 : Pj_Lock(NULL)
141 {
142 pj_lock_create_semaphore(pool->pool_(), name, initial, max, &lock_);
143 }
144};
145
146
147
148#endif /* __PJPP_LOCK_HPP__ */
149