Mercurial > sdl-ios-xcode
annotate src/thread/win32/SDL_syssem.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | b8d311d90021 |
children | 450721ad5436 |
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 | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <windows.h> | |
28 | |
29 #include "SDL_error.h" | |
30 #include "SDL_thread.h" | |
31 #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
|
32 #include "win_ce_semaphore.h" |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
33 #endif |
0 | 34 |
35 | |
36 struct SDL_semaphore { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
37 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
38 SYNCHHANDLE id; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
39 #else |
0 | 40 HANDLE id; |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
41 #endif |
0 | 42 Uint32 volatile count; |
43 }; | |
44 | |
45 | |
46 /* Create a semaphore */ | |
47 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
48 { | |
49 SDL_sem *sem; | |
50 | |
51 /* Allocate sem memory */ | |
52 sem = (SDL_sem *)malloc(sizeof(*sem)); | |
53 if ( sem ) { | |
54 /* 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
|
55 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
56 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
|
57 #else |
0 | 58 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
|
59 #endif |
0 | 60 sem->count = initial_value; |
61 if ( ! sem->id ) { | |
62 SDL_SetError("Couldn't create semaphore"); | |
63 free(sem); | |
64 sem = NULL; | |
65 } | |
66 } else { | |
67 SDL_OutOfMemory(); | |
68 } | |
69 return(sem); | |
70 } | |
71 | |
72 /* Free the semaphore */ | |
73 void SDL_DestroySemaphore(SDL_sem *sem) | |
74 { | |
75 if ( sem ) { | |
76 if ( sem->id ) { | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
77 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
78 CloseSynchHandle(sem->id); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
79 #else |
0 | 80 CloseHandle(sem->id); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
81 #endif |
0 | 82 sem->id = 0; |
83 } | |
84 free(sem); | |
85 } | |
86 } | |
87 | |
88 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
89 { | |
90 int retval; | |
91 DWORD dwMilliseconds; | |
92 | |
93 if ( ! sem ) { | |
94 SDL_SetError("Passed a NULL sem"); | |
95 return -1; | |
96 } | |
97 | |
98 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
99 dwMilliseconds = INFINITE; | |
100 } else { | |
101 dwMilliseconds = (DWORD)timeout; | |
102 } | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
103 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
104 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
105 #else |
0 | 106 switch (WaitForSingleObject(sem->id, dwMilliseconds)) { |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
107 #endif |
0 | 108 case WAIT_OBJECT_0: |
109 --sem->count; | |
110 retval = 0; | |
111 break; | |
112 case WAIT_TIMEOUT: | |
113 retval = SDL_MUTEX_TIMEDOUT; | |
114 break; | |
115 default: | |
116 SDL_SetError("WaitForSingleObject() failed"); | |
117 retval = -1; | |
118 break; | |
119 } | |
120 return retval; | |
121 } | |
122 | |
123 int SDL_SemTryWait(SDL_sem *sem) | |
124 { | |
125 return SDL_SemWaitTimeout(sem, 0); | |
126 } | |
127 | |
128 int SDL_SemWait(SDL_sem *sem) | |
129 { | |
130 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
131 } | |
132 | |
133 /* Returns the current count of the semaphore */ | |
134 Uint32 SDL_SemValue(SDL_sem *sem) | |
135 { | |
136 if ( ! sem ) { | |
137 SDL_SetError("Passed a NULL sem"); | |
138 return 0; | |
139 } | |
140 return sem->count; | |
141 } | |
142 | |
143 int SDL_SemPost(SDL_sem *sem) | |
144 { | |
145 if ( ! sem ) { | |
146 SDL_SetError("Passed a NULL sem"); | |
147 return -1; | |
148 } | |
149 /* Increase the counter in the first place, because | |
150 * after a successful release the semaphore may | |
151 * immediately get destroyed by another thread which | |
152 * is waiting for this semaphore. | |
153 */ | |
154 ++sem->count; | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
155 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
156 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
157 #else |
0 | 158 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
|
159 #endif |
0 | 160 --sem->count; /* restore */ |
161 SDL_SetError("ReleaseSemaphore() failed"); | |
162 return -1; | |
163 } | |
164 return 0; | |
165 } |