Mercurial > sdl-ios-xcode
annotate src/thread/win32/SDL_syssem.c @ 1386:2b7e0180a72c
Pick the right default GL library on IRIX
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 20 Feb 2006 10:19:22 +0000 |
parents | c71e05b4dc2e |
children | d910939febfa |
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 */ |
22 | |
23 /* Semaphore functions using the Win32 API */ | |
24 | |
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
|
25 #include "SDL_windows.h" |
0 | 26 |
27 #include "SDL_thread.h" | |
28 #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
|
29 #include "win_ce_semaphore.h" |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
30 #endif |
0 | 31 |
32 | |
33 struct SDL_semaphore { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
34 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
35 SYNCHHANDLE id; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
36 #else |
0 | 37 HANDLE id; |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
38 #endif |
0 | 39 Uint32 volatile count; |
40 }; | |
41 | |
42 | |
43 /* Create a semaphore */ | |
44 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
45 { | |
46 SDL_sem *sem; | |
47 | |
48 /* Allocate sem memory */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
49 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); |
0 | 50 if ( sem ) { |
51 /* 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
|
52 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
53 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
|
54 #else |
0 | 55 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
|
56 #endif |
0 | 57 sem->count = initial_value; |
58 if ( ! sem->id ) { | |
59 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
|
60 SDL_free(sem); |
0 | 61 sem = NULL; |
62 } | |
63 } else { | |
64 SDL_OutOfMemory(); | |
65 } | |
66 return(sem); | |
67 } | |
68 | |
69 /* Free the semaphore */ | |
70 void SDL_DestroySemaphore(SDL_sem *sem) | |
71 { | |
72 if ( sem ) { | |
73 if ( sem->id ) { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
74 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
75 CloseSynchHandle(sem->id); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
76 #else |
0 | 77 CloseHandle(sem->id); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
78 #endif |
0 | 79 sem->id = 0; |
80 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
81 SDL_free(sem); |
0 | 82 } |
83 } | |
84 | |
85 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
86 { | |
87 int retval; | |
88 DWORD dwMilliseconds; | |
89 | |
90 if ( ! sem ) { | |
91 SDL_SetError("Passed a NULL sem"); | |
92 return -1; | |
93 } | |
94 | |
95 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
96 dwMilliseconds = INFINITE; | |
97 } else { | |
98 dwMilliseconds = (DWORD)timeout; | |
99 } | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
100 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
101 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
102 #else |
0 | 103 switch (WaitForSingleObject(sem->id, dwMilliseconds)) { |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
104 #endif |
0 | 105 case WAIT_OBJECT_0: |
106 --sem->count; | |
107 retval = 0; | |
108 break; | |
109 case WAIT_TIMEOUT: | |
110 retval = SDL_MUTEX_TIMEDOUT; | |
111 break; | |
112 default: | |
113 SDL_SetError("WaitForSingleObject() failed"); | |
114 retval = -1; | |
115 break; | |
116 } | |
117 return retval; | |
118 } | |
119 | |
120 int SDL_SemTryWait(SDL_sem *sem) | |
121 { | |
122 return SDL_SemWaitTimeout(sem, 0); | |
123 } | |
124 | |
125 int SDL_SemWait(SDL_sem *sem) | |
126 { | |
127 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
128 } | |
129 | |
130 /* Returns the current count of the semaphore */ | |
131 Uint32 SDL_SemValue(SDL_sem *sem) | |
132 { | |
133 if ( ! sem ) { | |
134 SDL_SetError("Passed a NULL sem"); | |
135 return 0; | |
136 } | |
137 return sem->count; | |
138 } | |
139 | |
140 int SDL_SemPost(SDL_sem *sem) | |
141 { | |
142 if ( ! sem ) { | |
143 SDL_SetError("Passed a NULL sem"); | |
144 return -1; | |
145 } | |
146 /* Increase the counter in the first place, because | |
147 * after a successful release the semaphore may | |
148 * immediately get destroyed by another thread which | |
149 * is waiting for this semaphore. | |
150 */ | |
151 ++sem->count; | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
152 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
153 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
154 #else |
0 | 155 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
|
156 #endif |
0 | 157 --sem->count; /* restore */ |
158 SDL_SetError("ReleaseSemaphore() failed"); | |
159 return -1; | |
160 } | |
161 return 0; | |
162 } |