Mercurial > sdl-ios-xcode
annotate src/thread/generic/SDL_sysmutex.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | b8d311d90021 |
children | 450721ad5436 |
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 <stdio.h> | |
26 #include <stdlib.h> | |
27 | |
28 #include "SDL_error.h" | |
29 #include "SDL_thread.h" | |
30 #include "SDL_systhread_c.h" | |
31 | |
32 | |
33 struct SDL_mutex { | |
34 int recursive; | |
35 Uint32 owner; | |
36 SDL_sem *sem; | |
37 }; | |
38 | |
39 /* Create a mutex */ | |
40 SDL_mutex *SDL_CreateMutex(void) | |
41 { | |
42 SDL_mutex *mutex; | |
43 | |
44 /* Allocate mutex memory */ | |
45 mutex = (SDL_mutex *)malloc(sizeof(*mutex)); | |
46 if ( mutex ) { | |
47 /* Create the mutex semaphore, with initial value 1 */ | |
48 mutex->sem = SDL_CreateSemaphore(1); | |
49 mutex->recursive = 0; | |
50 mutex->owner = 0; | |
51 if ( ! mutex->sem ) { | |
52 free(mutex); | |
53 mutex = NULL; | |
54 } | |
55 } else { | |
56 SDL_OutOfMemory(); | |
57 } | |
58 return mutex; | |
59 } | |
60 | |
61 /* Free the mutex */ | |
62 void SDL_DestroyMutex(SDL_mutex *mutex) | |
63 { | |
64 if ( mutex ) { | |
65 if ( mutex->sem ) { | |
66 SDL_DestroySemaphore(mutex->sem); | |
67 } | |
68 free(mutex); | |
69 } | |
70 } | |
71 | |
72 /* Lock the semaphore */ | |
73 int SDL_mutexP(SDL_mutex *mutex) | |
74 { | |
75 #ifdef DISABLE_THREADS | |
76 return 0; | |
77 #else | |
78 Uint32 this_thread; | |
79 | |
80 if ( mutex == NULL ) { | |
81 SDL_SetError("Passed a NULL mutex"); | |
82 return -1; | |
83 } | |
84 | |
85 this_thread = SDL_ThreadID(); | |
86 if ( mutex->owner == this_thread ) { | |
87 ++mutex->recursive; | |
88 } else { | |
89 /* The order of operations is important. | |
90 We set the locking thread id after we obtain the lock | |
91 so unlocks from other threads will fail. | |
92 */ | |
93 SDL_SemWait(mutex->sem); | |
94 mutex->owner = this_thread; | |
95 mutex->recursive = 0; | |
96 } | |
97 | |
98 return 0; | |
99 #endif /* DISABLE_THREADS */ | |
100 } | |
101 | |
102 /* Unlock the mutex */ | |
103 int SDL_mutexV(SDL_mutex *mutex) | |
104 { | |
105 #ifdef DISABLE_THREADS | |
106 return 0; | |
107 #else | |
108 if ( mutex == NULL ) { | |
109 SDL_SetError("Passed a NULL mutex"); | |
110 return -1; | |
111 } | |
112 | |
113 /* If we don't own the mutex, we can't unlock it */ | |
114 if ( SDL_ThreadID() != mutex->owner ) { | |
115 SDL_SetError("mutex not owned by this thread"); | |
116 return -1; | |
117 } | |
118 | |
119 if ( mutex->recursive ) { | |
120 --mutex->recursive; | |
121 } else { | |
122 /* The order of operations is important. | |
123 First reset the owner so another thread doesn't lock | |
124 the mutex and set the ownership before we reset it, | |
125 then release the lock semaphore. | |
126 */ | |
127 mutex->owner = 0; | |
128 SDL_SemPost(mutex->sem); | |
129 } | |
130 return 0; | |
131 #endif /* DISABLE_THREADS */ | |
132 } |