comparison src/thread/riscos/SDL_sysmutex.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
39 pthread_t owner; 39 pthread_t owner;
40 #endif 40 #endif
41 }; 41 };
42 42
43 SDL_mutex * 43 SDL_mutex *
44 SDL_CreateMutex (void) 44 SDL_CreateMutex(void)
45 { 45 {
46 SDL_mutex *mutex; 46 SDL_mutex *mutex;
47 pthread_mutexattr_t attr; 47 pthread_mutexattr_t attr;
48 48
49 /* Allocate the structure */ 49 /* Allocate the structure */
50 mutex = (SDL_mutex *) SDL_calloc (1, sizeof (*mutex)); 50 mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
51 if (mutex) { 51 if (mutex) {
52 pthread_mutexattr_init (&attr); 52 pthread_mutexattr_init(&attr);
53 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX 53 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
54 /* No extra attributes necessary */ 54 /* No extra attributes necessary */
55 #else 55 #else
56 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); 56 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
57 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */ 57 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
58 if (pthread_mutex_init (&mutex->id, &attr) != 0) { 58 if (pthread_mutex_init(&mutex->id, &attr) != 0) {
59 SDL_SetError ("pthread_mutex_init() failed"); 59 SDL_SetError("pthread_mutex_init() failed");
60 SDL_free (mutex); 60 SDL_free(mutex);
61 mutex = NULL; 61 mutex = NULL;
62 } 62 }
63 } else { 63 } else {
64 SDL_OutOfMemory (); 64 SDL_OutOfMemory();
65 } 65 }
66 return (mutex); 66 return (mutex);
67 } 67 }
68 68
69 void 69 void
70 SDL_DestroyMutex (SDL_mutex * mutex) 70 SDL_DestroyMutex(SDL_mutex * mutex)
71 { 71 {
72 if (mutex) { 72 if (mutex) {
73 pthread_mutex_destroy (&mutex->id); 73 pthread_mutex_destroy(&mutex->id);
74 SDL_free (mutex); 74 SDL_free(mutex);
75 } 75 }
76 } 76 }
77 77
78 /* Lock the mutex */ 78 /* Lock the mutex */
79 int 79 int
80 SDL_mutexP (SDL_mutex * mutex) 80 SDL_mutexP(SDL_mutex * mutex)
81 { 81 {
82 int retval; 82 int retval;
83 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX 83 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
84 pthread_t this_thread; 84 pthread_t this_thread;
85 #endif 85 #endif
86 86
87 if (mutex == NULL) { 87 if (mutex == NULL) {
88 SDL_SetError ("Passed a NULL mutex"); 88 SDL_SetError("Passed a NULL mutex");
89 return -1; 89 return -1;
90 } 90 }
91 91
92 retval = 0; 92 retval = 0;
93 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX 93 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
94 this_thread = pthread_self (); 94 this_thread = pthread_self();
95 if (mutex->owner == this_thread) { 95 if (mutex->owner == this_thread) {
96 ++mutex->recursive; 96 ++mutex->recursive;
97 } else { 97 } else {
98 /* The order of operations is important. 98 /* The order of operations is important.
99 We set the locking thread id after we obtain the lock 99 We set the locking thread id after we obtain the lock
100 so unlocks from other threads will fail. 100 so unlocks from other threads will fail.
101 */ 101 */
102 if (pthread_mutex_lock (&mutex->id) == 0) { 102 if (pthread_mutex_lock(&mutex->id) == 0) {
103 mutex->owner = this_thread; 103 mutex->owner = this_thread;
104 mutex->recursive = 0; 104 mutex->recursive = 0;
105 } else { 105 } else {
106 SDL_SetError ("pthread_mutex_lock() failed"); 106 SDL_SetError("pthread_mutex_lock() failed");
107 retval = -1; 107 retval = -1;
108 } 108 }
109 } 109 }
110 #else 110 #else
111 if (pthread_mutex_lock (&mutex->id) < 0) { 111 if (pthread_mutex_lock(&mutex->id) < 0) {
112 SDL_SetError ("pthread_mutex_lock() failed"); 112 SDL_SetError("pthread_mutex_lock() failed");
113 retval = -1; 113 retval = -1;
114 } 114 }
115 #endif 115 #endif
116 return retval; 116 return retval;
117 } 117 }
118 118
119 int 119 int
120 SDL_mutexV (SDL_mutex * mutex) 120 SDL_mutexV(SDL_mutex * mutex)
121 { 121 {
122 int retval; 122 int retval;
123 123
124 if (mutex == NULL) { 124 if (mutex == NULL) {
125 SDL_SetError ("Passed a NULL mutex"); 125 SDL_SetError("Passed a NULL mutex");
126 return -1; 126 return -1;
127 } 127 }
128 128
129 retval = 0; 129 retval = 0;
130 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX 130 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
131 /* We can only unlock the mutex if we own it */ 131 /* We can only unlock the mutex if we own it */
132 if (pthread_self () == mutex->owner) { 132 if (pthread_self() == mutex->owner) {
133 if (mutex->recursive) { 133 if (mutex->recursive) {
134 --mutex->recursive; 134 --mutex->recursive;
135 } else { 135 } else {
136 /* The order of operations is important. 136 /* The order of operations is important.
137 First reset the owner so another thread doesn't lock 137 First reset the owner so another thread doesn't lock
138 the mutex and set the ownership before we reset it, 138 the mutex and set the ownership before we reset it,
139 then release the lock semaphore. 139 then release the lock semaphore.
140 */ 140 */
141 mutex->owner = 0; 141 mutex->owner = 0;
142 pthread_mutex_unlock (&mutex->id); 142 pthread_mutex_unlock(&mutex->id);
143 } 143 }
144 } else { 144 } else {
145 SDL_SetError ("mutex not owned by this thread"); 145 SDL_SetError("mutex not owned by this thread");
146 retval = -1; 146 retval = -1;
147 } 147 }
148 148
149 #else 149 #else
150 if (pthread_mutex_unlock (&mutex->id) < 0) { 150 if (pthread_mutex_unlock(&mutex->id) < 0) {
151 SDL_SetError ("pthread_mutex_unlock() failed"); 151 SDL_SetError("pthread_mutex_unlock() failed");
152 retval = -1; 152 retval = -1;
153 } 153 }
154 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */ 154 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
155 155
156 return retval; 156 return retval;