src/thread/pthread/SDL_sysmutex.c
author Sam Lantinga
Wed, 03 Oct 2012 19:26:07 -0700
changeset 6552 caa7decef32b
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rw-r--r--
Merged Ryan's changes
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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>
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@1895
    81
SDL_mutexP(SDL_mutex * mutex)
slouken@1361
    82
{
slouken@1895
    83
    int retval;
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) {
slouken@1895
    89
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    90
        return -1;
slouken@1895
    91
    }
slouken@1361
    92
slouken@1895
    93
    retval = 0;
slouken@1361
    94
#if FAKE_RECURSIVE_MUTEX
slouken@1895
    95
    this_thread = pthread_self();
slouken@1895
    96
    if (mutex->owner == this_thread) {
slouken@1895
    97
        ++mutex->recursive;
slouken@1895
    98
    } else {
slouken@1895
    99
        /* The order of operations is important.
slouken@1895
   100
           We set the locking thread id after we obtain the lock
slouken@1895
   101
           so unlocks from other threads will fail.
slouken@1895
   102
         */
slouken@1895
   103
        if (pthread_mutex_lock(&mutex->id) == 0) {
slouken@1895
   104
            mutex->owner = this_thread;
slouken@1895
   105
            mutex->recursive = 0;
slouken@1895
   106
        } else {
slouken@1895
   107
            SDL_SetError("pthread_mutex_lock() failed");
slouken@1895
   108
            retval = -1;
slouken@1895
   109
        }
slouken@1895
   110
    }
slouken@1361
   111
#else
slouken@1895
   112
    if (pthread_mutex_lock(&mutex->id) < 0) {
slouken@1895
   113
        SDL_SetError("pthread_mutex_lock() failed");
slouken@1895
   114
        retval = -1;
slouken@1895
   115
    }
slouken@1361
   116
#endif
slouken@1895
   117
    return retval;
slouken@1361
   118
}
slouken@1361
   119
slouken@1895
   120
int
slouken@1895
   121
SDL_mutexV(SDL_mutex * mutex)
slouken@1361
   122
{
slouken@1895
   123
    int retval;
slouken@1361
   124
slouken@1895
   125
    if (mutex == NULL) {
slouken@1895
   126
        SDL_SetError("Passed a NULL mutex");
slouken@1895
   127
        return -1;
slouken@1895
   128
    }
slouken@1361
   129
slouken@1895
   130
    retval = 0;
slouken@1361
   131
#if FAKE_RECURSIVE_MUTEX
slouken@1895
   132
    /* We can only unlock the mutex if we own it */
slouken@1895
   133
    if (pthread_self() == mutex->owner) {
slouken@1895
   134
        if (mutex->recursive) {
slouken@1895
   135
            --mutex->recursive;
slouken@1895
   136
        } else {
slouken@1895
   137
            /* The order of operations is important.
slouken@1895
   138
               First reset the owner so another thread doesn't lock
slouken@1895
   139
               the mutex and set the ownership before we reset it,
slouken@1895
   140
               then release the lock semaphore.
slouken@1895
   141
             */
slouken@1895
   142
            mutex->owner = 0;
slouken@1895
   143
            pthread_mutex_unlock(&mutex->id);
slouken@1895
   144
        }
slouken@1895
   145
    } else {
slouken@1895
   146
        SDL_SetError("mutex not owned by this thread");
slouken@1895
   147
        retval = -1;
slouken@1895
   148
    }
slouken@1361
   149
slouken@1361
   150
#else
slouken@1895
   151
    if (pthread_mutex_unlock(&mutex->id) < 0) {
slouken@1895
   152
        SDL_SetError("pthread_mutex_unlock() failed");
slouken@1895
   153
        retval = -1;
slouken@1895
   154
    }
slouken@1361
   155
#endif /* FAKE_RECURSIVE_MUTEX */
slouken@1361
   156
slouken@1895
   157
    return retval;
slouken@1361
   158
}
slouken@1895
   159
slouken@1895
   160
/* vi: set ts=4 sw=4 expandtab: */