comparison src/cdrom/macosx/SDLOSXCAGuard.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 dc6b59e925a2
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
89 /*#warning Need a try-based Locker too*/ 89 /*#warning Need a try-based Locker too*/
90 /*============================================================================= 90 /*=============================================================================
91 SDLOSXCAGuard 91 SDLOSXCAGuard
92 =============================================================================*/ 92 =============================================================================*/
93 93
94 static int SDLOSXCAGuard_Lock(SDLOSXCAGuard *cag) 94 static int
95 SDLOSXCAGuard_Lock(SDLOSXCAGuard * cag)
95 { 96 {
96 int theAnswer = 0; 97 int theAnswer = 0;
97 98
98 if(pthread_self() != cag->mOwner) 99 if (pthread_self() != cag->mOwner) {
99 {
100 OSStatus theError = pthread_mutex_lock(&cag->mMutex); 100 OSStatus theError = pthread_mutex_lock(&cag->mMutex);
101 (void)theError; 101 (void) theError;
102 assert(theError == 0); 102 assert(theError == 0);
103 cag->mOwner = pthread_self(); 103 cag->mOwner = pthread_self();
104 theAnswer = 1; 104 theAnswer = 1;
105 } 105 }
106 106
107 return theAnswer; 107 return theAnswer;
108 } 108 }
109 109
110 static void SDLOSXCAGuard_Unlock(SDLOSXCAGuard *cag) 110 static void
111 SDLOSXCAGuard_Unlock(SDLOSXCAGuard * cag)
111 { 112 {
112 OSStatus theError; 113 OSStatus theError;
113 assert(pthread_self() == cag->mOwner); 114 assert(pthread_self() == cag->mOwner);
114 115
115 cag->mOwner = 0; 116 cag->mOwner = 0;
116 theError = pthread_mutex_unlock(&cag->mMutex); 117 theError = pthread_mutex_unlock(&cag->mMutex);
117 (void)theError; 118 (void) theError;
118 assert(theError == 0); 119 assert(theError == 0);
119 } 120 }
120 121
121 static int SDLOSXCAGuard_Try (SDLOSXCAGuard *cag, int *outWasLocked) 122 static int
123 SDLOSXCAGuard_Try(SDLOSXCAGuard * cag, int *outWasLocked)
122 { 124 {
123 int theAnswer = 0; 125 int theAnswer = 0;
124 *outWasLocked = 0; 126 *outWasLocked = 0;
125 127
126 if (pthread_self() == cag->mOwner) { 128 if (pthread_self() == cag->mOwner) {
127 theAnswer = 1; 129 theAnswer = 1;
128 *outWasLocked = 0; 130 *outWasLocked = 0;
129 } else { 131 } else {
130 OSStatus theError = pthread_mutex_trylock(&cag->mMutex); 132 OSStatus theError = pthread_mutex_trylock(&cag->mMutex);
132 cag->mOwner = pthread_self(); 134 cag->mOwner = pthread_self();
133 theAnswer = 1; 135 theAnswer = 1;
134 *outWasLocked = 1; 136 *outWasLocked = 1;
135 } 137 }
136 } 138 }
137 139
138 return theAnswer; 140 return theAnswer;
139 } 141 }
140 142
141 static void SDLOSXCAGuard_Wait(SDLOSXCAGuard *cag) 143 static void
144 SDLOSXCAGuard_Wait(SDLOSXCAGuard * cag)
142 { 145 {
143 OSStatus theError; 146 OSStatus theError;
144 assert(pthread_self() == cag->mOwner); 147 assert(pthread_self() == cag->mOwner);
145 148
146 cag->mOwner = 0; 149 cag->mOwner = 0;
147 150
148 theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex); 151 theError = pthread_cond_wait(&cag->mCondVar, &cag->mMutex);
149 (void)theError; 152 (void) theError;
150 assert(theError == 0); 153 assert(theError == 0);
151 cag->mOwner = pthread_self(); 154 cag->mOwner = pthread_self();
152 } 155 }
153 156
154 static void SDLOSXCAGuard_Notify(SDLOSXCAGuard *cag) 157 static void
158 SDLOSXCAGuard_Notify(SDLOSXCAGuard * cag)
155 { 159 {
156 OSStatus theError = pthread_cond_signal(&cag->mCondVar); 160 OSStatus theError = pthread_cond_signal(&cag->mCondVar);
157 (void)theError; 161 (void) theError;
158 assert(theError == 0); 162 assert(theError == 0);
159 } 163 }
160 164
161 165
162 SDLOSXCAGuard *new_SDLOSXCAGuard(void) 166 SDLOSXCAGuard *
167 new_SDLOSXCAGuard(void)
163 { 168 {
164 OSStatus theError; 169 OSStatus theError;
165 SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof (SDLOSXCAGuard)); 170 SDLOSXCAGuard *cag = (SDLOSXCAGuard *) SDL_malloc(sizeof(SDLOSXCAGuard));
166 if (cag == NULL) 171 if (cag == NULL)
167 return NULL; 172 return NULL;
168 SDL_memset(cag, '\0', sizeof (*cag)); 173 SDL_memset(cag, '\0', sizeof(*cag));
169 174
170 #define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m 175 #define SET_SDLOSXCAGUARD_METHOD(m) cag->m = SDLOSXCAGuard_##m
171 SET_SDLOSXCAGUARD_METHOD(Lock); 176 SET_SDLOSXCAGUARD_METHOD(Lock);
172 SET_SDLOSXCAGUARD_METHOD(Unlock); 177 SET_SDLOSXCAGUARD_METHOD(Unlock);
173 SET_SDLOSXCAGUARD_METHOD(Try); 178 SET_SDLOSXCAGUARD_METHOD(Try);
174 SET_SDLOSXCAGUARD_METHOD(Wait); 179 SET_SDLOSXCAGUARD_METHOD(Wait);
175 SET_SDLOSXCAGUARD_METHOD(Notify); 180 SET_SDLOSXCAGUARD_METHOD(Notify);
176 #undef SET_SDLOSXCAGUARD_METHOD 181 #undef SET_SDLOSXCAGUARD_METHOD
177 182
178 theError = pthread_mutex_init(&cag->mMutex, NULL); 183 theError = pthread_mutex_init(&cag->mMutex, NULL);
179 (void)theError; 184 (void) theError;
180 assert(theError == 0); 185 assert(theError == 0);
181 186
182 theError = pthread_cond_init(&cag->mCondVar, NULL); 187 theError = pthread_cond_init(&cag->mCondVar, NULL);
183 (void)theError; 188 (void) theError;
184 assert(theError == 0); 189 assert(theError == 0);
185 190
186 cag->mOwner = 0; 191 cag->mOwner = 0;
187 return cag; 192 return cag;
188 } 193 }
189 194
190 void delete_SDLOSXCAGuard(SDLOSXCAGuard *cag) 195 void
191 { 196 delete_SDLOSXCAGuard(SDLOSXCAGuard * cag)
192 if (cag != NULL) 197 {
193 { 198 if (cag != NULL) {
194 pthread_mutex_destroy(&cag->mMutex); 199 pthread_mutex_destroy(&cag->mMutex);
195 pthread_cond_destroy(&cag->mCondVar); 200 pthread_cond_destroy(&cag->mCondVar);
196 SDL_free(cag); 201 SDL_free(cag);
197 } 202 }
198 } 203 }
199 204
205 /* vi: set ts=4 sw=4 expandtab: */