~ubuntu-branches/ubuntu/karmic/openipmi/karmic

« back to all changes in this revision

Viewing changes to utils/locks.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-07-04 21:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20050704212917-igddk5jawjmhrlay
Tags: upstream-2.0.1
Import upstream version 2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * locks.c
 
3
 *
 
4
 * Code for abstracting locks in IPMI
 
5
 *
 
6
 * Author: MontaVista Software, Inc.
 
7
 *         Corey Minyard <minyard@mvista.com>
 
8
 *         source@mvista.com
 
9
 *
 
10
 * Copyright 2004 MontaVista Software Inc.
 
11
 *
 
12
 *  This program is free software; you can redistribute it and/or
 
13
 *  modify it under the terms of the GNU Lesser General Public License
 
14
 *  as published by the Free Software Foundation; either version 2 of
 
15
 *  the License, or (at your option) any later version.
 
16
 *
 
17
 *
 
18
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
19
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
20
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
21
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
22
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
23
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 
24
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
25
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 
26
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
27
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 *
 
29
 *  You should have received a copy of the GNU Lesser General Public
 
30
 *  License along with this program; if not, write to the Free
 
31
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
32
 */
 
33
 
 
34
#include <stdlib.h>
 
35
#include <errno.h>
 
36
 
 
37
#include <OpenIPMI/ipmi_debug.h>
 
38
 
 
39
#include <OpenIPMI/internal/ipmi_malloc.h>
 
40
#include <OpenIPMI/internal/ipmi_locks.h>
 
41
 
 
42
struct ipmi_lock_s
 
43
{
 
44
    os_hnd_lock_t *ll_lock;
 
45
    os_handler_t  *os_hnd;
 
46
};
 
47
 
 
48
int __ipmi_debug_locks = 0;
 
49
 
 
50
int
 
51
ipmi_create_lock_os_hnd(os_handler_t *os_hnd, ipmi_lock_t **new_lock)
 
52
{
 
53
    ipmi_lock_t *lock;
 
54
    int         rv;
 
55
 
 
56
    lock = ipmi_mem_alloc(sizeof(*lock));
 
57
    if (!lock)
 
58
        return ENOMEM;
 
59
 
 
60
    lock->os_hnd = os_hnd;
 
61
    if (lock->os_hnd && lock->os_hnd->create_lock) {
 
62
        rv = lock->os_hnd->create_lock(lock->os_hnd, &(lock->ll_lock));
 
63
        if (rv) {
 
64
            ipmi_mem_free(lock);
 
65
            return rv;
 
66
        }
 
67
    } else {
 
68
        lock->ll_lock = NULL;
 
69
    }
 
70
 
 
71
    *new_lock = lock;
 
72
 
 
73
    return 0;
 
74
}
 
75
 
 
76
void ipmi_destroy_lock(ipmi_lock_t *lock)
 
77
{
 
78
    if (lock->ll_lock)
 
79
        lock->os_hnd->destroy_lock(lock->os_hnd, lock->ll_lock);
 
80
    ipmi_mem_free(lock);
 
81
}
 
82
 
 
83
void ipmi_lock(ipmi_lock_t *lock)
 
84
{
 
85
    if (lock->ll_lock)
 
86
        lock->os_hnd->lock(lock->os_hnd, lock->ll_lock);
 
87
}
 
88
 
 
89
void ipmi_unlock(ipmi_lock_t *lock)
 
90
{
 
91
    if (lock->ll_lock)
 
92
        lock->os_hnd->unlock(lock->os_hnd, lock->ll_lock);
 
93
}
 
94
 
 
95
struct ipmi_rwlock_s
 
96
{
 
97
    os_hnd_rwlock_t *ll_lock;
 
98
    os_handler_t  *os_hnd;
 
99
};
 
100
 
 
101
int
 
102
ipmi_create_rwlock_os_hnd(os_handler_t *os_hnd, ipmi_rwlock_t **new_lock)
 
103
{
 
104
    ipmi_rwlock_t *lock;
 
105
    int         rv;
 
106
 
 
107
    lock = ipmi_mem_alloc(sizeof(*lock));
 
108
    if (!lock)
 
109
        return ENOMEM;
 
110
 
 
111
    lock->os_hnd = os_hnd;
 
112
    if (lock->os_hnd && lock->os_hnd->create_lock) {
 
113
        rv = lock->os_hnd->create_rwlock(lock->os_hnd, &(lock->ll_lock));
 
114
        if (rv) {
 
115
            ipmi_mem_free(lock);
 
116
            return rv;
 
117
        }
 
118
    } else {
 
119
        lock->ll_lock = NULL;
 
120
    }
 
121
 
 
122
    *new_lock = lock;
 
123
 
 
124
    return 0;
 
125
}
 
126
 
 
127
void ipmi_destroy_rwlock(ipmi_rwlock_t *lock)
 
128
{
 
129
    if (lock->ll_lock)
 
130
        lock->os_hnd->destroy_rwlock(lock->os_hnd, lock->ll_lock);
 
131
    ipmi_mem_free(lock);
 
132
}
 
133
 
 
134
void ipmi_rwlock_read_lock(ipmi_rwlock_t *lock)
 
135
{
 
136
    if (lock->ll_lock)
 
137
        lock->os_hnd->read_lock(lock->os_hnd, lock->ll_lock);
 
138
}
 
139
 
 
140
void ipmi_rwlock_read_unlock(ipmi_rwlock_t *lock)
 
141
{
 
142
    if (lock->ll_lock)
 
143
        lock->os_hnd->read_unlock(lock->os_hnd, lock->ll_lock);
 
144
}
 
145
 
 
146
void ipmi_rwlock_write_lock(ipmi_rwlock_t *lock)
 
147
{
 
148
    if (lock->ll_lock)
 
149
        lock->os_hnd->write_lock(lock->os_hnd, lock->ll_lock);
 
150
}
 
151
 
 
152
void ipmi_rwlock_write_unlock(ipmi_rwlock_t *lock)
 
153
{
 
154
    if (lock->ll_lock)
 
155
        lock->os_hnd->write_unlock(lock->os_hnd, lock->ll_lock);
 
156
}
 
157
 
 
158
#ifdef IPMI_CHECK_LOCKS
 
159
/* Set a breakpoint here to detect locking errors. */
 
160
void
 
161
ipmi_report_lock_error(os_handler_t *handler, char *str)
 
162
{
 
163
    handler->log(handler, IPMI_LOG_WARNING, "%s", str);
 
164
}
 
165
 
 
166
void
 
167
ipmi_check_lock(ipmi_lock_t *lock, char *str)
 
168
{
 
169
    if ((!DEBUG_LOCKS) || (!lock) || (!lock->ll_lock))
 
170
        return;
 
171
 
 
172
    if (! lock->os_hnd->is_locked(lock->os_hnd, lock->ll_lock))
 
173
        IPMI_REPORT_LOCK_ERROR(lock->os_hnd, str);
 
174
}
 
175
#endif