Mercurial > sdl-ios-xcode
annotate src/atomic/SDL_spinlock.c @ 5003:3a95a2b93eb3
Updated the atomic API for better use cases
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 15 Jan 2011 12:41:59 -0800 |
parents | |
children | 0c72ae7b7cb2 |
rev | line source |
---|---|
5003
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
1 /* |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
2 SDL - Simple DirectMedia Layer |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
3 Copyright (C) 1997-2010 Sam Lantinga |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
4 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
5 This library is free software; you can redistribute it and/or |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
7 License as published by the Free Software Foundation; either |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
9 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
10 This library is distributed in the hope that it will be useful, |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
13 Lesser General Public License for more details. |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
14 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
18 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
19 Sam Lantinga |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
20 slouken@libsdl.org |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
21 */ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
22 #include "SDL_stdinc.h" |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
23 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
24 #include "SDL_atomic.h" |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
25 #include "SDL_timer.h" |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
26 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
27 #if defined(__WIN32__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
28 #define WIN32_LEAN_AND_MEAN |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
29 #include <windows.h> |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
30 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
31 #elif defined(__MACOSX__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
32 #include <libkern/OSAtomic.h> |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
33 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
34 #endif |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
35 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
36 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
37 /* This function is where all the magic happens... */ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
38 SDL_bool |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
39 SDL_AtomicTryLock(SDL_SpinLock *lock) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
40 { |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
41 #if defined(__WIN32__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
42 return (InterlockedExchange(lock, 1) == 0); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
43 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
44 #elif defined(__MACOSX__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
45 return OSAtomicCompareAndSwap32Barrier(0, 1, lock); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
46 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
47 #elif defined(__GNUC__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
48 #if defined(__arm__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
49 #ifdef __ARM_ARCH_5__ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
50 int result; |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
51 __asm__ __volatile__ ( |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
52 "swp %0, %1, [%2]\n" |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
53 : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory"); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
54 return (result == 0); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
55 #else |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
56 int result; |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
57 __asm__ __volatile__ ( |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
58 "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]" |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
59 : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory"); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
60 return (result == 0); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
61 #endif |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
62 #else |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
63 return (__sync_lock_test_and_set(lock, 1) == 0); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
64 #endif |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
65 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
66 #else |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
67 /* Need CPU instructions for spinlock here! */ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
68 __need_spinlock_implementation__ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
69 #endif |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
70 } |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
71 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
72 void |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
73 SDL_AtomicLock(SDL_SpinLock *lock) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
74 { |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
75 /* FIXME: Should we have an eventual timeout? */ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
76 while (!SDL_AtomicTryLock(lock)) { |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
77 SDL_Delay(0); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
78 } |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
79 } |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
80 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
81 void |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
82 SDL_AtomicUnlock(SDL_SpinLock *lock) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
83 { |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
84 #if defined(__WIN32__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
85 *lock = 0; |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
86 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
87 #elif defined(__MACOSX__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
88 *lock = 0; |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
89 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
90 #elif defined(__GNUC__) && !defined(__arm__) |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
91 __sync_lock_release(lock); |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
92 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
93 #else |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
94 /* Assuming memory barrier in lock and integral assignment operation */ |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
95 *lock = 0; |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
96 #endif |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
97 } |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
98 |
3a95a2b93eb3
Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
99 /* vi: set ts=4 sw=4 expandtab: */ |