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