comparison src/thread/dc/SDL_sysmutex.c @ 509:dad72daf44b3

Added initial support for Dreamcast (thanks HERO!)
author Sam Lantinga <slouken@libsdl.org>
date Sat, 05 Oct 2002 16:50:56 +0000
parents
children b8d311d90021
comparison
equal deleted inserted replaced
508:9ff7e90aaa94 509:dad72daf44b3
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 BERO
20 bero@geocities.co.jp
21
22 based on generic/SDL_sysmutex.c
23
24 Sam Lantinga
25 slouken@libsdl.org
26 */
27
28 #ifdef SAVE_RCSID
29 static char rcsid =
30 "@(#) $Id$";
31 #endif
32
33 /* An implementation of mutexes using semaphores */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "SDL_error.h"
39 #include "SDL_thread.h"
40 #include "SDL_systhread_c.h"
41
42 #include <arch/spinlock.h>
43
44 struct SDL_mutex {
45 int recursive;
46 Uint32 owner;
47 spinlock_t mutex;
48 };
49
50 /* Create a mutex */
51 SDL_mutex *SDL_CreateMutex(void)
52 {
53 SDL_mutex *mutex;
54
55 /* Allocate mutex memory */
56 mutex = (SDL_mutex *)malloc(sizeof(*mutex));
57 if ( mutex ) {
58 spinlock_init(&mutex->mutex);
59 mutex->recursive = 0;
60 mutex->owner = 0;
61 } else {
62 SDL_OutOfMemory();
63 }
64 return mutex;
65 }
66
67 /* Free the mutex */
68 void SDL_DestroyMutex(SDL_mutex *mutex)
69 {
70 if ( mutex ) {
71 free(mutex);
72 }
73 }
74
75 /* Lock the semaphore */
76 int SDL_mutexP(SDL_mutex *mutex)
77 {
78 #ifdef DISABLE_THREADS
79 return 0;
80 #else
81 Uint32 this_thread;
82
83 if ( mutex == NULL ) {
84 SDL_SetError("Passed a NULL mutex");
85 return -1;
86 }
87
88 this_thread = SDL_ThreadID();
89 if ( mutex->owner == this_thread ) {
90 ++mutex->recursive;
91 } else {
92 /* The order of operations is important.
93 We set the locking thread id after we obtain the lock
94 so unlocks from other threads will fail.
95 */
96 spinlock_lock(&mutex->mutex);
97 mutex->owner = this_thread;
98 mutex->recursive = 0;
99 }
100
101 return 0;
102 #endif /* DISABLE_THREADS */
103 }
104
105 /* Unlock the mutex */
106 int SDL_mutexV(SDL_mutex *mutex)
107 {
108 #ifdef DISABLE_THREADS
109 return 0;
110 #else
111 if ( mutex == NULL ) {
112 SDL_SetError("Passed a NULL mutex");
113 return -1;
114 }
115
116 /* If we don't own the mutex, we can't unlock it */
117 if ( SDL_ThreadID() != mutex->owner ) {
118 SDL_SetError("mutex not owned by this thread");
119 return -1;
120 }
121
122 if ( mutex->recursive ) {
123 --mutex->recursive;
124 } else {
125 /* The order of operations is important.
126 First reset the owner so another thread doesn't lock
127 the mutex and set the ownership before we reset it,
128 then release the lock semaphore.
129 */
130 mutex->owner = 0;
131 spinlock_unlock(&mutex->mutex);
132 }
133 return 0;
134 #endif /* DISABLE_THREADS */
135 }