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
|
/*
* Copyright (C) 2012 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* 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; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will 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.
*/
#include <unistd.h> /* for _POSIX_SPIN_LOCKS */
#include <pthread.h>
#include <library.h>
#include <debug.h>
#include "spinlock.h"
#include "mutex.h"
#include "lock_profiler.h"
#if defined(_POSIX_SPIN_LOCKS) && _POSIX_SPIN_LOCKS == -1
#undef _POSIX_SPIN_LOCKS
#endif
typedef struct private_spinlock_t private_spinlock_t;
/**
* private data
*/
struct private_spinlock_t {
/**
* public functions
*/
spinlock_t public;
#ifdef _POSIX_SPIN_LOCKS
/**
* wrapped pthread spin lock
*/
pthread_spinlock_t spinlock;
/**
* profiling info, if enabled (the mutex below does profile itself)
*/
lock_profile_t profile;
#else /* _POSIX_SPIN_LOCKS */
/**
* use a mutex if spin locks are not available
*/
mutex_t *mutex;
#endif /* _POSIX_SPIN_LOCKS */
};
METHOD(spinlock_t, lock, void,
private_spinlock_t *this)
{
#ifdef _POSIX_SPIN_LOCKS
int err;
profiler_start(&this->profile);
err = pthread_spin_lock(&this->spinlock);
if (err)
{
DBG1(DBG_LIB, "!!! SPIN LOCK LOCK ERROR: %s !!!", strerror(err));
}
profiler_end(&this->profile);
#else
this->mutex->lock(this->mutex);
#endif
}
METHOD(spinlock_t, unlock, void,
private_spinlock_t *this)
{
#ifdef _POSIX_SPIN_LOCKS
int err;
err = pthread_spin_unlock(&this->spinlock);
if (err)
{
DBG1(DBG_LIB, "!!! SPIN LOCK UNLOCK ERROR: %s !!!", strerror(err));
}
#else
this->mutex->unlock(this->mutex);
#endif
}
METHOD(spinlock_t, destroy, void,
private_spinlock_t *this)
{
#ifdef _POSIX_SPIN_LOCKS
profiler_cleanup(&this->profile);
pthread_spin_destroy(&this->spinlock);
#else
this->mutex->destroy(this->mutex);
#endif
free(this);
}
/*
* Described in header
*/
spinlock_t *spinlock_create()
{
private_spinlock_t *this;
INIT(this,
.public = {
.lock = _lock,
.unlock = _unlock,
.destroy = _destroy,
},
);
#ifdef _POSIX_SPIN_LOCKS
pthread_spin_init(&this->spinlock, PTHREAD_PROCESS_PRIVATE);
profiler_init(&this->profile);
#else
#warning Using mutexes as spin lock alternatives
this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
#endif
return &this->public;
}
|