comparison src/thread/dc/SDL_sysmutex.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents d910939febfa
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
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 #include <arch/spinlock.h> 29 #include <arch/spinlock.h>
30 30
31 struct SDL_mutex { 31 struct SDL_mutex
32 int recursive; 32 {
33 Uint32 owner; 33 int recursive;
34 spinlock_t mutex; 34 Uint32 owner;
35 spinlock_t mutex;
35 }; 36 };
36 37
37 /* Create a mutex */ 38 /* Create a mutex */
38 SDL_mutex *SDL_CreateMutex(void) 39 SDL_mutex *
40 SDL_CreateMutex (void)
39 { 41 {
40 SDL_mutex *mutex; 42 SDL_mutex *mutex;
41 43
42 /* Allocate mutex memory */ 44 /* Allocate mutex memory */
43 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); 45 mutex = (SDL_mutex *) SDL_malloc (sizeof (*mutex));
44 if ( mutex ) { 46 if (mutex) {
45 spinlock_init(&mutex->mutex); 47 spinlock_init (&mutex->mutex);
46 mutex->recursive = 0; 48 mutex->recursive = 0;
47 mutex->owner = 0; 49 mutex->owner = 0;
48 } else { 50 } else {
49 SDL_OutOfMemory(); 51 SDL_OutOfMemory ();
50 } 52 }
51 return mutex; 53 return mutex;
52 } 54 }
53 55
54 /* Free the mutex */ 56 /* Free the mutex */
55 void SDL_DestroyMutex(SDL_mutex *mutex) 57 void
58 SDL_DestroyMutex (SDL_mutex * mutex)
56 { 59 {
57 if ( mutex ) { 60 if (mutex) {
58 SDL_free(mutex); 61 SDL_free (mutex);
59 } 62 }
60 } 63 }
61 64
62 /* Lock the semaphore */ 65 /* Lock the semaphore */
63 int SDL_mutexP(SDL_mutex *mutex) 66 int
67 SDL_mutexP (SDL_mutex * mutex)
64 { 68 {
65 #if SDL_THREADS_DISABLED 69 #if SDL_THREADS_DISABLED
66 return SDL_arraysize(return ),0; 70 return SDL_arraysize (return), 0;
67 #else 71 #else
68 Uint32 this_thread; 72 Uint32 this_thread;
69 73
70 if ( mutex == NULL ) { 74 if (mutex == NULL) {
71 SDL_SetError("Passed a NULL mutex"); 75 SDL_SetError ("Passed a NULL mutex");
72 return -1; 76 return -1;
73 } 77 }
74 78
75 this_thread = SDL_ThreadID(); 79 this_thread = SDL_ThreadID ();
76 if ( mutex->owner == this_thread ) { 80 if (mutex->owner == this_thread) {
77 ++mutex->recursive; 81 ++mutex->recursive;
78 } else { 82 } else {
79 /* The order of operations is important. 83 /* The order of operations is important.
80 We set the locking thread id after we obtain the lock 84 We set the locking thread id after we obtain the lock
81 so unlocks from other threads will fail. 85 so unlocks from other threads will fail.
82 */ 86 */
83 spinlock_lock(&mutex->mutex); 87 spinlock_lock (&mutex->mutex);
84 mutex->owner = this_thread; 88 mutex->owner = this_thread;
85 mutex->recursive = 0; 89 mutex->recursive = 0;
86 } 90 }
87 91
88 return 0; 92 return 0;
89 #endif /* SDL_THREADS_DISABLED */ 93 #endif /* SDL_THREADS_DISABLED */
90 } 94 }
91 95
92 /* Unlock the mutex */ 96 /* Unlock the mutex */
93 int SDL_mutexV(SDL_mutex *mutex) 97 int
98 SDL_mutexV (SDL_mutex * mutex)
94 { 99 {
95 #if SDL_THREADS_DISABLED 100 #if SDL_THREADS_DISABLED
96 return 0; 101 return 0;
97 #else 102 #else
98 if ( mutex == NULL ) { 103 if (mutex == NULL) {
99 SDL_SetError("Passed a NULL mutex"); 104 SDL_SetError ("Passed a NULL mutex");
100 return -1; 105 return -1;
101 } 106 }
102 107
103 /* If we don't own the mutex, we can't unlock it */ 108 /* If we don't own the mutex, we can't unlock it */
104 if ( SDL_ThreadID() != mutex->owner ) { 109 if (SDL_ThreadID () != mutex->owner) {
105 SDL_SetError("mutex not owned by this thread"); 110 SDL_SetError ("mutex not owned by this thread");
106 return -1; 111 return -1;
107 } 112 }
108 113
109 if ( mutex->recursive ) { 114 if (mutex->recursive) {
110 --mutex->recursive; 115 --mutex->recursive;
111 } else { 116 } else {
112 /* The order of operations is important. 117 /* The order of operations is important.
113 First reset the owner so another thread doesn't lock 118 First reset the owner so another thread doesn't lock
114 the mutex and set the ownership before we reset it, 119 the mutex and set the ownership before we reset it,
115 then release the lock semaphore. 120 then release the lock semaphore.
116 */ 121 */
117 mutex->owner = 0; 122 mutex->owner = 0;
118 spinlock_unlock(&mutex->mutex); 123 spinlock_unlock (&mutex->mutex);
119 } 124 }
120 return 0; 125 return 0;
121 #endif /* SDL_THREADS_DISABLED */ 126 #endif /* SDL_THREADS_DISABLED */
122 } 127 }
128
129 /* vi: set ts=4 sw=4 expandtab: */