Mercurial > sdl-ios-xcode
comparison src/thread/dc/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 |
---|---|
35 spinlock_t mutex; | 35 spinlock_t mutex; |
36 }; | 36 }; |
37 | 37 |
38 /* Create a mutex */ | 38 /* Create a mutex */ |
39 SDL_mutex * | 39 SDL_mutex * |
40 SDL_CreateMutex (void) | 40 SDL_CreateMutex(void) |
41 { | 41 { |
42 SDL_mutex *mutex; | 42 SDL_mutex *mutex; |
43 | 43 |
44 /* Allocate mutex memory */ | 44 /* Allocate mutex memory */ |
45 mutex = (SDL_mutex *) SDL_malloc (sizeof (*mutex)); | 45 mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex)); |
46 if (mutex) { | 46 if (mutex) { |
47 spinlock_init (&mutex->mutex); | 47 spinlock_init(&mutex->mutex); |
48 mutex->recursive = 0; | 48 mutex->recursive = 0; |
49 mutex->owner = 0; | 49 mutex->owner = 0; |
50 } else { | 50 } else { |
51 SDL_OutOfMemory (); | 51 SDL_OutOfMemory(); |
52 } | 52 } |
53 return mutex; | 53 return mutex; |
54 } | 54 } |
55 | 55 |
56 /* Free the mutex */ | 56 /* Free the mutex */ |
57 void | 57 void |
58 SDL_DestroyMutex (SDL_mutex * mutex) | 58 SDL_DestroyMutex(SDL_mutex * mutex) |
59 { | 59 { |
60 if (mutex) { | 60 if (mutex) { |
61 SDL_free (mutex); | 61 SDL_free(mutex); |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 /* Lock the semaphore */ | 65 /* Lock the semaphore */ |
66 int | 66 int |
67 SDL_mutexP (SDL_mutex * mutex) | 67 SDL_mutexP(SDL_mutex * mutex) |
68 { | 68 { |
69 #if SDL_THREADS_DISABLED | 69 #if SDL_THREADS_DISABLED |
70 return SDL_arraysize (return), 0; | 70 return SDL_arraysize(return), 0; |
71 #else | 71 #else |
72 Uint32 this_thread; | 72 Uint32 this_thread; |
73 | 73 |
74 if (mutex == NULL) { | 74 if (mutex == NULL) { |
75 SDL_SetError ("Passed a NULL mutex"); | 75 SDL_SetError("Passed a NULL mutex"); |
76 return -1; | 76 return -1; |
77 } | 77 } |
78 | 78 |
79 this_thread = SDL_ThreadID (); | 79 this_thread = SDL_ThreadID(); |
80 if (mutex->owner == this_thread) { | 80 if (mutex->owner == this_thread) { |
81 ++mutex->recursive; | 81 ++mutex->recursive; |
82 } else { | 82 } else { |
83 /* The order of operations is important. | 83 /* The order of operations is important. |
84 We set the locking thread id after we obtain the lock | 84 We set the locking thread id after we obtain the lock |
85 so unlocks from other threads will fail. | 85 so unlocks from other threads will fail. |
86 */ | 86 */ |
87 spinlock_lock (&mutex->mutex); | 87 spinlock_lock(&mutex->mutex); |
88 mutex->owner = this_thread; | 88 mutex->owner = this_thread; |
89 mutex->recursive = 0; | 89 mutex->recursive = 0; |
90 } | 90 } |
91 | 91 |
92 return 0; | 92 return 0; |
93 #endif /* SDL_THREADS_DISABLED */ | 93 #endif /* SDL_THREADS_DISABLED */ |
94 } | 94 } |
95 | 95 |
96 /* Unlock the mutex */ | 96 /* Unlock the mutex */ |
97 int | 97 int |
98 SDL_mutexV (SDL_mutex * mutex) | 98 SDL_mutexV(SDL_mutex * mutex) |
99 { | 99 { |
100 #if SDL_THREADS_DISABLED | 100 #if SDL_THREADS_DISABLED |
101 return 0; | 101 return 0; |
102 #else | 102 #else |
103 if (mutex == NULL) { | 103 if (mutex == NULL) { |
104 SDL_SetError ("Passed a NULL mutex"); | 104 SDL_SetError("Passed a NULL mutex"); |
105 return -1; | 105 return -1; |
106 } | 106 } |
107 | 107 |
108 /* If we don't own the mutex, we can't unlock it */ | 108 /* If we don't own the mutex, we can't unlock it */ |
109 if (SDL_ThreadID () != mutex->owner) { | 109 if (SDL_ThreadID() != mutex->owner) { |
110 SDL_SetError ("mutex not owned by this thread"); | 110 SDL_SetError("mutex not owned by this thread"); |
111 return -1; | 111 return -1; |
112 } | 112 } |
113 | 113 |
114 if (mutex->recursive) { | 114 if (mutex->recursive) { |
115 --mutex->recursive; | 115 --mutex->recursive; |
118 First reset the owner so another thread doesn't lock | 118 First reset the owner so another thread doesn't lock |
119 the mutex and set the ownership before we reset it, | 119 the mutex and set the ownership before we reset it, |
120 then release the lock semaphore. | 120 then release the lock semaphore. |
121 */ | 121 */ |
122 mutex->owner = 0; | 122 mutex->owner = 0; |
123 spinlock_unlock (&mutex->mutex); | 123 spinlock_unlock(&mutex->mutex); |
124 } | 124 } |
125 return 0; | 125 return 0; |
126 #endif /* SDL_THREADS_DISABLED */ | 126 #endif /* SDL_THREADS_DISABLED */ |
127 } | 127 } |
128 | 128 |