src/thread/psp/SDL_sysmutex.c
author Sam Lantinga
Fri, 27 Jan 2017 21:23:27 -0800
changeset 10861 71d8f9afb690
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed bug 3569 - GL_UpdateViewport leaves PROJECTION matrix selected

Tom Seddon

GL_ActivateRenderer may call GL_UpdateViewport, which leaves the GL_PROJECTION matrix selected. But after GL_ResetState, the GL_MODELVIEW matrix is selected, suggesting that's the intended default state.

It seems at least like these should be consistent. Presumably GL_UpdateViewport should be doing a glMatrixMode(GL_MODELVIEW) before it finishes.
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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
kimonline@7009
    22
philipp@9329
    23
#if SDL_THREAD_PSP
philipp@9329
    24
kimonline@7009
    25
/* An implementation of mutexes using semaphores */
kimonline@7009
    26
kimonline@7009
    27
#include "SDL_thread.h"
kimonline@7009
    28
#include "SDL_systhread_c.h"
kimonline@7009
    29
kimonline@7009
    30
kimonline@7009
    31
struct SDL_mutex
kimonline@7009
    32
{
kimonline@7009
    33
    int recursive;
kimonline@7009
    34
    SDL_threadID owner;
kimonline@7009
    35
    SDL_sem *sem;
kimonline@7009
    36
};
kimonline@7009
    37
kimonline@7009
    38
/* Create a mutex */
kimonline@7009
    39
SDL_mutex *
kimonline@7009
    40
SDL_CreateMutex(void)
kimonline@7009
    41
{
kimonline@7009
    42
    SDL_mutex *mutex;
kimonline@7009
    43
kimonline@7009
    44
    /* Allocate mutex memory */
kimonline@7009
    45
    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
kimonline@7009
    46
    if (mutex) {
kimonline@7009
    47
        /* Create the mutex semaphore, with initial value 1 */
kimonline@7009
    48
        mutex->sem = SDL_CreateSemaphore(1);
kimonline@7009
    49
        mutex->recursive = 0;
kimonline@7009
    50
        mutex->owner = 0;
kimonline@7009
    51
        if (!mutex->sem) {
kimonline@7009
    52
            SDL_free(mutex);
kimonline@7009
    53
            mutex = NULL;
kimonline@7009
    54
        }
kimonline@7009
    55
    } else {
kimonline@7009
    56
        SDL_OutOfMemory();
kimonline@7009
    57
    }
kimonline@7009
    58
    return mutex;
kimonline@7009
    59
}
kimonline@7009
    60
kimonline@7009
    61
/* Free the mutex */
kimonline@7009
    62
void
kimonline@7009
    63
SDL_DestroyMutex(SDL_mutex * mutex)
kimonline@7009
    64
{
kimonline@7009
    65
    if (mutex) {
kimonline@7009
    66
        if (mutex->sem) {
kimonline@7009
    67
            SDL_DestroySemaphore(mutex->sem);
kimonline@7009
    68
        }
kimonline@7009
    69
        SDL_free(mutex);
kimonline@7009
    70
    }
kimonline@7009
    71
}
kimonline@7009
    72
kimonline@7009
    73
/* Lock the semaphore */
kimonline@7009
    74
int
kimonline@7009
    75
SDL_mutexP(SDL_mutex * mutex)
kimonline@7009
    76
{
kimonline@7009
    77
#if SDL_THREADS_DISABLED
kimonline@7009
    78
    return 0;
kimonline@7009
    79
#else
kimonline@7009
    80
    SDL_threadID this_thread;
kimonline@7009
    81
kimonline@7009
    82
    if (mutex == NULL) {
icculus@7037
    83
        return SDL_SetError("Passed a NULL mutex");
kimonline@7009
    84
    }
kimonline@7009
    85
kimonline@7009
    86
    this_thread = SDL_ThreadID();
kimonline@7009
    87
    if (mutex->owner == this_thread) {
kimonline@7009
    88
        ++mutex->recursive;
kimonline@7009
    89
    } else {
kimonline@7009
    90
        /* The order of operations is important.
kimonline@7009
    91
           We set the locking thread id after we obtain the lock
kimonline@7009
    92
           so unlocks from other threads will fail.
kimonline@7009
    93
         */
kimonline@7009
    94
        SDL_SemWait(mutex->sem);
kimonline@7009
    95
        mutex->owner = this_thread;
kimonline@7009
    96
        mutex->recursive = 0;
kimonline@7009
    97
    }
kimonline@7009
    98
kimonline@7009
    99
    return 0;
kimonline@7009
   100
#endif /* SDL_THREADS_DISABLED */
kimonline@7009
   101
}
kimonline@7009
   102
kimonline@7009
   103
/* Unlock the mutex */
kimonline@7009
   104
int
kimonline@7009
   105
SDL_mutexV(SDL_mutex * mutex)
kimonline@7009
   106
{
kimonline@7009
   107
#if SDL_THREADS_DISABLED
kimonline@7009
   108
    return 0;
kimonline@7009
   109
#else
kimonline@7009
   110
    if (mutex == NULL) {
icculus@7037
   111
        return SDL_SetError("Passed a NULL mutex");
kimonline@7009
   112
    }
kimonline@7009
   113
kimonline@7009
   114
    /* If we don't own the mutex, we can't unlock it */
kimonline@7009
   115
    if (SDL_ThreadID() != mutex->owner) {
icculus@7037
   116
        return SDL_SetError("mutex not owned by this thread");
kimonline@7009
   117
    }
kimonline@7009
   118
kimonline@7009
   119
    if (mutex->recursive) {
kimonline@7009
   120
        --mutex->recursive;
kimonline@7009
   121
    } else {
kimonline@7009
   122
        /* The order of operations is important.
kimonline@7009
   123
           First reset the owner so another thread doesn't lock
kimonline@7009
   124
           the mutex and set the ownership before we reset it,
kimonline@7009
   125
           then release the lock semaphore.
kimonline@7009
   126
         */
kimonline@7009
   127
        mutex->owner = 0;
kimonline@7009
   128
        SDL_SemPost(mutex->sem);
kimonline@7009
   129
    }
kimonline@7009
   130
    return 0;
kimonline@7009
   131
#endif /* SDL_THREADS_DISABLED */
kimonline@7009
   132
}
kimonline@7009
   133
philipp@9329
   134
#endif /* SDL_THREAD_PSP */
philipp@9329
   135
kimonline@7009
   136
/* vi: set ts=4 sw=4 expandtab: */