Mercurial > sdl-ios-xcode
annotate src/thread/riscos/SDL_syssem.c @ 4139:568c9b3c0167 SDL-1.2
* Added configure option --enable-screensaver, to allow enabling the screensaver by default.
* Use XResetScreenSaver() instead of disabling screensaver entirely.
Full discussion summary from Erik on the SDL mailing list:
Current behaviour
=================
SDL changes the user's display power management settings without
permission from the user and without telling the user.
The interface that it uses to do so is DPMSDisable/DPMSEnable, which
should only ever be used by configuration utilities like KControl, never
by normal application programs, let alone by the libraries that they
use. Using an interface that is not at all intended for what SDL tries
to achieve means that it will not work as it should. Firstly, the power
management is completely disabled during the whole lifetime of the SDL
program, not only when it should be. Secondly, it makes SDL
non-reentrant, meaning that things will break when multiple SDL programs
are clients of the same X server simultaneously. Thirdly, no cleanup
mechanism ensures that the setting is restored if the client does not do
that (for example if it crashes).
In addition to that, this interface is broken on xorg,
[http://bugs.freedesktop.org/show_bug.cgi?id=13962], so what SDL tries
to do does not work at all on that implementation of the X Window
System. (The reason that the DPMSEnable works in KControl is that it
calls DPMSSetTimeout immediately after,
[http://websvn.kde.org/tags/KDE/3.5.9/kdebase/kcontrol/energy/energy.cpp?annotate=774532#l343]).
The problems that the current behaviour causes
==============================================
1. Information leak. When the user is away, someone might see what the
user has on the display when the user counts on the screensaver
preventing this. This does not even require physical access to the
workstation, it is enough to see it from a distance.
2. Draining battery. An SDL program that runs on a laptop will quickly
drain the battery while the user is away. The system will soon shut down
and require recharging before being usable again, while it should in
fact have consumed very little energy if the user's settings would have
been obeyed.
3. Wasting energy. Even if battery issues are not considered, energy as
such is wasted.
4. Display wear. The display may be worn out.
The problems that the current behaviour tries to solve
======================================================
1. Preventing screensaver while playing movies.
Many SDL applications are media players. They have reasons to prevent
screensavers from being activated while a movie is being played. When a
user clicks on the play button it can be interpreted as saying "play
this movie, but do not turn off the display while playing it, because I
will watch it even though I do not interact with the system".
2. Preventing screensaver when some input bypasses X.
Sometimes SDL uses input from another source than the X server, so
that the X server is bypassed. This obviously breaks the screensaver
handling. SDL tries to work around that.
3. Preventing screensaver when all input bypasses X.
There is something called Direct Graphics Access mode, where a
program takes control of both the display and the input devices from the
X server. This obviously means that the X server can not handle the
screensaver alone, since screensaver handling depends on input handling.
SDL does not do what it should to help the X server to handle the
screensaver. Nor does SDL take care of screeensaver handling itself. SDL
simply disables the screensaver completely.
How the problems should be solved
=================================
The correct way for an application program to prevent the screensaver
under X is to call XResetScreenSaver. This was recently discovered and
implemented by the mplayer developers,
[http://svn.mplayerhq.hu/mplayer?view=rev&revision=25637]. SDL needs to
wrap this in an API call (SDL_ResetScreenSaver) and implement it for the
other video targets (if they do not have a corresponding call, SDL
should do what it takes on that particular target, for example sending
fake key events).
1. When a movie is played, the player should reset the screensaver when
the animation is advanced to a new frame. The same applies to anything
similar, like slideshows.
2. When the X server is handling input, it must handle all input
(keyboards, mice, gamepads, ...). This is necessary, not only to be able
to handle the screensaver, but also so that it can send the events to
the correct (the currently active) client. If there is an input device
that the X server can not handle for some reason (such as lack of Plug
and Play capability), the program that handles the device as a
workaround must simulate what would happen if the X server would have
handled the device, by calling XResetScreenSaver when input is received
from the device.
3. When the X server is not handling the input, it depends on the
program that does to call XResetScreenSaver whenever an input event
occurs. Alternatively the program must handle the screensaver countdown
internally and call XActivateScreenSaver.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 29 Feb 2008 13:55:44 +0000 |
parents | 536b0704b7d8 |
children | c121d94672cb a1b03ba2fcd0 |
rev | line source |
---|---|
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
1 /* |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
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:
1099
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
4 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
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:
1099
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
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:
1099
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
9 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
10 This library is distributed in the hope that it will be useful, |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
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:
1099
diff
changeset
|
13 Lesser General Public License for more details. |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1099
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:
1099
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:
1099
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
18 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
19 Sam Lantinga |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
20 slouken@libsdl.org |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
21 */ |
1851
536b0704b7d8
Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
Ryan C. Gordon <icculus@icculus.org>
parents:
1402
diff
changeset
|
22 |
536b0704b7d8
Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
Ryan C. Gordon <icculus@icculus.org>
parents:
1402
diff
changeset
|
23 #include <errno.h> |
536b0704b7d8
Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
Ryan C. Gordon <icculus@icculus.org>
parents:
1402
diff
changeset
|
24 |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
25 #include "SDL_config.h" |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
26 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
27 /* RISC OS semiphores based on linux code */ |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
28 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
29 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
30 #include "SDL_timer.h" |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
31 #include "SDL_thread.h" |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
32 #include "SDL_systhread_c.h" |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
33 |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
34 #if !SDL_THREADS_DISABLED |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
35 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
36 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
37 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
38 SDL_SetError("SDL not configured with thread support"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
39 return (SDL_sem *)0; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
40 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
41 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
42 void SDL_DestroySemaphore(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
43 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
44 return; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
45 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
46 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
47 int SDL_SemTryWait(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
48 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
49 SDL_SetError("SDL not configured with thread support"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
50 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
51 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
52 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
53 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
54 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
55 SDL_SetError("SDL not configured with thread support"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
56 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
57 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
58 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
59 int SDL_SemWait(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
60 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
61 SDL_SetError("SDL not configured with thread support"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
62 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
63 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
64 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
65 Uint32 SDL_SemValue(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
66 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
67 return 0; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
68 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
69 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
70 int SDL_SemPost(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
71 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
72 SDL_SetError("SDL not configured with thread support"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
73 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
74 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
75 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
76 #else |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
77 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
78 |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
79 #include <unistd.h> /* For getpid() */ |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
80 #include <pthread.h> |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
81 #include <semaphore.h> |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
82 |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
83 struct SDL_semaphore { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
84 sem_t *sem; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
85 sem_t sem_data; |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
86 }; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
87 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
88 /* Create a semaphore, initialized with value */ |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
89 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
90 { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
91 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem)); |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
92 if ( sem ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
93 if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
94 SDL_SetError("sem_init() failed"); |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
95 SDL_free(sem); |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
96 sem = NULL; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
97 } else { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
98 sem->sem = &sem->sem_data; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
99 } |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
100 } else { |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
101 SDL_OutOfMemory(); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
102 } |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
103 return sem; |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
104 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
105 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
106 void SDL_DestroySemaphore(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
107 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
108 if ( sem ) { |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
109 sem_destroy(sem->sem); |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
110 SDL_free(sem); |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
111 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
112 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
113 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
114 int SDL_SemTryWait(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
115 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
116 int retval; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
117 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
118 if ( ! sem ) { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
119 SDL_SetError("Passed a NULL semaphore"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
120 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
121 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
122 retval = SDL_MUTEX_TIMEDOUT; |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
123 if ( sem_trywait(sem->sem) == 0 ) { |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
124 retval = 0; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
125 } |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
126 return retval; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
127 } |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
128 |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
129 int SDL_SemWait(SDL_sem *sem) |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
130 { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
131 int retval; |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
132 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
133 if ( ! sem ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
134 SDL_SetError("Passed a NULL semaphore"); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
135 return -1; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
136 } |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
137 |
1851
536b0704b7d8
Make sure sem_wait didn't return early with EINTR. Fixes Bugzilla #231.
Ryan C. Gordon <icculus@icculus.org>
parents:
1402
diff
changeset
|
138 while ( ((retval = sem_wait(sem->sem)) == -1) && (errno == EINTR) ) {} |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
139 if ( retval < 0 ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
140 SDL_SetError("sem_wait() failed"); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
141 } |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
142 return retval; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
143 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
144 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
145 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
146 { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
147 int retval; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
148 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
149 if ( ! sem ) { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
150 SDL_SetError("Passed a NULL semaphore"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
151 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
152 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
153 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
154 /* Try the easy cases first */ |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
155 if ( timeout == 0 ) { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
156 return SDL_SemTryWait(sem); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
157 } |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
158 if ( timeout == SDL_MUTEX_MAXWAIT ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
159 return SDL_SemWait(sem); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
160 } |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
161 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
162 /* Ack! We have to busy wait... */ |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
163 timeout += SDL_GetTicks(); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
164 do { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
165 retval = SDL_SemTryWait(sem); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
166 if ( retval == 0 ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
167 break; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
168 } |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
169 SDL_Delay(1); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
170 } while ( SDL_GetTicks() < timeout ); |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
171 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
172 return retval; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
173 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
174 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
175 Uint32 SDL_SemValue(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
176 { |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
177 int ret = 0; |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
178 if ( sem ) { |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
179 sem_getvalue(sem->sem, &ret); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
180 if ( ret < 0 ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
181 ret = 0; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
182 } |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
183 } |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
184 return (Uint32)ret; |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
185 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
186 |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
187 int SDL_SemPost(SDL_sem *sem) |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
188 { |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
189 int retval; |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
190 |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
191 if ( ! sem ) { |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
192 SDL_SetError("Passed a NULL semaphore"); |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
193 return -1; |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
194 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
195 |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
196 retval = sem_post(sem->sem); |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
197 if ( retval < 0 ) { |
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
198 SDL_SetError("sem_post() failed"); |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
199 } |
1099
1ccbb2b7d905
Date: Fri, 15 Jul 2005 08:29:01 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
955
diff
changeset
|
200 return retval; |
955
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
201 } |
d74fbf56f2f6
Date: Fri, 25 Jun 2004 13:29:15 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
diff
changeset
|
202 |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
203 #endif /* !SDL_THREADS_DISABLED */ |