-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_alloc.cpp
136 lines (109 loc) · 4.21 KB
/
lock_alloc.cpp
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
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (April 2006)
Copyright (c) 2006, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
//******************************************************************
//******************************************************************
#include <assert.h>
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
#include "osd_abstract.h"
#include "fmttypes.h"
#include "log.h"
//******** These are for the directory splitting ****
#define LOCK_BITS 8
#define LOCK_BITMASK 0x00FF
#define LOCK_MAX 256
#define id_slot(id) (id & LOCK_BITMASK)
pthread_mutex_t lock_table[LOCK_MAX];
//******************************************************************
// lock_osd_id - Locks the ID. Blocks until a lock can be acquired
//******************************************************************
void lock_osd_id(osd_id_t id)
{
pthread_mutex_lock(&(lock_table[id_slot(id)]));
}
//******************************************************************
// unlock_osd_id - Unlocks the ID.
//******************************************************************
void unlock_osd_id(osd_id_t id)
{
pthread_mutex_unlock(&(lock_table[id_slot(id)]));
}
//******************************************************************
// lock_osd_id_pair - Locks a pair of IDs. Blocks until a lock can be acquired
//******************************************************************
void lock_osd_id_pair(osd_id_t id1, osd_id_t id2)
{
int slot1 = id_slot(id1);
int slot2 = id_slot(id2);
if (slot1 == slot2) {
pthread_mutex_lock(&(lock_table[slot1]));
} else if (slot1 > slot2) {
pthread_mutex_lock(&(lock_table[slot1]));
pthread_mutex_lock(&(lock_table[slot2]));
} else {
pthread_mutex_lock(&(lock_table[slot2]));
pthread_mutex_lock(&(lock_table[slot1]));
}
}
//******************************************************************
// unlock_osd_id_pair - Unlocks the ID pair.
//******************************************************************
void unlock_osd_id_pair(osd_id_t id1, osd_id_t id2)
{
int slot1 = id_slot(id1);
int slot2 = id_slot(id2);
if (slot1 == slot2) {
pthread_mutex_unlock(&(lock_table[slot1]));
} else if (slot1 > slot2) {
pthread_mutex_unlock(&(lock_table[slot2]));
pthread_mutex_unlock(&(lock_table[slot1]));
} else {
pthread_mutex_unlock(&(lock_table[slot1]));
pthread_mutex_unlock(&(lock_table[slot2]));
}
}
//******************************************************************
// lock_alloc_init - Initializes the allocation locking routines
//******************************************************************
void lock_alloc_init()
{
int i;
for (i=0; i<LOCK_MAX; i++) {
pthread_mutex_init(&(lock_table[i]), NULL);
}
}
//******************************************************************
// lock_alloc_destroy - Destroys the allocation locking data
//******************************************************************
void lock_alloc_destroy()
{
int i;
for (i=0; i<LOCK_MAX; i++) {
pthread_mutex_destroy(&(lock_table[i]));
}
}