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