src/thread/psp/SDL_syssem.c
author Captain Lex <kimonline@126.com>
Sun, 17 Mar 2013 20:07:02 +0800
changeset 7009 161b7b6a5303
child 7037 3fedf1f25b94
permissions -rw-r--r--
Add PSP support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     1
/*
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     2
  Simple DirectMedia Layer
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     4
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     7
  arising from the use of this software.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     8
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    12
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    16
     appreciated but is not required.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    18
     misrepresented as being the original software.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    20
*/
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    21
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    22
/* Semaphore functions for the PSP. */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    23
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    24
#include <stdio.h>
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    25
#include <stdlib.h>
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    26
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    27
#include "SDL_error.h"
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    28
#include "SDL_thread.h"
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    29
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    30
#include <pspthreadman.h>
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    31
#include <pspkerror.h>
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    32
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    33
struct SDL_semaphore {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    34
	SceUID	semid;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    35
};
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    36
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    37
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    38
/* Create a semaphore */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    39
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    40
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    41
	SDL_sem *sem;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    42
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    43
	sem = (SDL_sem *) malloc(sizeof(*sem));
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    44
	if (sem != NULL) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    45
		/* TODO: Figure out the limit on the maximum value. */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    46
		sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    47
		if (sem->semid < 0) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    48
			SDL_SetError("Couldn't create semaphore");
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    49
			free(sem);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    50
			sem = NULL;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    51
		}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    52
	} else {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    53
		SDL_OutOfMemory();
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    54
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    55
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    56
	return sem;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    57
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    58
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    59
/* Free the semaphore */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    60
void SDL_DestroySemaphore(SDL_sem *sem)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    61
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    62
	if (sem != NULL) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    63
		if (sem->semid > 0) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    64
			sceKernelDeleteSema(sem->semid);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    65
			sem->semid = 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    66
		}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    67
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    68
		free(sem);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    69
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    70
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    71
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    72
/* TODO: This routine is a bit overloaded.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    73
 * If the timeout is 0 then just poll the semaphore; if it's SDL_MUTEX_MAXWAIT, pass
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    74
 * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    75
 * is specified, convert it to microseconds. */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    76
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    77
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    78
	Uint32 *pTimeout;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    79
       unsigned int res;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    80
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    81
	if (sem == NULL) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    82
		SDL_SetError("Passed a NULL sem");
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    83
		return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    84
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    85
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    86
	if (timeout == 0) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    87
		res = sceKernelPollSema(sem->semid, 1);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    88
		if (res < 0) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    89
			return SDL_MUTEX_TIMEDOUT;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    90
		}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    91
		return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    92
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    93
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    94
	if (timeout == SDL_MUTEX_MAXWAIT) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    95
		pTimeout = NULL;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    96
	} else {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    97
		timeout *= 1000;  /* Convert to microseconds. */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    98
		pTimeout = &timeout;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    99
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   100
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   101
	res = sceKernelWaitSema(sem->semid, 1, pTimeout);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   102
       switch (res) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   103
               case SCE_KERNEL_ERROR_OK:
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   104
                       return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   105
               case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   106
                       return SDL_MUTEX_TIMEDOUT;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   107
               default:
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   108
                       SDL_SetError("WaitForSingleObject() failed");
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   109
                       return -1;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   110
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   111
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   112
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   113
int SDL_SemTryWait(SDL_sem *sem)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   114
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   115
	return SDL_SemWaitTimeout(sem, 0);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   116
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   117
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   118
int SDL_SemWait(SDL_sem *sem)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   119
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   120
	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   121
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   122
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   123
/* Returns the current count of the semaphore */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   124
Uint32 SDL_SemValue(SDL_sem *sem)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   125
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   126
	SceKernelSemaInfo info;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   127
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   128
	if (sem == NULL) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   129
		SDL_SetError("Passed a NULL sem");
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   130
		return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   131
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   132
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   133
	if (sceKernelReferSemaStatus(sem->semid, &info) >= 0) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   134
		return info.currentCount;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   135
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   136
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   137
	return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   138
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   139
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   140
int SDL_SemPost(SDL_sem *sem)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   141
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   142
	int res;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   143
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   144
	if (sem == NULL) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   145
		SDL_SetError("Passed a NULL sem");
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   146
		return -1;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   147
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   148
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   149
	res = sceKernelSignalSema(sem->semid, 1);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   150
	if (res < 0) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   151
		SDL_SetError("sceKernelSignalSema() failed");
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   152
		return -1;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   153
	}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   154
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   155
	return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   156
}
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   157
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   158
/* vim: ts=4 sw=4
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   159
 */