summaryrefslogtreecommitdiff
path: root/lib/pthread-spin.c
blob: 93e8b370ca64c81570dca717d25b7081dec7c085 (plain)
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
/* POSIX spin locks.
   Copyright (C) 2010-2020 Free Software Foundation, Inc.

   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, or (at your option)
   any later version.

   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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert, 2010, and Bruno Haible <bruno@clisp.org>, 2019.  */

#include <config.h>

/* Specification.  */
#include <pthread.h>

#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
# include "windows-spin.h"
#endif

#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
/* Use Windows threads.  */

int
pthread_spin_init (pthread_spinlock_t *lock,
                   int shared_across_processes _GL_UNUSED)
{
  glwthread_spin_init (lock);
  return 0;
}

int
pthread_spin_lock (pthread_spinlock_t *lock)
{
  return glwthread_spin_lock (lock);
}

int
pthread_spin_trylock (pthread_spinlock_t *lock)
{
  return glwthread_spin_trylock (lock);
}

int
pthread_spin_unlock (pthread_spinlock_t *lock)
{
  return glwthread_spin_unlock (lock);
}

int
pthread_spin_destroy (pthread_spinlock_t *lock)
{
  return glwthread_spin_destroy (lock);
}

#elif HAVE_PTHREAD_H
/* Provide workarounds for POSIX threads.  */

/* We don't use the C11 <stdatomic.h> (available in GCC >= 4.9) because it would
   require to link with -latomic.  */

# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
/* Use GCC built-ins (available in GCC >= 4.7) that operate on the first byte
   of the lock.
   Documentation:
   <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html>  */

int
pthread_spin_init (pthread_spinlock_t *lock,
                   int shared_across_processes _GL_UNUSED)
{
  __atomic_clear (lock, __ATOMIC_SEQ_CST);
  return 0;
}

int
pthread_spin_lock (pthread_spinlock_t *lock)
{
  while (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
    ;
  return 0;
}

int
pthread_spin_trylock (pthread_spinlock_t *lock)
{
  if (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
    return EBUSY;
  return 0;
}

int
pthread_spin_unlock (pthread_spinlock_t *lock)
{
  __atomic_clear (lock, __ATOMIC_SEQ_CST);
  return 0;
}

int
pthread_spin_destroy (pthread_spinlock_t *lock)
{
  return 0;
}

# else
/* Emulate a spin lock through a mutex.  */

int
pthread_spin_init (pthread_spinlock_t *lock,
                   int shared_across_processes _GL_UNUSED)
{
  return pthread_mutex_init (lock, NULL);
}

int
pthread_spin_lock (pthread_spinlock_t *lock)
{
  return pthread_mutex_lock (lock);
}

int
pthread_spin_trylock (pthread_spinlock_t *lock)
{
  return pthread_mutex_trylock (lock);
}

int
pthread_spin_unlock (pthread_spinlock_t *lock)
{
  return pthread_mutex_unlock (lock);
}

int
pthread_spin_destroy (pthread_spinlock_t *lock)
{
  return pthread_mutex_destroy (lock);
}

# endif

#else
/* Provide a dummy implementation for single-threaded applications.  */

int
pthread_spin_init (pthread_spinlock_t *lock _GL_UNUSED,
                   int shared_across_processes _GL_UNUSED)
{
  return 0;
}

int
pthread_spin_lock (pthread_spinlock_t *lock _GL_UNUSED)
{
  return 0;
}

int
pthread_spin_trylock (pthread_spinlock_t *lock _GL_UNUSED)
{
  return 0;
}

int
pthread_spin_unlock (pthread_spinlock_t *lock _GL_UNUSED)
{
  return 0;
}

int
pthread_spin_destroy (pthread_spinlock_t *lock _GL_UNUSED)
{
  return 0;
}

#endif