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