-
Notifications
You must be signed in to change notification settings - Fork 33
/
locks.c
139 lines (119 loc) · 4.02 KB
/
locks.c
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
/* nbdkit
* Copyright Red Hat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Red Hat nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include "internal.h"
/* Note that the plugin's thread model cannot change after being
* loaded, so caching it here is safe.
*/
unsigned thread_model = -1;
static pthread_mutex_t connection_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t all_requests_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_rwlock_t unload_prevention_lock = PTHREAD_RWLOCK_INITIALIZER;
/* Map thread model to string; use only from single-threaded context */
const char *
name_of_thread_model (int model)
{
static char buf[] = "-2147483648 # unknown thread model!";
switch (model) {
case NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS:
return "serialize_connections";
case NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS:
return "serialize_all_requests";
case NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS:
return "serialize_requests";
case NBDKIT_THREAD_MODEL_PARALLEL:
return "parallel";
}
snprintf (buf, sizeof buf, "%d # unknown thread model!", model);
return buf;
}
void
lock_init_thread_model (void)
{
thread_model = top->thread_model (top);
debug ("using thread model: %s", name_of_thread_model (thread_model));
assert (thread_model <= NBDKIT_THREAD_MODEL_PARALLEL);
}
void
lock_connection (void)
{
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS &&
pthread_mutex_lock (&connection_lock))
abort ();
}
void
unlock_connection (void)
{
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS &&
pthread_mutex_unlock (&connection_lock))
abort ();
}
void
lock_request (void)
{
struct connection *conn = threadlocal_get_conn ();
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS &&
pthread_mutex_lock (&all_requests_lock))
abort ();
if (conn && thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS &&
pthread_mutex_lock (&conn->request_lock))
abort ();
if (pthread_rwlock_rdlock (&unload_prevention_lock))
abort ();
}
void
unlock_request ()
{
struct connection *conn = threadlocal_get_conn ();
if (pthread_rwlock_unlock (&unload_prevention_lock))
abort ();
if (conn && thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS &&
pthread_mutex_unlock (&conn->request_lock))
abort ();
if (thread_model <= NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS &&
pthread_mutex_unlock (&all_requests_lock))
abort ();
}
void
lock_unload (void)
{
if (pthread_rwlock_wrlock (&unload_prevention_lock))
abort ();
}
void
unlock_unload (void)
{
if (pthread_rwlock_unlock (&unload_prevention_lock))
abort ();
}