Mercurial > sdl-ios-xcode
annotate src/thread/win32/SDL_syssem.c @ 1429:aff0170f9f1b
Date: Sat, 25 Feb 2006 14:26:19 +0100 (CET)
From: "Torsten Giebl"
Subject: [SDL] testpalette.diff
To get rid of Mousecursor Fullscreen problems on Mac OSX.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 25 Feb 2006 15:01:44 +0000 |
parents | d910939febfa |
children | bb6839704ed6 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
36
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* Semaphore functions using the Win32 API */ | |
25 | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
26 #include "SDL_windows.h" |
0 | 27 |
28 #include "SDL_thread.h" | |
29 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
30 #include "win_ce_semaphore.h" |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
31 #endif |
0 | 32 |
33 | |
34 struct SDL_semaphore { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
35 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
36 SYNCHHANDLE id; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
37 #else |
0 | 38 HANDLE id; |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
39 #endif |
0 | 40 Uint32 volatile count; |
41 }; | |
42 | |
43 | |
44 /* Create a semaphore */ | |
45 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
46 { | |
47 SDL_sem *sem; | |
48 | |
49 /* Allocate sem memory */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
50 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); |
0 | 51 if ( sem ) { |
52 /* Create the semaphore, with max value 32K */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
53 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
54 sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
55 #else |
0 | 56 sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
57 #endif |
0 | 58 sem->count = initial_value; |
59 if ( ! sem->id ) { | |
60 SDL_SetError("Couldn't create semaphore"); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
61 SDL_free(sem); |
0 | 62 sem = NULL; |
63 } | |
64 } else { | |
65 SDL_OutOfMemory(); | |
66 } | |
67 return(sem); | |
68 } | |
69 | |
70 /* Free the semaphore */ | |
71 void SDL_DestroySemaphore(SDL_sem *sem) | |
72 { | |
73 if ( sem ) { | |
74 if ( sem->id ) { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
75 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
76 CloseSynchHandle(sem->id); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
77 #else |
0 | 78 CloseHandle(sem->id); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
79 #endif |
0 | 80 sem->id = 0; |
81 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
82 SDL_free(sem); |
0 | 83 } |
84 } | |
85 | |
86 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
87 { | |
88 int retval; | |
89 DWORD dwMilliseconds; | |
90 | |
91 if ( ! sem ) { | |
92 SDL_SetError("Passed a NULL sem"); | |
93 return -1; | |
94 } | |
95 | |
96 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
97 dwMilliseconds = INFINITE; | |
98 } else { | |
99 dwMilliseconds = (DWORD)timeout; | |
100 } | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
101 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
102 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
103 #else |
0 | 104 switch (WaitForSingleObject(sem->id, dwMilliseconds)) { |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
105 #endif |
0 | 106 case WAIT_OBJECT_0: |
107 --sem->count; | |
108 retval = 0; | |
109 break; | |
110 case WAIT_TIMEOUT: | |
111 retval = SDL_MUTEX_TIMEDOUT; | |
112 break; | |
113 default: | |
114 SDL_SetError("WaitForSingleObject() failed"); | |
115 retval = -1; | |
116 break; | |
117 } | |
118 return retval; | |
119 } | |
120 | |
121 int SDL_SemTryWait(SDL_sem *sem) | |
122 { | |
123 return SDL_SemWaitTimeout(sem, 0); | |
124 } | |
125 | |
126 int SDL_SemWait(SDL_sem *sem) | |
127 { | |
128 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
129 } | |
130 | |
131 /* Returns the current count of the semaphore */ | |
132 Uint32 SDL_SemValue(SDL_sem *sem) | |
133 { | |
134 if ( ! sem ) { | |
135 SDL_SetError("Passed a NULL sem"); | |
136 return 0; | |
137 } | |
138 return sem->count; | |
139 } | |
140 | |
141 int SDL_SemPost(SDL_sem *sem) | |
142 { | |
143 if ( ! sem ) { | |
144 SDL_SetError("Passed a NULL sem"); | |
145 return -1; | |
146 } | |
147 /* Increase the counter in the first place, because | |
148 * after a successful release the semaphore may | |
149 * immediately get destroyed by another thread which | |
150 * is waiting for this semaphore. | |
151 */ | |
152 ++sem->count; | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
153 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
154 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
155 #else |
0 | 156 if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) { |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
157 #endif |
0 | 158 --sem->count; /* restore */ |
159 SDL_SetError("ReleaseSemaphore() failed"); | |
160 return -1; | |
161 } | |
162 return 0; | |
163 } |