cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_syssem.c (3854B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 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
     33struct SDL_semaphore {
     34    SceUID  semid;
     35};
     36
     37
     38/* Create a semaphore */
     39SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
     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 {
     53        SDL_OutOfMemory();
     54    }
     55
     56    return sem;
     57}
     58
     59/* Free the semaphore */
     60void SDL_DestroySemaphore(SDL_sem *sem)
     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. */
     76int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
     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
    112int SDL_SemTryWait(SDL_sem *sem)
    113{
    114    return SDL_SemWaitTimeout(sem, 0);
    115}
    116
    117int SDL_SemWait(SDL_sem *sem)
    118{
    119    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
    120}
    121
    122/* Returns the current count of the semaphore */
    123Uint32 SDL_SemValue(SDL_sem *sem)
    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
    139int SDL_SemPost(SDL_sem *sem)
    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 */