Mercurial > sdl-ios-xcode
comparison src/thread/generic/SDL_sysmutex.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | d910939febfa |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
25 | 25 |
26 #include "SDL_thread.h" | 26 #include "SDL_thread.h" |
27 #include "SDL_systhread_c.h" | 27 #include "SDL_systhread_c.h" |
28 | 28 |
29 | 29 |
30 struct SDL_mutex { | 30 struct SDL_mutex |
31 int recursive; | 31 { |
32 Uint32 owner; | 32 int recursive; |
33 SDL_sem *sem; | 33 Uint32 owner; |
34 SDL_sem *sem; | |
34 }; | 35 }; |
35 | 36 |
36 /* Create a mutex */ | 37 /* Create a mutex */ |
37 SDL_mutex *SDL_CreateMutex(void) | 38 SDL_mutex * |
39 SDL_CreateMutex(void) | |
38 { | 40 { |
39 SDL_mutex *mutex; | 41 SDL_mutex *mutex; |
40 | 42 |
41 /* Allocate mutex memory */ | 43 /* Allocate mutex memory */ |
42 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); | 44 mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex)); |
43 if ( mutex ) { | 45 if (mutex) { |
44 /* Create the mutex semaphore, with initial value 1 */ | 46 /* Create the mutex semaphore, with initial value 1 */ |
45 mutex->sem = SDL_CreateSemaphore(1); | 47 mutex->sem = SDL_CreateSemaphore(1); |
46 mutex->recursive = 0; | 48 mutex->recursive = 0; |
47 mutex->owner = 0; | 49 mutex->owner = 0; |
48 if ( ! mutex->sem ) { | 50 if (!mutex->sem) { |
49 SDL_free(mutex); | 51 SDL_free(mutex); |
50 mutex = NULL; | 52 mutex = NULL; |
51 } | 53 } |
52 } else { | 54 } else { |
53 SDL_OutOfMemory(); | 55 SDL_OutOfMemory(); |
54 } | 56 } |
55 return mutex; | 57 return mutex; |
56 } | 58 } |
57 | 59 |
58 /* Free the mutex */ | 60 /* Free the mutex */ |
59 void SDL_DestroyMutex(SDL_mutex *mutex) | 61 void |
62 SDL_DestroyMutex(SDL_mutex * mutex) | |
60 { | 63 { |
61 if ( mutex ) { | 64 if (mutex) { |
62 if ( mutex->sem ) { | 65 if (mutex->sem) { |
63 SDL_DestroySemaphore(mutex->sem); | 66 SDL_DestroySemaphore(mutex->sem); |
64 } | 67 } |
65 SDL_free(mutex); | 68 SDL_free(mutex); |
66 } | 69 } |
67 } | 70 } |
68 | 71 |
69 /* Lock the semaphore */ | 72 /* Lock the semaphore */ |
70 int SDL_mutexP(SDL_mutex *mutex) | 73 int |
74 SDL_mutexP(SDL_mutex * mutex) | |
71 { | 75 { |
72 #if SDL_THREADS_DISABLED | 76 #if SDL_THREADS_DISABLED |
73 return 0; | 77 return 0; |
74 #else | 78 #else |
75 Uint32 this_thread; | 79 Uint32 this_thread; |
76 | 80 |
77 if ( mutex == NULL ) { | 81 if (mutex == NULL) { |
78 SDL_SetError("Passed a NULL mutex"); | 82 SDL_SetError("Passed a NULL mutex"); |
79 return -1; | 83 return -1; |
80 } | 84 } |
81 | 85 |
82 this_thread = SDL_ThreadID(); | 86 this_thread = SDL_ThreadID(); |
83 if ( mutex->owner == this_thread ) { | 87 if (mutex->owner == this_thread) { |
84 ++mutex->recursive; | 88 ++mutex->recursive; |
85 } else { | 89 } else { |
86 /* The order of operations is important. | 90 /* The order of operations is important. |
87 We set the locking thread id after we obtain the lock | 91 We set the locking thread id after we obtain the lock |
88 so unlocks from other threads will fail. | 92 so unlocks from other threads will fail. |
89 */ | 93 */ |
90 SDL_SemWait(mutex->sem); | 94 SDL_SemWait(mutex->sem); |
91 mutex->owner = this_thread; | 95 mutex->owner = this_thread; |
92 mutex->recursive = 0; | 96 mutex->recursive = 0; |
93 } | 97 } |
94 | 98 |
95 return 0; | 99 return 0; |
96 #endif /* SDL_THREADS_DISABLED */ | 100 #endif /* SDL_THREADS_DISABLED */ |
97 } | 101 } |
98 | 102 |
99 /* Unlock the mutex */ | 103 /* Unlock the mutex */ |
100 int SDL_mutexV(SDL_mutex *mutex) | 104 int |
105 SDL_mutexV(SDL_mutex * mutex) | |
101 { | 106 { |
102 #if SDL_THREADS_DISABLED | 107 #if SDL_THREADS_DISABLED |
103 return 0; | 108 return 0; |
104 #else | 109 #else |
105 if ( mutex == NULL ) { | 110 if (mutex == NULL) { |
106 SDL_SetError("Passed a NULL mutex"); | 111 SDL_SetError("Passed a NULL mutex"); |
107 return -1; | 112 return -1; |
108 } | 113 } |
109 | 114 |
110 /* If we don't own the mutex, we can't unlock it */ | 115 /* If we don't own the mutex, we can't unlock it */ |
111 if ( SDL_ThreadID() != mutex->owner ) { | 116 if (SDL_ThreadID() != mutex->owner) { |
112 SDL_SetError("mutex not owned by this thread"); | 117 SDL_SetError("mutex not owned by this thread"); |
113 return -1; | 118 return -1; |
114 } | 119 } |
115 | 120 |
116 if ( mutex->recursive ) { | 121 if (mutex->recursive) { |
117 --mutex->recursive; | 122 --mutex->recursive; |
118 } else { | 123 } else { |
119 /* The order of operations is important. | 124 /* The order of operations is important. |
120 First reset the owner so another thread doesn't lock | 125 First reset the owner so another thread doesn't lock |
121 the mutex and set the ownership before we reset it, | 126 the mutex and set the ownership before we reset it, |
122 then release the lock semaphore. | 127 then release the lock semaphore. |
123 */ | 128 */ |
124 mutex->owner = 0; | 129 mutex->owner = 0; |
125 SDL_SemPost(mutex->sem); | 130 SDL_SemPost(mutex->sem); |
126 } | 131 } |
127 return 0; | 132 return 0; |
128 #endif /* SDL_THREADS_DISABLED */ | 133 #endif /* SDL_THREADS_DISABLED */ |
129 } | 134 } |
135 | |
136 /* vi: set ts=4 sw=4 expandtab: */ |