src/thread/psp/SDL_sysmutex.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7037 3fedf1f25b94
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h The new header will include SDL_config.h, but allows for other global stuff.
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
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7037
diff changeset
    21
#include "../../SDL_internal.h"
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    22
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    23
/* An implementation of mutexes using semaphores */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    24
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    25
#include "SDL_thread.h"
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    26
#include "SDL_systhread_c.h"
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    27
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    28
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    29
struct SDL_mutex
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    30
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    31
    int recursive;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    32
    SDL_threadID owner;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    33
    SDL_sem *sem;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    34
};
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
/* Create a mutex */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    37
SDL_mutex *
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    38
SDL_CreateMutex(void)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    39
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    40
    SDL_mutex *mutex;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    41
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    42
    /* Allocate mutex memory */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    43
    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    44
    if (mutex) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    45
        /* Create the mutex semaphore, with initial value 1 */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    46
        mutex->sem = SDL_CreateSemaphore(1);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    47
        mutex->recursive = 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    48
        mutex->owner = 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    49
        if (!mutex->sem) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    50
            SDL_free(mutex);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    51
            mutex = NULL;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    52
        }
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    53
    } else {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    54
        SDL_OutOfMemory();
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 mutex;
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 mutex */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    60
void
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    61
SDL_DestroyMutex(SDL_mutex * mutex)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    62
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    63
    if (mutex) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    64
        if (mutex->sem) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    65
            SDL_DestroySemaphore(mutex->sem);
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
        SDL_free(mutex);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    68
    }
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
/* Lock the semaphore */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    72
int
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    73
SDL_mutexP(SDL_mutex * mutex)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    74
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    75
#if SDL_THREADS_DISABLED
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    76
    return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    77
#else
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    78
    SDL_threadID this_thread;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    79
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    80
    if (mutex == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7009
diff changeset
    81
        return SDL_SetError("Passed a NULL mutex");
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    82
    }
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    83
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    84
    this_thread = SDL_ThreadID();
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    85
    if (mutex->owner == this_thread) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    86
        ++mutex->recursive;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    87
    } else {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    88
        /* The order of operations is important.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    89
           We set the locking thread id after we obtain the lock
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    90
           so unlocks from other threads will fail.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    91
         */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    92
        SDL_SemWait(mutex->sem);
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    93
        mutex->owner = this_thread;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    94
        mutex->recursive = 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    95
    }
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    96
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    97
    return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
    98
#endif /* SDL_THREADS_DISABLED */
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
/* Unlock the mutex */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   102
int
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   103
SDL_mutexV(SDL_mutex * mutex)
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   104
{
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   105
#if SDL_THREADS_DISABLED
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   106
    return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   107
#else
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   108
    if (mutex == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7009
diff changeset
   109
        return SDL_SetError("Passed a NULL mutex");
7009
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
    /* If we don't own the mutex, we can't unlock it */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   113
    if (SDL_ThreadID() != mutex->owner) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7009
diff changeset
   114
        return SDL_SetError("mutex not owned by this thread");
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   115
    }
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
    if (mutex->recursive) {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   118
        --mutex->recursive;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   119
    } else {
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   120
        /* The order of operations is important.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   121
           First reset the owner so another thread doesn't lock
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   122
           the mutex and set the ownership before we reset it,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   123
           then release the lock semaphore.
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   124
         */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   125
        mutex->owner = 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   126
        SDL_SemPost(mutex->sem);
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
    return 0;
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   129
#endif /* SDL_THREADS_DISABLED */
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents:
diff changeset
   130
}
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
/* vi: set ts=4 sw=4 expandtab: */