src/thread/generic/SDL_sysmutex.c
author Sam Lantinga
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 769 b8d311d90021
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* An implementation of mutexes using semaphores */
slouken@0
    24
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <stdlib.h>
slouken@0
    27
slouken@0
    28
#include "SDL_error.h"
slouken@0
    29
#include "SDL_thread.h"
slouken@0
    30
#include "SDL_systhread_c.h"
slouken@0
    31
slouken@0
    32
slouken@0
    33
struct SDL_mutex {
slouken@0
    34
	int recursive;
slouken@0
    35
	Uint32 owner;
slouken@0
    36
	SDL_sem *sem;
slouken@0
    37
};
slouken@0
    38
slouken@0
    39
/* Create a mutex */
slouken@0
    40
SDL_mutex *SDL_CreateMutex(void)
slouken@0
    41
{
slouken@0
    42
	SDL_mutex *mutex;
slouken@0
    43
slouken@0
    44
	/* Allocate mutex memory */
slouken@0
    45
	mutex = (SDL_mutex *)malloc(sizeof(*mutex));
slouken@0
    46
	if ( mutex ) {
slouken@0
    47
		/* Create the mutex semaphore, with initial value 1 */
slouken@0
    48
		mutex->sem = SDL_CreateSemaphore(1);
slouken@0
    49
		mutex->recursive = 0;
slouken@0
    50
		mutex->owner = 0;
slouken@0
    51
		if ( ! mutex->sem ) {
slouken@0
    52
			free(mutex);
slouken@0
    53
			mutex = NULL;
slouken@0
    54
		}
slouken@0
    55
	} else {
slouken@0
    56
		SDL_OutOfMemory();
slouken@0
    57
	}
slouken@0
    58
	return mutex;
slouken@0
    59
}
slouken@0
    60
slouken@0
    61
/* Free the mutex */
slouken@0
    62
void SDL_DestroyMutex(SDL_mutex *mutex)
slouken@0
    63
{
slouken@0
    64
	if ( mutex ) {
slouken@0
    65
		if ( mutex->sem ) {
slouken@0
    66
			SDL_DestroySemaphore(mutex->sem);
slouken@0
    67
		}
slouken@0
    68
		free(mutex);
slouken@0
    69
	}
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
/* Lock the semaphore */
slouken@0
    73
int SDL_mutexP(SDL_mutex *mutex)
slouken@0
    74
{
slouken@0
    75
#ifdef DISABLE_THREADS
slouken@0
    76
	return 0;
slouken@0
    77
#else
slouken@0
    78
	Uint32 this_thread;
slouken@0
    79
slouken@0
    80
	if ( mutex == NULL ) {
slouken@0
    81
		SDL_SetError("Passed a NULL mutex");
slouken@0
    82
		return -1;
slouken@0
    83
	}
slouken@0
    84
slouken@0
    85
	this_thread = SDL_ThreadID();
slouken@0
    86
	if ( mutex->owner == this_thread ) {
slouken@0
    87
		++mutex->recursive;
slouken@0
    88
	} else {
slouken@0
    89
		/* The order of operations is important.
slouken@0
    90
		   We set the locking thread id after we obtain the lock
slouken@0
    91
		   so unlocks from other threads will fail.
slouken@0
    92
		*/
slouken@0
    93
		SDL_SemWait(mutex->sem);
slouken@0
    94
		mutex->owner = this_thread;
slouken@0
    95
		mutex->recursive = 0;
slouken@0
    96
	}
slouken@0
    97
slouken@0
    98
	return 0;
slouken@0
    99
#endif /* DISABLE_THREADS */
slouken@0
   100
}
slouken@0
   101
slouken@0
   102
/* Unlock the mutex */
slouken@0
   103
int SDL_mutexV(SDL_mutex *mutex)
slouken@0
   104
{
slouken@0
   105
#ifdef DISABLE_THREADS
slouken@0
   106
	return 0;
slouken@0
   107
#else
slouken@0
   108
	if ( mutex == NULL ) {
slouken@0
   109
		SDL_SetError("Passed a NULL mutex");
slouken@0
   110
		return -1;
slouken@0
   111
	}
slouken@0
   112
slouken@0
   113
	/* If we don't own the mutex, we can't unlock it */
slouken@0
   114
	if ( SDL_ThreadID() != mutex->owner ) {
slouken@0
   115
		SDL_SetError("mutex not owned by this thread");
slouken@0
   116
		return -1;
slouken@0
   117
	}
slouken@0
   118
slouken@0
   119
	if ( mutex->recursive ) {
slouken@0
   120
		--mutex->recursive;
slouken@0
   121
	} else {
slouken@0
   122
		/* The order of operations is important.
slouken@0
   123
		   First reset the owner so another thread doesn't lock
slouken@0
   124
		   the mutex and set the ownership before we reset it,
slouken@0
   125
		   then release the lock semaphore.
slouken@0
   126
		 */
slouken@0
   127
		mutex->owner = 0;
slouken@0
   128
		SDL_SemPost(mutex->sem);
slouken@0
   129
	}
slouken@0
   130
	return 0;
slouken@0
   131
#endif /* DISABLE_THREADS */
slouken@0
   132
}