src/thread/pthread/SDL_sysmutex.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 11233 554b2c683da2
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1361
    22
slouken@9734
    23
#include <errno.h>
slouken@1361
    24
#include <pthread.h>
slouken@1361
    25
slouken@1361
    26
#include "SDL_thread.h"
slouken@1361
    27
slouken@1361
    28
#if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
slouken@1361
    29
    !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
slouken@5481
    30
#define FAKE_RECURSIVE_MUTEX 1
slouken@1361
    31
#endif
slouken@1361
    32
slouken@1895
    33
struct SDL_mutex
slouken@1895
    34
{
slouken@1895
    35
    pthread_mutex_t id;
slouken@1361
    36
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    37
    int recursive;
slouken@1895
    38
    pthread_t owner;
slouken@1361
    39
#endif
slouken@1361
    40
};
slouken@1361
    41
slouken@1895
    42
SDL_mutex *
slouken@1895
    43
SDL_CreateMutex(void)
slouken@1361
    44
{
slouken@1895
    45
    SDL_mutex *mutex;
slouken@1895
    46
    pthread_mutexattr_t attr;
slouken@1361
    47
slouken@1895
    48
    /* Allocate the structure */
slouken@1895
    49
    mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
slouken@1895
    50
    if (mutex) {
slouken@1895
    51
        pthread_mutexattr_init(&attr);
slouken@1361
    52
#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
slouken@1895
    53
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
slouken@1361
    54
#elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
slouken@1895
    55
        pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
slouken@1361
    56
#else
slouken@1895
    57
        /* No extra attributes necessary */
slouken@1361
    58
#endif
slouken@1895
    59
        if (pthread_mutex_init(&mutex->id, &attr) != 0) {
slouken@1895
    60
            SDL_SetError("pthread_mutex_init() failed");
slouken@1895
    61
            SDL_free(mutex);
slouken@1895
    62
            mutex = NULL;
slouken@1895
    63
        }
slouken@1895
    64
    } else {
slouken@1895
    65
        SDL_OutOfMemory();
slouken@1895
    66
    }
slouken@1895
    67
    return (mutex);
slouken@1361
    68
}
slouken@1361
    69
slouken@1895
    70
void
slouken@1895
    71
SDL_DestroyMutex(SDL_mutex * mutex)
slouken@1361
    72
{
slouken@1895
    73
    if (mutex) {
slouken@1895
    74
        pthread_mutex_destroy(&mutex->id);
slouken@1895
    75
        SDL_free(mutex);
slouken@1895
    76
    }
slouken@1361
    77
}
slouken@1361
    78
slouken@1361
    79
/* Lock the mutex */
slouken@1895
    80
int
slouken@6977
    81
SDL_LockMutex(SDL_mutex * mutex)
slouken@1361
    82
{
slouken@1361
    83
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    84
    pthread_t this_thread;
slouken@1361
    85
#endif
slouken@1361
    86
slouken@1895
    87
    if (mutex == NULL) {
icculus@7037
    88
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
    89
    }
slouken@1361
    90
slouken@1361
    91
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    92
    this_thread = pthread_self();
slouken@1895
    93
    if (mutex->owner == this_thread) {
slouken@1895
    94
        ++mutex->recursive;
slouken@1895
    95
    } else {
slouken@1895
    96
        /* The order of operations is important.
slouken@1895
    97
           We set the locking thread id after we obtain the lock
slouken@1895
    98
           so unlocks from other threads will fail.
slouken@1895
    99
         */
slouken@1895
   100
        if (pthread_mutex_lock(&mutex->id) == 0) {
slouken@1895
   101
            mutex->owner = this_thread;
slouken@1895
   102
            mutex->recursive = 0;
slouken@1895
   103
        } else {
icculus@7037
   104
            return SDL_SetError("pthread_mutex_lock() failed");
slouken@1895
   105
        }
slouken@1895
   106
    }
slouken@1361
   107
#else
slouken@1895
   108
    if (pthread_mutex_lock(&mutex->id) < 0) {
icculus@7037
   109
        return SDL_SetError("pthread_mutex_lock() failed");
slouken@1895
   110
    }
slouken@1361
   111
#endif
icculus@7037
   112
    return 0;
slouken@1361
   113
}
slouken@1361
   114
slouken@1895
   115
int
urkle@6966
   116
SDL_TryLockMutex(SDL_mutex * mutex)
urkle@6966
   117
{
urkle@6966
   118
    int retval;
urkle@6966
   119
#if FAKE_RECURSIVE_MUTEX
urkle@6966
   120
    pthread_t this_thread;
urkle@6966
   121
#endif
urkle@6966
   122
urkle@6966
   123
    if (mutex == NULL) {
icculus@7037
   124
        return SDL_SetError("Passed a NULL mutex");
urkle@6966
   125
    }
urkle@6966
   126
urkle@6966
   127
    retval = 0;
urkle@6966
   128
#if FAKE_RECURSIVE_MUTEX
urkle@6966
   129
    this_thread = pthread_self();
aschiffler@6985
   130
    if (mutex->owner == this_thread) {
urkle@6966
   131
        ++mutex->recursive;
urkle@6966
   132
    } else {
urkle@6966
   133
        /* The order of operations is important.
urkle@6966
   134
         We set the locking thread id after we obtain the lock
urkle@6966
   135
         so unlocks from other threads will fail.
urkle@6966
   136
         */
urkle@6966
   137
        if (pthread_mutex_lock(&mutex->id) == 0) {
urkle@6966
   138
            mutex->owner = this_thread;
urkle@6966
   139
            mutex->recursive = 0;
urkle@6966
   140
        } else if (errno == EBUSY) {
urkle@6966
   141
            retval = SDL_MUTEX_TIMEDOUT;
urkle@6966
   142
        } else {
icculus@7037
   143
            retval = SDL_SetError("pthread_mutex_trylock() failed");
urkle@6966
   144
        }
urkle@6966
   145
    }
urkle@6966
   146
#else
urkle@6966
   147
    if (pthread_mutex_trylock(&mutex->id) != 0) {
urkle@6966
   148
        if (errno == EBUSY) {
urkle@6966
   149
            retval = SDL_MUTEX_TIMEDOUT;
urkle@6966
   150
        } else {
icculus@7037
   151
            retval = SDL_SetError("pthread_mutex_trylock() failed");
urkle@6966
   152
        }
urkle@6966
   153
    }
urkle@6966
   154
#endif
urkle@6966
   155
    return retval;
urkle@6966
   156
}
urkle@6966
   157
urkle@6966
   158
int
slouken@6977
   159
SDL_UnlockMutex(SDL_mutex * mutex)
slouken@1361
   160
{
slouken@1895
   161
    if (mutex == NULL) {
icculus@7037
   162
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
   163
    }
slouken@1361
   164
slouken@1361
   165
#if FAKE_RECURSIVE_MUTEX
slouken@1895
   166
    /* We can only unlock the mutex if we own it */
slouken@1895
   167
    if (pthread_self() == mutex->owner) {
slouken@1895
   168
        if (mutex->recursive) {
slouken@1895
   169
            --mutex->recursive;
slouken@1895
   170
        } else {
slouken@1895
   171
            /* The order of operations is important.
slouken@1895
   172
               First reset the owner so another thread doesn't lock
slouken@1895
   173
               the mutex and set the ownership before we reset it,
slouken@1895
   174
               then release the lock semaphore.
slouken@1895
   175
             */
slouken@1895
   176
            mutex->owner = 0;
slouken@1895
   177
            pthread_mutex_unlock(&mutex->id);
slouken@1895
   178
        }
slouken@1895
   179
    } else {
icculus@7037
   180
        return SDL_SetError("mutex not owned by this thread");
slouken@1895
   181
    }
slouken@1361
   182
slouken@1361
   183
#else
slouken@1895
   184
    if (pthread_mutex_unlock(&mutex->id) < 0) {
icculus@7037
   185
        return SDL_SetError("pthread_mutex_unlock() failed");
slouken@1895
   186
    }
slouken@1361
   187
#endif /* FAKE_RECURSIVE_MUTEX */
slouken@1361
   188
icculus@7037
   189
    return 0;
slouken@1361
   190
}
slouken@1895
   191
slouken@1895
   192
/* vi: set ts=4 sw=4 expandtab: */