SDL  2.0
SDL_sysmutex.c File Reference
#include "../../SDL_internal.h"
#include <errno.h>
#include <pthread.h>
#include "SDL_thread.h"
+ Include dependency graph for SDL_sysmutex.c:

Go to the source code of this file.

Data Structures

struct  SDL_mutex
 

Macros

#define _GNU_SOURCE
 
#define FAKE_RECURSIVE_MUTEX   1
 

Functions

SDL_mutexSDL_CreateMutex (void)
 
void SDL_DestroyMutex (SDL_mutex *mutex)
 
int SDL_LockMutex (SDL_mutex *mutex)
 
int SDL_TryLockMutex (SDL_mutex *mutex)
 
int SDL_UnlockMutex (SDL_mutex *mutex)
 

Macro Definition Documentation

§ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 24 of file SDL_sysmutex.c.

§ FAKE_RECURSIVE_MUTEX

#define FAKE_RECURSIVE_MUTEX   1

Definition at line 33 of file SDL_sysmutex.c.

Function Documentation

§ SDL_CreateMutex()

SDL_mutex* SDL_CreateMutex ( void  )

Create a mutex, initialized unlocked.

Definition at line 46 of file SDL_sysmutex.c.

References SDL_mutex::id, mutex, NULL, SDL_calloc(), SDL_free(), SDL_OutOfMemory, and SDL_SetError.

47 {
49  pthread_mutexattr_t attr;
50 
51  /* Allocate the structure */
52  mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
53  if (mutex) {
54  pthread_mutexattr_init(&attr);
55 #if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
56  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
57 #elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
58  pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
59 #else
60  /* No extra attributes necessary */
61 #endif
62  if (pthread_mutex_init(&mutex->id, &attr) != 0) {
63  SDL_SetError("pthread_mutex_init() failed");
64  SDL_free(mutex);
65  mutex = NULL;
66  }
67  } else {
69  }
70  return (mutex);
71 }
static SDL_mutex * mutex
Definition: testlock.c:23
void * SDL_calloc(size_t nmemb, size_t size)
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_SetError
pthread_mutex_t id
Definition: SDL_sysmutex.c:38

§ SDL_DestroyMutex()

void SDL_DestroyMutex ( SDL_mutex mutex)

Destroy a mutex.

Definition at line 74 of file SDL_sysmutex.c.

References SDL_mutex::id, and SDL_free().

75 {
76  if (mutex) {
77  pthread_mutex_destroy(&mutex->id);
78  SDL_free(mutex);
79  }
80 }
void SDL_free(void *mem)
pthread_mutex_t id
Definition: SDL_sysmutex.c:38

§ SDL_LockMutex()

int SDL_LockMutex ( SDL_mutex mutex)

Definition at line 84 of file SDL_sysmutex.c.

References SDL_mutex::id, NULL, SDL_mutex::owner, SDL_mutex::recursive, and SDL_SetError.

85 {
86 #if FAKE_RECURSIVE_MUTEX
87  pthread_t this_thread;
88 #endif
89 
90  if (mutex == NULL) {
91  return SDL_SetError("Passed a NULL mutex");
92  }
93 
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  return SDL_SetError("pthread_mutex_lock() failed");
108  }
109  }
110 #else
111  if (pthread_mutex_lock(&mutex->id) < 0) {
112  return SDL_SetError("pthread_mutex_lock() failed");
113  }
114 #endif
115  return 0;
116 }
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
SDL_threadID owner
Definition: SDL_sysmutex.c:32
int recursive
Definition: SDL_sysmutex.c:31
pthread_mutex_t id
Definition: SDL_sysmutex.c:38

§ SDL_TryLockMutex()

int SDL_TryLockMutex ( SDL_mutex mutex)

Try to lock the mutex

Returns
0, SDL_MUTEX_TIMEDOUT, or -1 on error

Definition at line 119 of file SDL_sysmutex.c.

References SDL_mutex::id, NULL, SDL_mutex::owner, SDL_mutex::recursive, retval, SDL_MUTEX_TIMEDOUT, and SDL_SetError.

120 {
121  int retval;
122 #if FAKE_RECURSIVE_MUTEX
123  pthread_t this_thread;
124 #endif
125 
126  if (mutex == NULL) {
127  return SDL_SetError("Passed a NULL mutex");
128  }
129 
130  retval = 0;
131 #if FAKE_RECURSIVE_MUTEX
132  this_thread = pthread_self();
133  if (mutex->owner == this_thread) {
134  ++mutex->recursive;
135  } else {
136  /* The order of operations is important.
137  We set the locking thread id after we obtain the lock
138  so unlocks from other threads will fail.
139  */
140  if (pthread_mutex_lock(&mutex->id) == 0) {
141  mutex->owner = this_thread;
142  mutex->recursive = 0;
143  } else if (errno == EBUSY) {
144  retval = SDL_MUTEX_TIMEDOUT;
145  } else {
146  retval = SDL_SetError("pthread_mutex_trylock() failed");
147  }
148  }
149 #else
150  if (pthread_mutex_trylock(&mutex->id) != 0) {
151  if (errno == EBUSY) {
152  retval = SDL_MUTEX_TIMEDOUT;
153  } else {
154  retval = SDL_SetError("pthread_mutex_trylock() failed");
155  }
156  }
157 #endif
158  return retval;
159 }
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
SDL_bool retval
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
SDL_threadID owner
Definition: SDL_sysmutex.c:32
int recursive
Definition: SDL_sysmutex.c:31
pthread_mutex_t id
Definition: SDL_sysmutex.c:38

§ SDL_UnlockMutex()

int SDL_UnlockMutex ( SDL_mutex mutex)

Definition at line 162 of file SDL_sysmutex.c.

References SDL_mutex::id, NULL, SDL_mutex::owner, SDL_mutex::recursive, and SDL_SetError.

163 {
164  if (mutex == NULL) {
165  return SDL_SetError("Passed a NULL mutex");
166  }
167 
168 #if FAKE_RECURSIVE_MUTEX
169  /* We can only unlock the mutex if we own it */
170  if (pthread_self() == mutex->owner) {
171  if (mutex->recursive) {
172  --mutex->recursive;
173  } else {
174  /* The order of operations is important.
175  First reset the owner so another thread doesn't lock
176  the mutex and set the ownership before we reset it,
177  then release the lock semaphore.
178  */
179  mutex->owner = 0;
180  pthread_mutex_unlock(&mutex->id);
181  }
182  } else {
183  return SDL_SetError("mutex not owned by this thread");
184  }
185 
186 #else
187  if (pthread_mutex_unlock(&mutex->id) < 0) {
188  return SDL_SetError("pthread_mutex_unlock() failed");
189  }
190 #endif /* FAKE_RECURSIVE_MUTEX */
191 
192  return 0;
193 }
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
SDL_threadID owner
Definition: SDL_sysmutex.c:32
int recursive
Definition: SDL_sysmutex.c:31
pthread_mutex_t id
Definition: SDL_sysmutex.c:38