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