Mercurial > sdl-ios-xcode
annotate src/thread/generic/SDL_sysmutex.c @ 1388:9a9b87172b4b
Fixed build dependencies... ugh
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 20 Feb 2006 11:29:36 +0000 |
parents | 19418e4422cb |
children | d910939febfa |
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 */ |
22 | |
23 /* An implementation of mutexes using semaphores */ | |
24 | |
25 #include "SDL_thread.h" | |
26 #include "SDL_systhread_c.h" | |
27 | |
28 | |
29 struct SDL_mutex { | |
30 int recursive; | |
31 Uint32 owner; | |
32 SDL_sem *sem; | |
33 }; | |
34 | |
35 /* Create a mutex */ | |
36 SDL_mutex *SDL_CreateMutex(void) | |
37 { | |
38 SDL_mutex *mutex; | |
39 | |
40 /* Allocate mutex memory */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
41 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); |
0 | 42 if ( mutex ) { |
43 /* Create the mutex semaphore, with initial value 1 */ | |
44 mutex->sem = SDL_CreateSemaphore(1); | |
45 mutex->recursive = 0; | |
46 mutex->owner = 0; | |
47 if ( ! mutex->sem ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
48 SDL_free(mutex); |
0 | 49 mutex = NULL; |
50 } | |
51 } else { | |
52 SDL_OutOfMemory(); | |
53 } | |
54 return mutex; | |
55 } | |
56 | |
57 /* Free the mutex */ | |
58 void SDL_DestroyMutex(SDL_mutex *mutex) | |
59 { | |
60 if ( mutex ) { | |
61 if ( mutex->sem ) { | |
62 SDL_DestroySemaphore(mutex->sem); | |
63 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
64 SDL_free(mutex); |
0 | 65 } |
66 } | |
67 | |
68 /* Lock the semaphore */ | |
69 int SDL_mutexP(SDL_mutex *mutex) | |
70 { | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
71 #if SDL_THREADS_DISABLED |
0 | 72 return 0; |
73 #else | |
74 Uint32 this_thread; | |
75 | |
76 if ( mutex == NULL ) { | |
77 SDL_SetError("Passed a NULL mutex"); | |
78 return -1; | |
79 } | |
80 | |
81 this_thread = SDL_ThreadID(); | |
82 if ( mutex->owner == this_thread ) { | |
83 ++mutex->recursive; | |
84 } else { | |
85 /* The order of operations is important. | |
86 We set the locking thread id after we obtain the lock | |
87 so unlocks from other threads will fail. | |
88 */ | |
89 SDL_SemWait(mutex->sem); | |
90 mutex->owner = this_thread; | |
91 mutex->recursive = 0; | |
92 } | |
93 | |
94 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
|
95 #endif /* SDL_THREADS_DISABLED */ |
0 | 96 } |
97 | |
98 /* Unlock the mutex */ | |
99 int SDL_mutexV(SDL_mutex *mutex) | |
100 { | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
101 #if SDL_THREADS_DISABLED |
0 | 102 return 0; |
103 #else | |
104 if ( mutex == NULL ) { | |
105 SDL_SetError("Passed a NULL mutex"); | |
106 return -1; | |
107 } | |
108 | |
109 /* If we don't own the mutex, we can't unlock it */ | |
110 if ( SDL_ThreadID() != mutex->owner ) { | |
111 SDL_SetError("mutex not owned by this thread"); | |
112 return -1; | |
113 } | |
114 | |
115 if ( mutex->recursive ) { | |
116 --mutex->recursive; | |
117 } else { | |
118 /* The order of operations is important. | |
119 First reset the owner so another thread doesn't lock | |
120 the mutex and set the ownership before we reset it, | |
121 then release the lock semaphore. | |
122 */ | |
123 mutex->owner = 0; | |
124 SDL_SemPost(mutex->sem); | |
125 } | |
126 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
|
127 #endif /* SDL_THREADS_DISABLED */ |
0 | 128 } |