src/thread/pthread/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Jun 2013 01:08:14 -0700
changeset 7257 5997d00782e8
parent 7037 3fedf1f25b94
child 8093 b43765095a6f
permissions -rw-r--r--
Fixed testnative on Mac OS X, which no longer ships X11 by default.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     7
  arising from the use of this software.
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    11
  freely, subject to the following restrictions:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    21
#include "SDL_config.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
3605
55b0a0191163 We're using GNU recursive mutexes
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    23
#define _GNU_SOURCE
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#include <pthread.h>
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    25
#include <errno.h>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#include "SDL_thread.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
    !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
5481
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    31
#define FAKE_RECURSIVE_MUTEX 1
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
#endif
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    34
struct SDL_mutex
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    35
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    36
    pthread_mutex_t id;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#if FAKE_RECURSIVE_MUTEX
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    38
    int recursive;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    39
    pthread_t owner;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
#endif
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
};
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    43
SDL_mutex *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    44
SDL_CreateMutex(void)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    46
    SDL_mutex *mutex;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    47
    pthread_mutexattr_t attr;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    49
    /* Allocate the structure */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    50
    mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    51
    if (mutex) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    52
        pthread_mutexattr_init(&attr);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    54
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
#elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    56
        pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    58
        /* No extra attributes necessary */
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    60
        if (pthread_mutex_init(&mutex->id, &attr) != 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    61
            SDL_SetError("pthread_mutex_init() failed");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    62
            SDL_free(mutex);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    63
            mutex = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    64
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    65
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    66
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    67
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    68
    return (mutex);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    71
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    72
SDL_DestroyMutex(SDL_mutex * mutex)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    74
    if (mutex) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    75
        pthread_mutex_destroy(&mutex->id);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    76
        SDL_free(mutex);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    77
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
/* Lock the mutex */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    81
int
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 6966
diff changeset
    82
SDL_LockMutex(SDL_mutex * mutex)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
{
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
#if FAKE_RECURSIVE_MUTEX
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    85
    pthread_t this_thread;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
#endif
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    88
    if (mutex == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
    89
        return SDL_SetError("Passed a NULL mutex");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    90
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
#if FAKE_RECURSIVE_MUTEX
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    93
    this_thread = pthread_self();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    94
    if (mutex->owner == this_thread) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    95
        ++mutex->recursive;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    96
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    97
        /* The order of operations is important.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    98
           We set the locking thread id after we obtain the lock
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    99
           so unlocks from other threads will fail.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   100
         */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   101
        if (pthread_mutex_lock(&mutex->id) == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   102
            mutex->owner = this_thread;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   103
            mutex->recursive = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   104
        } else {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   105
            return SDL_SetError("pthread_mutex_lock() failed");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   106
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   107
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   109
    if (pthread_mutex_lock(&mutex->id) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   110
        return SDL_SetError("pthread_mutex_lock() failed");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   111
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
#endif
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   113
    return 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   116
int
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   117
SDL_TryLockMutex(SDL_mutex * mutex)
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   118
{
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   119
    int retval;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   120
#if FAKE_RECURSIVE_MUTEX
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   121
    pthread_t this_thread;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   122
#endif
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   123
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   124
    if (mutex == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   125
        return SDL_SetError("Passed a NULL mutex");
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   126
    }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   127
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   128
    retval = 0;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   129
#if FAKE_RECURSIVE_MUTEX
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   130
    this_thread = pthread_self();
6985
a0f905ecdda3 Fix SDL_TryLockMutex compile error when FAKE_RECURSIVE_MUTEX is defined
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6977
diff changeset
   131
    if (mutex->owner == this_thread) {
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   132
        ++mutex->recursive;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   133
    } else {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   134
        /* The order of operations is important.
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   135
         We set the locking thread id after we obtain the lock
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   136
         so unlocks from other threads will fail.
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   137
         */
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   138
        if (pthread_mutex_lock(&mutex->id) == 0) {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   139
            mutex->owner = this_thread;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   140
            mutex->recursive = 0;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   141
        } else if (errno == EBUSY) {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   142
            retval = SDL_MUTEX_TIMEDOUT;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   143
        } else {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   144
            retval = SDL_SetError("pthread_mutex_trylock() failed");
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   145
        }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   146
    }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   147
#else
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   148
    if (pthread_mutex_trylock(&mutex->id) != 0) {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   149
        if (errno == EBUSY) {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   150
            retval = SDL_MUTEX_TIMEDOUT;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   151
        } else {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   152
            retval = SDL_SetError("pthread_mutex_trylock() failed");
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   153
        }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   154
    }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   155
#endif
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   156
    return retval;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   157
}
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   158
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
   159
int
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 6966
diff changeset
   160
SDL_UnlockMutex(SDL_mutex * mutex)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   162
    if (mutex == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   163
        return SDL_SetError("Passed a NULL mutex");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   164
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
#if FAKE_RECURSIVE_MUTEX
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   167
    /* We can only unlock the mutex if we own it */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   168
    if (pthread_self() == mutex->owner) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   169
        if (mutex->recursive) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   170
            --mutex->recursive;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   171
        } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   172
            /* The order of operations is important.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   173
               First reset the owner so another thread doesn't lock
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   174
               the mutex and set the ownership before we reset it,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   175
               then release the lock semaphore.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   176
             */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   177
            mutex->owner = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   178
            pthread_mutex_unlock(&mutex->id);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   179
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   180
    } else {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   181
        return SDL_SetError("mutex not owned by this thread");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   182
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   185
    if (pthread_mutex_unlock(&mutex->id) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   186
        return SDL_SetError("pthread_mutex_unlock() failed");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   187
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
#endif /* FAKE_RECURSIVE_MUTEX */
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6985
diff changeset
   190
    return 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   192
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   193
/* vi: set ts=4 sw=4 expandtab: */