zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_syssem.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22 
23 #ifdef SDL_THREAD_BEOS
24 
25 /* Semaphores in the BeOS environment */
26 
27 #include <be/kernel/OS.h>
28 
29 #include "SDL_thread.h"
30 
31 
32 struct SDL_semaphore
33 {
34  sem_id id;
35 };
36 
37 /* Create a counting semaphore */
38 SDL_sem *
39 SDL_CreateSemaphore(Uint32 initial_value)
40 {
41  SDL_sem *sem;
42 
43  sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
44  if (sem) {
45  sem->id = create_sem(initial_value, "SDL semaphore");
46  if (sem->id < B_NO_ERROR) {
47  SDL_SetError("create_sem() failed");
48  SDL_free(sem);
49  sem = NULL;
50  }
51  } else {
53  }
54  return (sem);
55 }
56 
57 /* Free the semaphore */
58 void
60 {
61  if (sem) {
62  if (sem->id >= B_NO_ERROR) {
63  delete_sem(sem->id);
64  }
65  SDL_free(sem);
66  }
67 }
68 
69 int
71 {
72  int32 val;
73  int retval;
74 
75  if (!sem) {
76  return SDL_SetError("Passed a NULL semaphore");
77  }
78 
79  tryagain:
80  if (timeout == SDL_MUTEX_MAXWAIT) {
81  val = acquire_sem(sem->id);
82  } else {
83  timeout *= 1000; /* BeOS uses a timeout in microseconds */
84  val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout);
85  }
86  switch (val) {
87  case B_INTERRUPTED:
88  goto tryagain;
89  case B_NO_ERROR:
90  retval = 0;
91  break;
92  case B_TIMED_OUT:
93  retval = SDL_MUTEX_TIMEDOUT;
94  break;
95  case B_WOULD_BLOCK:
96  retval = SDL_MUTEX_TIMEDOUT;
97  break;
98  default:
99  retval = SDL_SetError("acquire_sem() failed");
100  break;
101  }
102 
103  return retval;
104 }
105 
106 int
107 SDL_SemTryWait(SDL_sem * sem)
108 {
109  return SDL_SemWaitTimeout(sem, 0);
110 }
111 
112 int
113 SDL_SemWait(SDL_sem * sem)
114 {
116 }
117 
118 /* Returns the current count of the semaphore */
119 Uint32
120 SDL_SemValue(SDL_sem * sem)
121 {
122  int32 count;
123  Uint32 value;
124 
125  value = 0;
126  if (sem) {
127  get_sem_count(sem->id, &count);
128  if (count > 0) {
129  value = (Uint32) count;
130  }
131  }
132  return value;
133 }
134 
135 /* Atomically increases the semaphore's count (not blocking) */
136 int
137 SDL_SemPost(SDL_sem * sem)
138 {
139  if (!sem) {
140  return SDL_SetError("Passed a NULL semaphore");
141  }
142 
143  if (release_sem(sem->id) != B_NO_ERROR) {
144  return SDL_SetError("release_sem() failed");
145  }
146  return 0;
147 }
148 
149 #endif /* SDL_THREAD_BEOS */
150 
151 /* vi: set ts=4 sw=4 expandtab: */
DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value)
Definition: SDL_syssem.c:85
GLuint const GLfloat * val
Definition: glew.h:2715
#define SDL_MUTEX_MAXWAIT
Definition: SDL_mutex.h:49
DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem)
Definition: SDL_syssem.c:130
struct SDL_semaphore SDL_sem
Definition: SDL_mutex.h:107
#define NULL
Definition: ftobjs.h:61
DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 ms)
Definition: SDL_syssem.c:150
DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem)
Definition: SDL_syssem.c:180
DECLSPEC void SDLCALL SDL_free(void *mem)
GLuint id
Definition: gl2ext.h:1142
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem)
Definition: SDL_syssem.c:186
GLint GLsizei count
Definition: gl2ext.h:1011
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
GLbitfield GLuint64 timeout
Definition: glew.h:5938
DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem)
Definition: SDL_syssem.c:111
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
EGLSurface EGLint void ** value
Definition: eglext.h:301
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem)
Definition: SDL_syssem.c:200