src/thread/generic/SDL_sysmutex.c
author Sam Lantinga
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* An implementation of mutexes using semaphores */
slouken@0
    29
slouken@0
    30
#include <stdio.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
slouken@0
    33
#include "SDL_error.h"
slouken@0
    34
#include "SDL_thread.h"
slouken@0
    35
#include "SDL_systhread_c.h"
slouken@0
    36
slouken@0
    37
slouken@0
    38
struct SDL_mutex {
slouken@0
    39
	int recursive;
slouken@0
    40
	Uint32 owner;
slouken@0
    41
	SDL_sem *sem;
slouken@0
    42
};
slouken@0
    43
slouken@0
    44
/* Create a mutex */
slouken@0
    45
SDL_mutex *SDL_CreateMutex(void)
slouken@0
    46
{
slouken@0
    47
	SDL_mutex *mutex;
slouken@0
    48
slouken@0
    49
	/* Allocate mutex memory */
slouken@0
    50
	mutex = (SDL_mutex *)malloc(sizeof(*mutex));
slouken@0
    51
	if ( mutex ) {
slouken@0
    52
		/* Create the mutex semaphore, with initial value 1 */
slouken@0
    53
		mutex->sem = SDL_CreateSemaphore(1);
slouken@0
    54
		mutex->recursive = 0;
slouken@0
    55
		mutex->owner = 0;
slouken@0
    56
		if ( ! mutex->sem ) {
slouken@0
    57
			free(mutex);
slouken@0
    58
			mutex = NULL;
slouken@0
    59
		}
slouken@0
    60
	} else {
slouken@0
    61
		SDL_OutOfMemory();
slouken@0
    62
	}
slouken@0
    63
	return mutex;
slouken@0
    64
}
slouken@0
    65
slouken@0
    66
/* Free the mutex */
slouken@0
    67
void SDL_DestroyMutex(SDL_mutex *mutex)
slouken@0
    68
{
slouken@0
    69
	if ( mutex ) {
slouken@0
    70
		if ( mutex->sem ) {
slouken@0
    71
			SDL_DestroySemaphore(mutex->sem);
slouken@0
    72
		}
slouken@0
    73
		free(mutex);
slouken@0
    74
	}
slouken@0
    75
}
slouken@0
    76
slouken@0
    77
/* Lock the semaphore */
slouken@0
    78
int SDL_mutexP(SDL_mutex *mutex)
slouken@0
    79
{
slouken@0
    80
#ifdef DISABLE_THREADS
slouken@0
    81
	return 0;
slouken@0
    82
#else
slouken@0
    83
	Uint32 this_thread;
slouken@0
    84
slouken@0
    85
	if ( mutex == NULL ) {
slouken@0
    86
		SDL_SetError("Passed a NULL mutex");
slouken@0
    87
		return -1;
slouken@0
    88
	}
slouken@0
    89
slouken@0
    90
	this_thread = SDL_ThreadID();
slouken@0
    91
	if ( mutex->owner == this_thread ) {
slouken@0
    92
		++mutex->recursive;
slouken@0
    93
	} else {
slouken@0
    94
		/* The order of operations is important.
slouken@0
    95
		   We set the locking thread id after we obtain the lock
slouken@0
    96
		   so unlocks from other threads will fail.
slouken@0
    97
		*/
slouken@0
    98
		SDL_SemWait(mutex->sem);
slouken@0
    99
		mutex->owner = this_thread;
slouken@0
   100
		mutex->recursive = 0;
slouken@0
   101
	}
slouken@0
   102
slouken@0
   103
	return 0;
slouken@0
   104
#endif /* DISABLE_THREADS */
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
/* Unlock the mutex */
slouken@0
   108
int SDL_mutexV(SDL_mutex *mutex)
slouken@0
   109
{
slouken@0
   110
#ifdef DISABLE_THREADS
slouken@0
   111
	return 0;
slouken@0
   112
#else
slouken@0
   113
	if ( mutex == NULL ) {
slouken@0
   114
		SDL_SetError("Passed a NULL mutex");
slouken@0
   115
		return -1;
slouken@0
   116
	}
slouken@0
   117
slouken@0
   118
	/* If we don't own the mutex, we can't unlock it */
slouken@0
   119
	if ( SDL_ThreadID() != mutex->owner ) {
slouken@0
   120
		SDL_SetError("mutex not owned by this thread");
slouken@0
   121
		return -1;
slouken@0
   122
	}
slouken@0
   123
slouken@0
   124
	if ( mutex->recursive ) {
slouken@0
   125
		--mutex->recursive;
slouken@0
   126
	} else {
slouken@0
   127
		/* The order of operations is important.
slouken@0
   128
		   First reset the owner so another thread doesn't lock
slouken@0
   129
		   the mutex and set the ownership before we reset it,
slouken@0
   130
		   then release the lock semaphore.
slouken@0
   131
		 */
slouken@0
   132
		mutex->owner = 0;
slouken@0
   133
		SDL_SemPost(mutex->sem);
slouken@0
   134
	}
slouken@0
   135
	return 0;
slouken@0
   136
#endif /* DISABLE_THREADS */
slouken@0
   137
}