Mercurial > sdl-ios-xcode
comparison src/thread/nds/SDL_sysmutex.c @ 2671:c3e7c0698cbb gsoc2008_nds
some changes to the dummy driver for debug purposes that should be reverted.
most importantly, commenting out a check for an env. var.
author | Darren Alton <dalton@stevens.edu> |
---|---|
date | Thu, 12 Jun 2008 02:38:49 +0000 |
parents | 6e4669f4db49 |
children |
comparison
equal
deleted
inserted
replaced
2670:6e4669f4db49 | 2671:c3e7c0698cbb |
---|---|
20 slouken@devolution.com | 20 slouken@devolution.com |
21 */ | 21 */ |
22 | 22 |
23 #ifdef SAVE_RCSID | 23 #ifdef SAVE_RCSID |
24 static char rcsid = | 24 static char rcsid = |
25 "@(#) $Id: SDL_sysmutex.c,v 1.2 2001/04/26 16:50:18 hercules Exp $"; | 25 "@(#) $Id: SDL_sysmutex.c,v 1.2 2001/04/26 16:50:18 hercules Exp $"; |
26 #endif | 26 #endif |
27 | 27 |
28 /* An implementation of mutexes using semaphores */ | 28 /* An implementation of mutexes using semaphores */ |
29 | 29 |
30 #include <stdio.h> | 30 #include <stdio.h> |
33 #include "SDL_error.h" | 33 #include "SDL_error.h" |
34 #include "SDL_thread.h" | 34 #include "SDL_thread.h" |
35 #include "SDL_systhread_c.h" | 35 #include "SDL_systhread_c.h" |
36 | 36 |
37 | 37 |
38 struct SDL_mutex { | 38 struct SDL_mutex |
39 int recursive; | 39 { |
40 Uint32 owner; | 40 int recursive; |
41 SDL_sem *sem; | 41 Uint32 owner; |
42 SDL_sem *sem; | |
42 }; | 43 }; |
43 | 44 |
44 /* Create a mutex */ | 45 /* Create a mutex */ |
45 SDL_mutex *SDL_CreateMutex(void) | 46 SDL_mutex * |
47 SDL_CreateMutex(void) | |
46 { | 48 { |
47 SDL_mutex *mutex; | 49 SDL_mutex *mutex; |
48 | 50 |
49 /* Allocate mutex memory */ | 51 /* Allocate mutex memory */ |
50 mutex = (SDL_mutex *)malloc(sizeof(*mutex)); | 52 mutex = (SDL_mutex *) malloc(sizeof(*mutex)); |
51 if ( mutex ) { | 53 if (mutex) { |
52 /* Create the mutex semaphore, with initial value 1 */ | 54 /* Create the mutex semaphore, with initial value 1 */ |
53 mutex->sem = SDL_CreateSemaphore(1); | 55 mutex->sem = SDL_CreateSemaphore(1); |
54 mutex->recursive = 0; | 56 mutex->recursive = 0; |
55 mutex->owner = 0; | 57 mutex->owner = 0; |
56 if ( ! mutex->sem ) { | 58 if (!mutex->sem) { |
57 free(mutex); | 59 free(mutex); |
58 mutex = NULL; | 60 mutex = NULL; |
59 } | 61 } |
60 } else { | 62 } else { |
61 SDL_OutOfMemory(); | 63 SDL_OutOfMemory(); |
62 } | 64 } |
63 return mutex; | 65 return mutex; |
64 } | 66 } |
65 | 67 |
66 /* Free the mutex */ | 68 /* Free the mutex */ |
67 void SDL_DestroyMutex(SDL_mutex *mutex) | 69 void |
70 SDL_DestroyMutex(SDL_mutex * mutex) | |
68 { | 71 { |
69 if ( mutex ) { | 72 if (mutex) { |
70 if ( mutex->sem ) { | 73 if (mutex->sem) { |
71 SDL_DestroySemaphore(mutex->sem); | 74 SDL_DestroySemaphore(mutex->sem); |
72 } | 75 } |
73 free(mutex); | 76 free(mutex); |
74 } | 77 } |
75 } | 78 } |
76 | 79 |
77 /* Lock the semaphore */ | 80 /* Lock the semaphore */ |
78 int SDL_mutexP(SDL_mutex *mutex) | 81 int |
82 SDL_mutexP(SDL_mutex * mutex) | |
79 { | 83 { |
80 #ifdef DISABLE_THREADS | 84 #ifdef DISABLE_THREADS |
81 return 0; | 85 return 0; |
82 #else | 86 #else |
83 Uint32 this_thread; | 87 Uint32 this_thread; |
84 | 88 |
85 if ( mutex == NULL ) { | 89 if (mutex == NULL) { |
86 SDL_SetError("Passed a NULL mutex"); | 90 SDL_SetError("Passed a NULL mutex"); |
87 return -1; | 91 return -1; |
88 } | 92 } |
89 | 93 |
90 this_thread = SDL_ThreadID(); | 94 this_thread = SDL_ThreadID(); |
91 if ( mutex->owner == this_thread ) { | 95 if (mutex->owner == this_thread) { |
92 ++mutex->recursive; | 96 ++mutex->recursive; |
93 } else { | 97 } else { |
94 /* The order of operations is important. | 98 /* The order of operations is important. |
95 We set the locking thread id after we obtain the lock | 99 We set the locking thread id after we obtain the lock |
96 so unlocks from other threads will fail. | 100 so unlocks from other threads will fail. |
97 */ | 101 */ |
98 SDL_SemWait(mutex->sem); | 102 SDL_SemWait(mutex->sem); |
99 mutex->owner = this_thread; | 103 mutex->owner = this_thread; |
100 mutex->recursive = 0; | 104 mutex->recursive = 0; |
101 } | 105 } |
102 | 106 |
103 return 0; | 107 return 0; |
104 #endif /* DISABLE_THREADS */ | 108 #endif /* DISABLE_THREADS */ |
105 } | 109 } |
106 | 110 |
107 /* Unlock the mutex */ | 111 /* Unlock the mutex */ |
108 int SDL_mutexV(SDL_mutex *mutex) | 112 int |
113 SDL_mutexV(SDL_mutex * mutex) | |
109 { | 114 { |
110 #ifdef DISABLE_THREADS | 115 #ifdef DISABLE_THREADS |
111 return 0; | 116 return 0; |
112 #else | 117 #else |
113 if ( mutex == NULL ) { | 118 if (mutex == NULL) { |
114 SDL_SetError("Passed a NULL mutex"); | 119 SDL_SetError("Passed a NULL mutex"); |
115 return -1; | 120 return -1; |
116 } | 121 } |
117 | 122 |
118 /* If we don't own the mutex, we can't unlock it */ | 123 /* If we don't own the mutex, we can't unlock it */ |
119 if ( SDL_ThreadID() != mutex->owner ) { | 124 if (SDL_ThreadID() != mutex->owner) { |
120 SDL_SetError("mutex not owned by this thread"); | 125 SDL_SetError("mutex not owned by this thread"); |
121 return -1; | 126 return -1; |
122 } | 127 } |
123 | 128 |
124 if ( mutex->recursive ) { | 129 if (mutex->recursive) { |
125 --mutex->recursive; | 130 --mutex->recursive; |
126 } else { | 131 } else { |
127 /* The order of operations is important. | 132 /* The order of operations is important. |
128 First reset the owner so another thread doesn't lock | 133 First reset the owner so another thread doesn't lock |
129 the mutex and set the ownership before we reset it, | 134 the mutex and set the ownership before we reset it, |
130 then release the lock semaphore. | 135 then release the lock semaphore. |
131 */ | 136 */ |
132 mutex->owner = 0; | 137 mutex->owner = 0; |
133 SDL_SemPost(mutex->sem); | 138 SDL_SemPost(mutex->sem); |
134 } | 139 } |
135 return 0; | 140 return 0; |
136 #endif /* DISABLE_THREADS */ | 141 #endif /* DISABLE_THREADS */ |
137 } | 142 } |