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 
22 /* Semaphore functions for the PSP. */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include "SDL_error.h"
28 #include "SDL_thread.h"
29 
30 #include <pspthreadman.h>
31 #include <pspkerror.h>
32 
33 struct SDL_semaphore {
34  SceUID semid;
35 };
36 
37 
38 /* Create a semaphore */
40 {
41  SDL_sem *sem;
42 
43  sem = (SDL_sem *) malloc(sizeof(*sem));
44  if (sem != NULL) {
45  /* TODO: Figure out the limit on the maximum value. */
46  sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
47  if (sem->semid < 0) {
48  SDL_SetError("Couldn't create semaphore");
49  free(sem);
50  sem = NULL;
51  }
52  } else {
54  }
55 
56  return sem;
57 }
58 
59 /* Free the semaphore */
61 {
62  if (sem != NULL) {
63  if (sem->semid > 0) {
64  sceKernelDeleteSema(sem->semid);
65  sem->semid = 0;
66  }
67 
68  free(sem);
69  }
70 }
71 
72 /* TODO: This routine is a bit overloaded.
73  * If the timeout is 0 then just poll the semaphore; if it's SDL_MUTEX_MAXWAIT, pass
74  * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout
75  * is specified, convert it to microseconds. */
77 {
78  Uint32 *pTimeout;
79  unsigned int res;
80 
81  if (sem == NULL) {
82  SDL_SetError("Passed a NULL sem");
83  return 0;
84  }
85 
86  if (timeout == 0) {
87  res = sceKernelPollSema(sem->semid, 1);
88  if (res < 0) {
89  return SDL_MUTEX_TIMEDOUT;
90  }
91  return 0;
92  }
93 
94  if (timeout == SDL_MUTEX_MAXWAIT) {
95  pTimeout = NULL;
96  } else {
97  timeout *= 1000; /* Convert to microseconds. */
98  pTimeout = &timeout;
99  }
100 
101  res = sceKernelWaitSema(sem->semid, 1, pTimeout);
102  switch (res) {
103  case SCE_KERNEL_ERROR_OK:
104  return 0;
105  case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
106  return SDL_MUTEX_TIMEDOUT;
107  default:
108  return SDL_SetError("WaitForSingleObject() failed");
109  }
110 }
111 
113 {
114  return SDL_SemWaitTimeout(sem, 0);
115 }
116 
118 {
120 }
121 
122 /* Returns the current count of the semaphore */
124 {
125  SceKernelSemaInfo info;
126 
127  if (sem == NULL) {
128  SDL_SetError("Passed a NULL sem");
129  return 0;
130  }
131 
132  if (sceKernelReferSemaStatus(sem->semid, &info) >= 0) {
133  return info.currentCount;
134  }
135 
136  return 0;
137 }
138 
140 {
141  int res;
142 
143  if (sem == NULL) {
144  return SDL_SetError("Passed a NULL sem");
145  }
146 
147  res = sceKernelSignalSema(sem->semid, 1);
148  if (res < 0) {
149  return SDL_SetError("sceKernelSignalSema() failed");
150  }
151 
152  return 0;
153 }
154 
155 /* vim: ts=4 sw=4
156  */
DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value)
Definition: SDL_syssem.c:85
#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
SDL_EventEntry * free
Definition: SDL_events.c:80
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
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
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
#define malloc
Definition: SDL_malloc.c:635
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem)
Definition: SDL_syssem.c:200
GLuint res
Definition: glew.h:10669