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 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

◆ FAKE_RECURSIVE_MUTEX

#define FAKE_RECURSIVE_MUTEX   1

Definition at line 30 of file SDL_sysmutex.c.

Function Documentation

◆ SDL_CreateMutex()

SDL_mutex* SDL_CreateMutex ( void  )

Create a mutex, initialized unlocked.

Definition at line 43 of file SDL_sysmutex.c.

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

44 {
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 {
66  }
67  return (mutex);
68 }
static SDL_mutex * mutex
Definition: testlock.c:23
#define SDL_free
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_SetError
#define SDL_calloc
pthread_mutex_t id
Definition: SDL_sysmutex.c:35

◆ SDL_DestroyMutex()

void SDL_DestroyMutex ( SDL_mutex mutex)

Destroy a mutex.

Definition at line 71 of file SDL_sysmutex.c.

References SDL_mutex::id, and SDL_free.

72 {
73  if (mutex) {
74  pthread_mutex_destroy(&mutex->id);
75  SDL_free(mutex);
76  }
77 }
#define SDL_free
pthread_mutex_t id
Definition: SDL_sysmutex.c:35

◆ SDL_LockMutex()

int SDL_LockMutex ( SDL_mutex mutex)

Definition at line 81 of file SDL_sysmutex.c.

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

82 {
83 #if FAKE_RECURSIVE_MUTEX
84  pthread_t this_thread;
85 #endif
86 
87  if (mutex == NULL) {
88  return SDL_SetError("Passed a NULL mutex");
89  }
90 
91 #if FAKE_RECURSIVE_MUTEX
92  this_thread = pthread_self();
93  if (mutex->owner == this_thread) {
94  ++mutex->recursive;
95  } else {
96  /* The order of operations is important.
97  We set the locking thread id after we obtain the lock
98  so unlocks from other threads will fail.
99  */
100  if (pthread_mutex_lock(&mutex->id) == 0) {
101  mutex->owner = this_thread;
102  mutex->recursive = 0;
103  } else {
104  return SDL_SetError("pthread_mutex_lock() failed");
105  }
106  }
107 #else
108  if (pthread_mutex_lock(&mutex->id) != 0) {
109  return SDL_SetError("pthread_mutex_lock() failed");
110  }
111 #endif
112  return 0;
113 }
#define NULL
Definition: begin_code.h:164
#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:35

◆ 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 116 of file SDL_sysmutex.c.

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

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

◆ SDL_UnlockMutex()

int SDL_UnlockMutex ( SDL_mutex mutex)

Definition at line 159 of file SDL_sysmutex.c.

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

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