comparison src/thread/pthread/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
28 #if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \ 28 #if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
29 !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP 29 !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
30 #define FAKE_RECURSIVE_MUTEX 30 #define FAKE_RECURSIVE_MUTEX
31 #endif 31 #endif
32 32
33 struct SDL_mutex { 33 struct SDL_mutex
34 pthread_mutex_t id; 34 {
35 pthread_mutex_t id;
35 #if FAKE_RECURSIVE_MUTEX 36 #if FAKE_RECURSIVE_MUTEX
36 int recursive; 37 int recursive;
37 pthread_t owner; 38 pthread_t owner;
38 #endif 39 #endif
39 }; 40 };
40 41
41 SDL_mutex *SDL_CreateMutex (void) 42 SDL_mutex *
43 SDL_CreateMutex (void)
42 { 44 {
43 SDL_mutex *mutex; 45 SDL_mutex *mutex;
44 pthread_mutexattr_t attr; 46 pthread_mutexattr_t attr;
45 47
46 /* Allocate the structure */ 48 /* Allocate the structure */
47 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex)); 49 mutex = (SDL_mutex *) SDL_calloc (1, sizeof (*mutex));
48 if ( mutex ) { 50 if (mutex) {
49 pthread_mutexattr_init(&attr); 51 pthread_mutexattr_init (&attr);
50 #if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 52 #if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
51 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 53 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
52 #elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP 54 #elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
53 pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP); 55 pthread_mutexattr_setkind_np (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
54 #else 56 #else
55 /* No extra attributes necessary */ 57 /* No extra attributes necessary */
56 #endif 58 #endif
57 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) { 59 if (pthread_mutex_init (&mutex->id, &attr) != 0) {
58 SDL_SetError("pthread_mutex_init() failed"); 60 SDL_SetError ("pthread_mutex_init() failed");
59 SDL_free(mutex); 61 SDL_free (mutex);
60 mutex = NULL; 62 mutex = NULL;
61 } 63 }
62 } else { 64 } else {
63 SDL_OutOfMemory(); 65 SDL_OutOfMemory ();
64 } 66 }
65 return(mutex); 67 return (mutex);
66 } 68 }
67 69
68 void SDL_DestroyMutex(SDL_mutex *mutex) 70 void
71 SDL_DestroyMutex (SDL_mutex * mutex)
69 { 72 {
70 if ( mutex ) { 73 if (mutex) {
71 pthread_mutex_destroy(&mutex->id); 74 pthread_mutex_destroy (&mutex->id);
72 SDL_free(mutex); 75 SDL_free (mutex);
73 } 76 }
74 } 77 }
75 78
76 /* Lock the mutex */ 79 /* Lock the mutex */
77 int SDL_mutexP(SDL_mutex *mutex) 80 int
81 SDL_mutexP (SDL_mutex * mutex)
78 { 82 {
79 int retval; 83 int retval;
80 #if FAKE_RECURSIVE_MUTEX 84 #if FAKE_RECURSIVE_MUTEX
81 pthread_t this_thread; 85 pthread_t this_thread;
82 #endif 86 #endif
83 87
84 if ( mutex == NULL ) { 88 if (mutex == NULL) {
85 SDL_SetError("Passed a NULL mutex"); 89 SDL_SetError ("Passed a NULL mutex");
86 return -1; 90 return -1;
87 } 91 }
88 92
89 retval = 0; 93 retval = 0;
90 #if FAKE_RECURSIVE_MUTEX 94 #if FAKE_RECURSIVE_MUTEX
91 this_thread = pthread_self(); 95 this_thread = pthread_self ();
92 if ( mutex->owner == this_thread ) { 96 if (mutex->owner == this_thread) {
93 ++mutex->recursive; 97 ++mutex->recursive;
94 } else { 98 } else {
95 /* The order of operations is important. 99 /* The order of operations is important.
96 We set the locking thread id after we obtain the lock 100 We set the locking thread id after we obtain the lock
97 so unlocks from other threads will fail. 101 so unlocks from other threads will fail.
98 */ 102 */
99 if ( pthread_mutex_lock(&mutex->id) == 0 ) { 103 if (pthread_mutex_lock (&mutex->id) == 0) {
100 mutex->owner = this_thread; 104 mutex->owner = this_thread;
101 mutex->recursive = 0; 105 mutex->recursive = 0;
102 } else { 106 } else {
103 SDL_SetError("pthread_mutex_lock() failed"); 107 SDL_SetError ("pthread_mutex_lock() failed");
104 retval = -1; 108 retval = -1;
105 } 109 }
106 } 110 }
107 #else 111 #else
108 if ( pthread_mutex_lock(&mutex->id) < 0 ) { 112 if (pthread_mutex_lock (&mutex->id) < 0) {
109 SDL_SetError("pthread_mutex_lock() failed"); 113 SDL_SetError ("pthread_mutex_lock() failed");
110 retval = -1; 114 retval = -1;
111 } 115 }
112 #endif 116 #endif
113 return retval; 117 return retval;
114 } 118 }
115 119
116 int SDL_mutexV(SDL_mutex *mutex) 120 int
121 SDL_mutexV (SDL_mutex * mutex)
117 { 122 {
118 int retval; 123 int retval;
119 124
120 if ( mutex == NULL ) { 125 if (mutex == NULL) {
121 SDL_SetError("Passed a NULL mutex"); 126 SDL_SetError ("Passed a NULL mutex");
122 return -1; 127 return -1;
123 } 128 }
124 129
125 retval = 0; 130 retval = 0;
126 #if FAKE_RECURSIVE_MUTEX 131 #if FAKE_RECURSIVE_MUTEX
127 /* We can only unlock the mutex if we own it */ 132 /* We can only unlock the mutex if we own it */
128 if ( pthread_self() == mutex->owner ) { 133 if (pthread_self () == mutex->owner) {
129 if ( mutex->recursive ) { 134 if (mutex->recursive) {
130 --mutex->recursive; 135 --mutex->recursive;
131 } else { 136 } else {
132 /* The order of operations is important. 137 /* The order of operations is important.
133 First reset the owner so another thread doesn't lock 138 First reset the owner so another thread doesn't lock
134 the mutex and set the ownership before we reset it, 139 the mutex and set the ownership before we reset it,
135 then release the lock semaphore. 140 then release the lock semaphore.
136 */ 141 */
137 mutex->owner = 0; 142 mutex->owner = 0;
138 pthread_mutex_unlock(&mutex->id); 143 pthread_mutex_unlock (&mutex->id);
139 } 144 }
140 } else { 145 } else {
141 SDL_SetError("mutex not owned by this thread"); 146 SDL_SetError ("mutex not owned by this thread");
142 retval = -1; 147 retval = -1;
143 } 148 }
144 149
145 #else 150 #else
146 if ( pthread_mutex_unlock(&mutex->id) < 0 ) { 151 if (pthread_mutex_unlock (&mutex->id) < 0) {
147 SDL_SetError("pthread_mutex_unlock() failed"); 152 SDL_SetError ("pthread_mutex_unlock() failed");
148 retval = -1; 153 retval = -1;
149 } 154 }
150 #endif /* FAKE_RECURSIVE_MUTEX */ 155 #endif /* FAKE_RECURSIVE_MUTEX */
151 156
152 return retval; 157 return retval;
153 } 158 }
159
160 /* vi: set ts=4 sw=4 expandtab: */