-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathpthread_spinlock.c
128 lines (110 loc) · 2.83 KB
/
pthread_spinlock.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
/*
*
* Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
* All rights reserved.
*
* This file is part of libtrace.
*
* This code has been developed by the University of Waikato WAND
* research group. For further information please see http://www.wand.net.nz/
*
* libtrace is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* libtrace 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifdef __APPLE__
# include "pthread_spinlock.h"
/* Apple 10.12 deprecates spin locks as they cause issues with their scheduler
* https://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
*/
# if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
int pthread_spin_lock(pthread_spinlock_t *lock)
{
if (lock == NULL)
return EINVAL;
os_unfair_lock_lock(lock);
return 0;
}
int pthread_spin_trylock(pthread_spinlock_t *lock)
{
if (lock == NULL)
return EINVAL;
if (os_unfair_lock_trylock(lock)) {
return 0;
} else {
return EBUSY;
}
}
int pthread_spin_unlock(pthread_spinlock_t *lock)
{
if (lock == NULL)
return EINVAL;
os_unfair_lock_unlock(lock);
return 0;
}
int pthread_spin_destroy(pthread_spinlock_t *lock)
{
(void)lock;
return 0;
}
int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
{
if (lock == NULL)
return EINVAL;
*lock = OS_UNFAIR_LOCK_INIT;
(void)pshared;
return 0;
}
# else
int pthread_spin_lock(pthread_spinlock_t *lock)
{
if (lock == NULL)
return EINVAL;
OSSpinLockLock(lock);
return 0;
}
int pthread_spin_trylock(pthread_spinlock_t *lock)
{
if (lock == NULL)
return EINVAL;
if (OSSpinLockTry(lock)) {
return 0;
} else {
return EBUSY;
}
}
int pthread_spin_unlock(pthread_spinlock_t *lock)
{
if (lock == NULL)
return EINVAL;
OSSpinLockUnlock(lock);
return 0;
}
int pthread_spin_destroy(pthread_spinlock_t *lock)
{
if (*lock != 0)
return EBUSY;
return 0;
}
int pthread_spin_init(pthread_spinlock_t *lock, int pshared)
{
if (lock == NULL)
return EINVAL;
*lock = OS_SPINLOCK_INIT;
(void)pshared;
return 0;
}
# endif /* macOS 10.12 or newer */
#endif /* __APPLE__ */