• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    Copyright (c) 2013 mingw-w64 project
3    Copyright (c) 2015 Intel Corporation
4 
5    Permission is hereby granted, free of charge, to any person obtaining a
6    copy of this software and associated documentation files (the "Software"),
7    to deal in the Software without restriction, including without limitation
8    the rights to use, copy, modify, merge, publish, distribute, sublicense,
9    and/or sell copies of the Software, and to permit persons to whom the
10    Software is furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21    DEALINGS IN THE SOFTWARE.
22 */
23 
24 #include "pthread.h"
25 
26 #define likely(cond) __builtin_expect((cond) != 0, 1)
27 #define unlikely(cond) __builtin_expect((cond) != 0, 0)
28 
29 /* We use the pthread_spinlock_t itself as a lock:
30    -1 is free, 0 is locked.
31    (This is dictated by PTHREAD_SPINLOCK_INITIALIZER, which we can't change
32    without breaking binary compatibility.) */
33 typedef intptr_t spinlock_word_t;
34 
35 int
pthread_spin_init(pthread_spinlock_t * lock,int pshared)36 pthread_spin_init (pthread_spinlock_t *lock, int pshared)
37 {
38   spinlock_word_t *lk = (spinlock_word_t *)lock;
39   *lk = -1;
40   return 0;
41 }
42 
43 
44 int
pthread_spin_destroy(pthread_spinlock_t * lock)45 pthread_spin_destroy (pthread_spinlock_t *lock)
46 {
47   return 0;
48 }
49 
50 int
pthread_spin_lock(pthread_spinlock_t * lock)51 pthread_spin_lock (pthread_spinlock_t *lock)
52 {
53   volatile spinlock_word_t *lk = (volatile spinlock_word_t *)lock;
54   while (unlikely(__sync_lock_test_and_set(lk, 0) == 0))
55     do {
56 #if defined(__i386__) || defined(__x86_64__)
57       asm("pause" ::: "memory");
58 #elif defined(__arm__) || defined(__aarch64__)
59       asm("wfe" ::: "memory");
60 #else
61 #error Unsupported architecture
62 #endif
63     } while (*lk == 0);
64   return 0;
65 }
66 
67 int
pthread_spin_trylock(pthread_spinlock_t * lock)68 pthread_spin_trylock (pthread_spinlock_t *lock)
69 {
70   spinlock_word_t *lk = (spinlock_word_t *)lock;
71   return __sync_lock_test_and_set(lk, 0) == 0 ? EBUSY : 0;
72 }
73 
74 
75 int
pthread_spin_unlock(pthread_spinlock_t * lock)76 pthread_spin_unlock (pthread_spinlock_t *lock)
77 {
78   volatile spinlock_word_t *lk = (volatile spinlock_word_t *)lock;
79   *lk = -1;
80   return 0;
81 }
82