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