master
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#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#define WIN32_LEAN_AND_MEAN
29#include <windows.h>
30
31/* public header files */
32#include "pthread.h"
33/* internal header files */
34#include "misc.h"
35
36/* We use the pthread_spinlock_t itself as a lock:
37 -1 is free, 0 is locked.
38 (This is dictated by PTHREAD_SPINLOCK_INITIALIZER, which we can't change
39 without breaking binary compatibility.) */
40typedef intptr_t spinlock_word_t;
41
42int
43pthread_spin_init (pthread_spinlock_t *lock, int pshared)
44{
45 spinlock_word_t *lk = (spinlock_word_t *)lock;
46 *lk = -1;
47 return 0;
48}
49
50
51int
52pthread_spin_destroy (pthread_spinlock_t *lock)
53{
54 return 0;
55}
56
57int
58pthread_spin_lock (pthread_spinlock_t *lock)
59{
60 volatile spinlock_word_t *lk = (volatile spinlock_word_t *)lock;
61 while (unlikely(InterlockedExchangePointer((PVOID volatile *)lk, 0) == 0))
62 do {
63 YieldProcessor();
64 } while (*lk == 0);
65 return 0;
66}
67
68int
69pthread_spin_trylock (pthread_spinlock_t *lock)
70{
71 spinlock_word_t *lk = (spinlock_word_t *)lock;
72 return InterlockedExchangePointer((PVOID volatile *)lk, 0) == 0 ? EBUSY : 0;
73}
74
75
76int
77pthread_spin_unlock (pthread_spinlock_t *lock)
78{
79 volatile spinlock_word_t *lk = (volatile spinlock_word_t *)lock;
80 *lk = -1;
81 return 0;
82}