File: qlock.c

package info (click to toggle)
xfsdump 3.1.9%2B0
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,932 kB
  • sloc: ansic: 45,863; sh: 3,227; makefile: 545
file content (249 lines) | stat: -rw-r--r-- 4,950 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <assert.h>

#include "config.h"

#include "types.h"
#include "qlock.h"
#include "mlog.h"

struct qlock {
	ix_t ql_ord;
		/* ordinal position of this lock
		 */
	pthread_mutex_t ql_mutex;
};

typedef struct  qlock qlock_t;
	/* internal qlock
	 */

typedef size_t ordmap_t;
	/* bitmap of ordinals. used to track what ordinals have
	 * been allocated.
	 */

#define ORDMAX					(8 * sizeof(ordmap_t))
	/* ordinals must fit into a wordsize bitmap
	 */

static ordmap_t qlock_ordalloced;
	/* to enforce allocation of only one lock to each ordinal value
	 */

static __thread ordmap_t thread_ordmap;
	/* holds the ordmap for each thread
	 */

#define QLOCK_ORDMAP_SET(ordmap, ord)	(ordmap |= 1U << ord)
	/* sets the ordinal bit in an ordmap
	 */

#define QLOCK_ORDMAP_CLR(ordmap, ord)	(ordmap &= ~(1U << ord))
	/* clears the ordinal bit in an ordmap
	 */

#define QLOCK_ORDMAP_GET(ordmap, ord)	(ordmap & (1U << ord))
	/* checks if ordinal set in ordmap
	 */

#define QLOCK_ORDMAP_CHK(ordmap, ord)	(ordmap & ((1U << ord) - 1U))
	/* checks if any bits less than ord are set in the ordmap
	 */


qlockh_t
qlock_alloc(ix_t ord)
{
	qlock_t *qlockp;

	/* verify the ordinal is not already taken, and mark as taken
	 */
	assert(!QLOCK_ORDMAP_GET(qlock_ordalloced, ord));
	QLOCK_ORDMAP_SET(qlock_ordalloced, ord);

	/* allocate lock memory
	 */
	qlockp = (qlock_t *)calloc(1, sizeof(qlock_t));
	assert(qlockp);

	/* initialize the mutex
	 */
	pthread_mutex_init(&qlockp->ql_mutex, NULL);

	/* assign the ordinal position
	 */
	qlockp->ql_ord = ord;

	return (qlockh_t)qlockp;
}

void
qlock_lock(qlockh_t qlockh)
{
	qlock_t *qlockp = (qlock_t *)qlockh;
	pthread_t tid;
	/* REFERENCED */
	int rval;

	/* get the caller's tid
	 */
	tid = pthread_self();

	/* assert that this lock not already held by this thread
	 */
	if (QLOCK_ORDMAP_GET(thread_ordmap, qlockp->ql_ord)) {
		mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_NOLOCK,
		      _("lock already held: tid %lu ord %d map %x\n"),
		      tid,
		      qlockp->ql_ord,
		      thread_ordmap);
	}
	assert(!QLOCK_ORDMAP_GET(thread_ordmap, qlockp->ql_ord));

	/* assert that no locks with a lesser ordinal are held by this thread
	 */
	if (QLOCK_ORDMAP_CHK(thread_ordmap, qlockp->ql_ord)) {
		mlog(MLOG_NORMAL | MLOG_WARNING | MLOG_NOLOCK,
		      _("lock ordinal violation: tid %lu ord %d map %x\n"),
		      tid,
		      qlockp->ql_ord,
		      thread_ordmap);
	}
	assert(!QLOCK_ORDMAP_CHK(thread_ordmap, qlockp->ql_ord));

	/* acquire the lock
	 */
	rval = pthread_mutex_lock(&qlockp->ql_mutex);
	assert(!rval);

	/* add ordinal to this threads ordmap
	 */
	QLOCK_ORDMAP_SET(thread_ordmap, qlockp->ql_ord);
}

void
qlock_unlock(qlockh_t qlockh)
{
	qlock_t *qlockp = (qlock_t *)qlockh;
	/* REFERENCED */
	int rval;

	/* verify lock is held by this thread
	 */
	assert(QLOCK_ORDMAP_GET(thread_ordmap, qlockp->ql_ord));

	/* clear lock's ord from thread's ord map
	 */
	QLOCK_ORDMAP_CLR(thread_ordmap, qlockp->ql_ord);

	/* release the lock
	 */
	rval = pthread_mutex_unlock(&qlockp->ql_mutex);
	assert(!rval);
}

qsemh_t
qsem_alloc(ix_t cnt)
{
	sem_t *semp;
	int rval;

	/* allocate a semaphore
	 */
	semp = (sem_t *)calloc(1, sizeof(sem_t));
	assert(semp);

	/* initialize the semaphore
	 */
	rval = sem_init(semp, 0, cnt);
	assert(!rval);

	return (qsemh_t)semp;
}

void
qsem_free(qsemh_t qsemh)
{
	sem_t *semp = (sem_t *)qsemh;
	int rval;

	/* destroy the mutex and condition
	 */
	rval = sem_destroy(semp);
	assert(!rval);

	/* free the semaphore
	 */
	free(semp);
}

void
qsemP(qsemh_t qsemh)
{
	sem_t *semp = (sem_t *)qsemh;
	int rval;

	/* "P" the semaphore
	 */
	rval = sem_wait(semp);
	assert(!rval);
}

void
qsemV(qsemh_t qsemh)
{
	sem_t *semp = (sem_t *)qsemh;
	int rval;

	/* "V" the semaphore
	 */
	rval = sem_post(semp);
	assert(!rval);
}

bool_t
qsemPwouldblock(qsemh_t qsemh)
{
	sem_t *semp = (sem_t *)qsemh;
	int count;
	int rval;

	rval = sem_getvalue(semp, &count);
	assert(!rval);

	return count <= 0 ? BOOL_TRUE : BOOL_FALSE;
}

size_t
qsemPavail(qsemh_t qsemh)
{
	sem_t *semp = (sem_t *)qsemh;
	int count;
	int rval;

	rval = sem_getvalue(semp, &count);
	assert(!rval);

	return count < 0 ? 0 : count;
}