Mercurial > sdl-ios-xcode
annotate src/thread/generic/SDL_sysmutex.c @ 4109:cd2ab40f1219 SDL-1.2
Made the mprotect() fix for SDL_SoftStretch() more general for hardened linux, etc.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 29 Dec 2007 05:18:33 +0000 |
parents | d910939febfa |
children | 782fd950bd46 c121d94672cb a1b03ba2fcd0 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* An implementation of mutexes using semaphores */ | |
25 | |
26 #include "SDL_thread.h" | |
27 #include "SDL_systhread_c.h" | |
28 | |
29 | |
30 struct SDL_mutex { | |
31 int recursive; | |
32 Uint32 owner; | |
33 SDL_sem *sem; | |
34 }; | |
35 | |
36 /* Create a mutex */ | |
37 SDL_mutex *SDL_CreateMutex(void) | |
38 { | |
39 SDL_mutex *mutex; | |
40 | |
41 /* Allocate mutex memory */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
42 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); |
0 | 43 if ( mutex ) { |
44 /* Create the mutex semaphore, with initial value 1 */ | |
45 mutex->sem = SDL_CreateSemaphore(1); | |
46 mutex->recursive = 0; | |
47 mutex->owner = 0; | |
48 if ( ! mutex->sem ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
49 SDL_free(mutex); |
0 | 50 mutex = NULL; |
51 } | |
52 } else { | |
53 SDL_OutOfMemory(); | |
54 } | |
55 return mutex; | |
56 } | |
57 | |
58 /* Free the mutex */ | |
59 void SDL_DestroyMutex(SDL_mutex *mutex) | |
60 { | |
61 if ( mutex ) { | |
62 if ( mutex->sem ) { | |
63 SDL_DestroySemaphore(mutex->sem); | |
64 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
65 SDL_free(mutex); |
0 | 66 } |
67 } | |
68 | |
69 /* Lock the semaphore */ | |
70 int SDL_mutexP(SDL_mutex *mutex) | |
71 { | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
72 #if SDL_THREADS_DISABLED |
0 | 73 return 0; |
74 #else | |
75 Uint32 this_thread; | |
76 | |
77 if ( mutex == NULL ) { | |
78 SDL_SetError("Passed a NULL mutex"); | |
79 return -1; | |
80 } | |
81 | |
82 this_thread = SDL_ThreadID(); | |
83 if ( mutex->owner == this_thread ) { | |
84 ++mutex->recursive; | |
85 } else { | |
86 /* The order of operations is important. | |
87 We set the locking thread id after we obtain the lock | |
88 so unlocks from other threads will fail. | |
89 */ | |
90 SDL_SemWait(mutex->sem); | |
91 mutex->owner = this_thread; | |
92 mutex->recursive = 0; | |
93 } | |
94 | |
95 return 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
96 #endif /* SDL_THREADS_DISABLED */ |
0 | 97 } |
98 | |
99 /* Unlock the mutex */ | |
100 int SDL_mutexV(SDL_mutex *mutex) | |
101 { | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
102 #if SDL_THREADS_DISABLED |
0 | 103 return 0; |
104 #else | |
105 if ( mutex == NULL ) { | |
106 SDL_SetError("Passed a NULL mutex"); | |
107 return -1; | |
108 } | |
109 | |
110 /* If we don't own the mutex, we can't unlock it */ | |
111 if ( SDL_ThreadID() != mutex->owner ) { | |
112 SDL_SetError("mutex not owned by this thread"); | |
113 return -1; | |
114 } | |
115 | |
116 if ( mutex->recursive ) { | |
117 --mutex->recursive; | |
118 } else { | |
119 /* The order of operations is important. | |
120 First reset the owner so another thread doesn't lock | |
121 the mutex and set the ownership before we reset it, | |
122 then release the lock semaphore. | |
123 */ | |
124 mutex->owner = 0; | |
125 SDL_SemPost(mutex->sem); | |
126 } | |
127 return 0; | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
128 #endif /* SDL_THREADS_DISABLED */ |
0 | 129 } |