Mercurial > sdl-ios-xcode
annotate src/thread/beos/SDL_syssem.c @ 1388:9a9b87172b4b
Fixed build dependencies... ugh
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 20 Feb 2006 11:29:36 +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:
1146
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:
1146
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:
1146
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:
1146
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:
1146
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:
1146
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:
1146
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:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Semaphores in the BeOS environment */ | |
24 | |
25 #include <be/kernel/OS.h> | |
26 | |
27 #include "SDL_thread.h" | |
28 | |
29 | |
30 struct SDL_semaphore { | |
31 sem_id id; | |
32 }; | |
33 | |
34 /* Create a counting semaphore */ | |
35 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
36 { | |
37 SDL_sem *sem; | |
38 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
39 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); |
0 | 40 if ( sem ) { |
41 sem->id = create_sem(initial_value, "SDL semaphore"); | |
42 if ( sem->id < B_NO_ERROR ) { | |
43 SDL_SetError("create_sem() failed"); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
44 SDL_free(sem); |
0 | 45 sem = NULL; |
46 } | |
47 } else { | |
48 SDL_OutOfMemory(); | |
49 } | |
50 return(sem); | |
51 } | |
52 | |
53 /* Free the semaphore */ | |
54 void SDL_DestroySemaphore(SDL_sem *sem) | |
55 { | |
56 if ( sem ) { | |
57 if ( sem->id >= B_NO_ERROR ) { | |
58 delete_sem(sem->id); | |
59 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
60 SDL_free(sem); |
0 | 61 } |
62 } | |
63 | |
64 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
65 { | |
66 int32 val; | |
67 int retval; | |
68 | |
69 if ( ! sem ) { | |
70 SDL_SetError("Passed a NULL semaphore"); | |
71 return -1; | |
72 } | |
73 | |
74 tryagain: | |
75 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
76 val = acquire_sem(sem->id); | |
77 } else { | |
78 timeout *= 1000; /* BeOS uses a timeout in microseconds */ | |
79 val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout); | |
80 } | |
81 switch (val) { | |
82 case B_INTERRUPTED: | |
83 goto tryagain; | |
84 case B_NO_ERROR: | |
85 retval = 0; | |
86 break; | |
1146
ab0154afe938
Date: Sat, 17 Sep 2005 13:38:49 +0200
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
87 case B_TIMED_OUT: |
ab0154afe938
Date: Sat, 17 Sep 2005 13:38:49 +0200
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
88 retval = SDL_MUTEX_TIMEDOUT; |
ab0154afe938
Date: Sat, 17 Sep 2005 13:38:49 +0200
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
89 break; |
ab0154afe938
Date: Sat, 17 Sep 2005 13:38:49 +0200
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
90 case B_WOULD_BLOCK: |
ab0154afe938
Date: Sat, 17 Sep 2005 13:38:49 +0200
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
91 retval = SDL_MUTEX_TIMEDOUT; |
ab0154afe938
Date: Sat, 17 Sep 2005 13:38:49 +0200
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
92 break; |
0 | 93 default: |
94 SDL_SetError("acquire_sem() failed"); | |
95 retval = -1; | |
96 break; | |
97 } | |
98 | |
99 return retval; | |
100 } | |
101 | |
102 int SDL_SemTryWait(SDL_sem *sem) | |
103 { | |
104 return SDL_SemWaitTimeout(sem, 0); | |
105 } | |
106 | |
107 int SDL_SemWait(SDL_sem *sem) | |
108 { | |
109 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
110 } | |
111 | |
112 /* Returns the current count of the semaphore */ | |
113 Uint32 SDL_SemValue(SDL_sem *sem) | |
114 { | |
115 int32 count; | |
116 Uint32 value; | |
117 | |
118 value = 0; | |
119 if ( sem ) { | |
120 get_sem_count(sem->id, &count); | |
121 if ( count > 0 ) { | |
122 value = (Uint32)count; | |
123 } | |
124 } | |
125 return value; | |
126 } | |
127 | |
128 /* Atomically increases the semaphore's count (not blocking) */ | |
129 int SDL_SemPost(SDL_sem *sem) | |
130 { | |
131 if ( ! sem ) { | |
132 SDL_SetError("Passed a NULL semaphore"); | |
133 return -1; | |
134 } | |
135 | |
136 if ( release_sem(sem->id) != B_NO_ERROR ) { | |
137 SDL_SetError("release_sem() failed"); | |
138 return -1; | |
139 } | |
140 return 0; | |
141 } |