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