src/thread/pthread/SDL_sysmutex.c
author Philipp Wiesemann
Thu, 30 May 2013 12:23:36 +0200
changeset 7242 96f4133dfafa
parent 7037 3fedf1f25b94
child 8093 b43765095a6f
permissions -rw-r--r--
Added deactivated loading of SDL2_net to Java file.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1361
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1361
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1361
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1361
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@1361
    22
slouken@3605
    23
#define _GNU_SOURCE
slouken@1361
    24
#include <pthread.h>
urkle@6966
    25
#include <errno.h>
slouken@1361
    26
slouken@1361
    27
#include "SDL_thread.h"
slouken@1361
    28
slouken@1361
    29
#if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
slouken@1361
    30
    !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
slouken@5481
    31
#define FAKE_RECURSIVE_MUTEX 1
slouken@1361
    32
#endif
slouken@1361
    33
slouken@1895
    34
struct SDL_mutex
slouken@1895
    35
{
slouken@1895
    36
    pthread_mutex_t id;
slouken@1361
    37
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    38
    int recursive;
slouken@1895
    39
    pthread_t owner;
slouken@1361
    40
#endif
slouken@1361
    41
};
slouken@1361
    42
slouken@1895
    43
SDL_mutex *
slouken@1895
    44
SDL_CreateMutex(void)
slouken@1361
    45
{
slouken@1895
    46
    SDL_mutex *mutex;
slouken@1895
    47
    pthread_mutexattr_t attr;
slouken@1361
    48
slouken@1895
    49
    /* Allocate the structure */
slouken@1895
    50
    mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
slouken@1895
    51
    if (mutex) {
slouken@1895
    52
        pthread_mutexattr_init(&attr);
slouken@1361
    53
#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
slouken@1895
    54
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
slouken@1361
    55
#elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
slouken@1895
    56
        pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
slouken@1361
    57
#else
slouken@1895
    58
        /* No extra attributes necessary */
slouken@1361
    59
#endif
slouken@1895
    60
        if (pthread_mutex_init(&mutex->id, &attr) != 0) {
slouken@1895
    61
            SDL_SetError("pthread_mutex_init() failed");
slouken@1895
    62
            SDL_free(mutex);
slouken@1895
    63
            mutex = NULL;
slouken@1895
    64
        }
slouken@1895
    65
    } else {
slouken@1895
    66
        SDL_OutOfMemory();
slouken@1895
    67
    }
slouken@1895
    68
    return (mutex);
slouken@1361
    69
}
slouken@1361
    70
slouken@1895
    71
void
slouken@1895
    72
SDL_DestroyMutex(SDL_mutex * mutex)
slouken@1361
    73
{
slouken@1895
    74
    if (mutex) {
slouken@1895
    75
        pthread_mutex_destroy(&mutex->id);
slouken@1895
    76
        SDL_free(mutex);
slouken@1895
    77
    }
slouken@1361
    78
}
slouken@1361
    79
slouken@1361
    80
/* Lock the mutex */
slouken@1895
    81
int
slouken@6977
    82
SDL_LockMutex(SDL_mutex * mutex)
slouken@1361
    83
{
slouken@1361
    84
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    85
    pthread_t this_thread;
slouken@1361
    86
#endif
slouken@1361
    87
slouken@1895
    88
    if (mutex == NULL) {
icculus@7037
    89
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
    90
    }
slouken@1361
    91
slouken@1361
    92
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    93
    this_thread = pthread_self();
slouken@1895
    94
    if (mutex->owner == this_thread) {
slouken@1895
    95
        ++mutex->recursive;
slouken@1895
    96
    } else {
slouken@1895
    97
        /* The order of operations is important.
slouken@1895
    98
           We set the locking thread id after we obtain the lock
slouken@1895
    99
           so unlocks from other threads will fail.
slouken@1895
   100
         */
slouken@1895
   101
        if (pthread_mutex_lock(&mutex->id) == 0) {
slouken@1895
   102
            mutex->owner = this_thread;
slouken@1895
   103
            mutex->recursive = 0;
slouken@1895
   104
        } else {
icculus@7037
   105
            return SDL_SetError("pthread_mutex_lock() failed");
slouken@1895
   106
        }
slouken@1895
   107
    }
slouken@1361
   108
#else
slouken@1895
   109
    if (pthread_mutex_lock(&mutex->id) < 0) {
icculus@7037
   110
        return SDL_SetError("pthread_mutex_lock() failed");
slouken@1895
   111
    }
slouken@1361
   112
#endif
icculus@7037
   113
    return 0;
slouken@1361
   114
}
slouken@1361
   115
slouken@1895
   116
int
urkle@6966
   117
SDL_TryLockMutex(SDL_mutex * mutex)
urkle@6966
   118
{
urkle@6966
   119
    int retval;
urkle@6966
   120
#if FAKE_RECURSIVE_MUTEX
urkle@6966
   121
    pthread_t this_thread;
urkle@6966
   122
#endif
urkle@6966
   123
urkle@6966
   124
    if (mutex == NULL) {
icculus@7037
   125
        return SDL_SetError("Passed a NULL mutex");
urkle@6966
   126
    }
urkle@6966
   127
urkle@6966
   128
    retval = 0;
urkle@6966
   129
#if FAKE_RECURSIVE_MUTEX
urkle@6966
   130
    this_thread = pthread_self();
aschiffler@6985
   131
    if (mutex->owner == this_thread) {
urkle@6966
   132
        ++mutex->recursive;
urkle@6966
   133
    } else {
urkle@6966
   134
        /* The order of operations is important.
urkle@6966
   135
         We set the locking thread id after we obtain the lock
urkle@6966
   136
         so unlocks from other threads will fail.
urkle@6966
   137
         */
urkle@6966
   138
        if (pthread_mutex_lock(&mutex->id) == 0) {
urkle@6966
   139
            mutex->owner = this_thread;
urkle@6966
   140
            mutex->recursive = 0;
urkle@6966
   141
        } else if (errno == EBUSY) {
urkle@6966
   142
            retval = SDL_MUTEX_TIMEDOUT;
urkle@6966
   143
        } else {
icculus@7037
   144
            retval = SDL_SetError("pthread_mutex_trylock() failed");
urkle@6966
   145
        }
urkle@6966
   146
    }
urkle@6966
   147
#else
urkle@6966
   148
    if (pthread_mutex_trylock(&mutex->id) != 0) {
urkle@6966
   149
        if (errno == EBUSY) {
urkle@6966
   150
            retval = SDL_MUTEX_TIMEDOUT;
urkle@6966
   151
        } else {
icculus@7037
   152
            retval = SDL_SetError("pthread_mutex_trylock() failed");
urkle@6966
   153
        }
urkle@6966
   154
    }
urkle@6966
   155
#endif
urkle@6966
   156
    return retval;
urkle@6966
   157
}
urkle@6966
   158
urkle@6966
   159
int
slouken@6977
   160
SDL_UnlockMutex(SDL_mutex * mutex)
slouken@1361
   161
{
slouken@1895
   162
    if (mutex == NULL) {
icculus@7037
   163
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
   164
    }
slouken@1361
   165
slouken@1361
   166
#if FAKE_RECURSIVE_MUTEX
slouken@1895
   167
    /* We can only unlock the mutex if we own it */
slouken@1895
   168
    if (pthread_self() == mutex->owner) {
slouken@1895
   169
        if (mutex->recursive) {
slouken@1895
   170
            --mutex->recursive;
slouken@1895
   171
        } else {
slouken@1895
   172
            /* The order of operations is important.
slouken@1895
   173
               First reset the owner so another thread doesn't lock
slouken@1895
   174
               the mutex and set the ownership before we reset it,
slouken@1895
   175
               then release the lock semaphore.
slouken@1895
   176
             */
slouken@1895
   177
            mutex->owner = 0;
slouken@1895
   178
            pthread_mutex_unlock(&mutex->id);
slouken@1895
   179
        }
slouken@1895
   180
    } else {
icculus@7037
   181
        return SDL_SetError("mutex not owned by this thread");
slouken@1895
   182
    }
slouken@1361
   183
slouken@1361
   184
#else
slouken@1895
   185
    if (pthread_mutex_unlock(&mutex->id) < 0) {
icculus@7037
   186
        return SDL_SetError("pthread_mutex_unlock() failed");
slouken@1895
   187
    }
slouken@1361
   188
#endif /* FAKE_RECURSIVE_MUTEX */
slouken@1361
   189
icculus@7037
   190
    return 0;
slouken@1361
   191
}
slouken@1895
   192
slouken@1895
   193
/* vi: set ts=4 sw=4 expandtab: */