view src/thread/linux/SDL_syssem.c @ 1133:609c060fd2a2

The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails: To: SDL Developers <sdl@libsdl.org> From: =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb@algonet.se> Date: Mon, 30 May 2005 23:29:04 +0200 Subject: [SDL] Mac OS X Video Drivers [patch] I've updated/added the Carbon and X11 video drivers to the Mac OS X port of SDL 1.2 (the CVS version), and made the Cocoa driver and runtime *optional*. The default is still Cocoa, and the "Quartz" driver. But you can now also use "toolbox" for Carbon, and "x11" for running with Apple's (or other) X11 server: export SDL_VIDEODRIVER=x11 export SDL_VIDEO_GL_DRIVER=/usr/X11R6/lib/libGL.dylib It also checks if the frameworks are available, by a: #include <Carbon/Carbon.h> or #import <Cocoa/Cocoa.h> (this should make it configure on plain Darwin as well?) Here are the new configure targets: --enable-video-cocoa use Cocoa/Quartz video driver default=yes --enable-video-carbon use Carbon/QuickDraw video driver default=yes --enable-video-x11 use X11 video driver default=no ./configure --enable-video-cocoa --enable-video-carbon --enable-video-x11 \ --x-includes=/usr/X11R6/include --x-libraries=/usr/X11R6/lib The Carbon version is just an updated version of the old SDL driver for Mac OS 9, and could probably be improved... (but it does work, including the Carbon version of SDLmain) If you disable cocoa, you can run with -framework Carbon only, and the C version of SDL_main.c. And if you disable carbon too, you can still use the X11 version which doesn't require SDLmain. I updated the DrawSprocket version, but did not include it. (no blitters or VRAM GWorlds etc. available on OS X anyway) Besides for Mac OS 9, I don't think there's any use for it ? And note that any performance on Mac OS X equals OpenGL anyway... You can get "fair" software SDL results on captured CG displays, but for decent frame rates you need to be using GL for rendering. Finally, here is the patch itself: http://www.algonet.se/~afb/SDL-12CVS-macvideo.patch --anders PS. It says "video", but as usual it applies to mouse/keyboard too. ------ To: A list for developers using the SDL library <sdl@libsdl.org> From: =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb@algonet.se> Date: Sun, 4 Sep 2005 10:02:15 +0200 Subject: [SDL] Updated Mac patch Updated the previous Mac patch to disable Carbon by default. Also "fixed" the SDL.spec again, so that it builds on Darwin. http://www.algonet.se/~afb/SDL-1.2.9-mac.patch Also applied fine to SDL12 CVS, when I tried it.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 08 Sep 2005 06:16:14 +0000
parents b8d311d90021
children fd068ab116ee
line wrap: on
line source

/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2004 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Sam Lantinga
    slouken@libsdl.org
*/

#ifdef SAVE_RCSID
static char rcsid =
 "@(#) $Id$";
#endif

#include <stdlib.h>
#include "SDL_error.h"
#include "SDL_thread.h"
#include "SDL_timer.h"

#ifdef linux
/* Look to see if glibc is available, and if so, what version */
#include <features.h>

#if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
#warning Working around a bug in glibc 2.0 pthreads
#undef SDL_USE_PTHREADS
/* The bug is actually a problem where threads are suspended, but don't
   wake up when the thread manager sends them a signal.  This is a problem
   with thread creation too, but it happens less often. :-/
   We avoid this by using System V IPC for semaphores.
 */
#endif /* glibc 2.0 */
#endif /* linux */

#ifdef SDL_USE_PTHREADS

#ifdef SDL_NO_PTHREAD_SEMAPHORES
#include "generic/SDL_syssem.c"
#else

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>			/* For getpid() */
#include <pthread.h>
#include <semaphore.h>

/* Wrapper around POSIX 1003.1b semaphores */

#ifdef MACOSX
#define USE_NAMED_SEMAPHORES
/* Broken sem_getvalue() in MacOS X Public Beta */
#define BROKEN_SEMGETVALUE
#endif /* MACOSX */

struct SDL_semaphore {
	sem_t *sem;
#ifndef USE_NAMED_SEMAPHORES
	sem_t sem_data;
#endif
#ifdef BROKEN_SEMGETVALUE
	/* This is a little hack for MacOS X -
	   It's not thread-safe, but it's better than nothing
	 */
	int sem_value;
#endif
};

/* Create a semaphore, initialized with value */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
	SDL_sem *sem = (SDL_sem *) malloc(sizeof(SDL_sem));
	if ( sem ) {
#ifdef USE_NAMED_SEMAPHORES
		static int semnum = 0;
		char name[32];

		sprintf(name, "/SDL_sem-%d-%4.4d", getpid(), semnum++);
		sem->sem = sem_open(name, O_CREAT, 0600, initial_value);
		if ( sem->sem == (sem_t *)SEM_FAILED ) {
			SDL_SetError("sem_open(%s) failed", name);
			free(sem);
			sem = NULL;
		} else {
			sem_unlink(name);
		}
#else
		if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
			SDL_SetError("sem_init() failed");
			free(sem);
			sem = NULL;
		} else {
			sem->sem = &sem->sem_data;
		}
#endif /* USE_NAMED_SEMAPHORES */

#ifdef BROKEN_SEMGETVALUE
		if ( sem ) {
			sem->sem_value = initial_value;
		}
#endif /* BROKEN_SEMGETVALUE */
	} else {
		SDL_OutOfMemory();
	}
	return sem;
}

void SDL_DestroySemaphore(SDL_sem *sem)
{
	if ( sem ) {
#ifdef USE_NAMED_SEMAPHORES
		sem_close(sem->sem);
#else
		sem_destroy(sem->sem);
#endif
		free(sem);
	}
}

int SDL_SemTryWait(SDL_sem *sem)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}
	retval = SDL_MUTEX_TIMEDOUT;
	if ( sem_trywait(sem->sem) == 0 ) {
#ifdef BROKEN_SEMGETVALUE
		--sem->sem_value;
#endif
		retval = 0;
	}
	return retval;
}

int SDL_SemWait(SDL_sem *sem)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

#ifdef BROKEN_SEMGETVALUE
	--sem->sem_value;
#endif
	retval = sem_wait(sem->sem);
	if ( retval < 0 ) {
		SDL_SetError("sem_wait() failed");
	}
	return retval;
}

int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

	/* Try the easy cases first */
	if ( timeout == 0 ) {
		return SDL_SemTryWait(sem);
	}
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
		return SDL_SemWait(sem);
	}

	/* Ack!  We have to busy wait... */
	timeout += SDL_GetTicks();
	do {
		retval = SDL_SemTryWait(sem);
		if ( retval == 0 ) {
			break;
		}
		SDL_Delay(1);
	} while ( SDL_GetTicks() < timeout );

	return retval;
}

Uint32 SDL_SemValue(SDL_sem *sem)
{
	int ret = 0;
	if ( sem ) {
#ifdef BROKEN_SEMGETVALUE
		ret = sem->sem_value;
#else
		sem_getvalue(sem->sem, &ret);
#endif
		if ( ret < 0 ) {
			ret = 0;
		}
	}
	return (Uint32)ret;
}

int SDL_SemPost(SDL_sem *sem)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

#ifdef BROKEN_SEMGETVALUE
	++sem->sem_value;
#endif
	retval = sem_post(sem->sem);
	if ( retval < 0 ) {
		SDL_SetError("sem_post() failed");
	}
	return retval;
}

#endif /* NO_PTHREAD_SEMAPHORES */

#else /* System V IPC implementation */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>

#include "SDL_error.h"
#include "SDL_thread.h"


struct SDL_semaphore {
	int id;
};

/* Not defined by many operating systems, use configure to detect */
#if !defined(HAVE_SEMUN)
union semun {
	int val;
	struct semid_ds *buf;
	ushort *array;
};
#endif

static struct sembuf op_trywait[2] = {
	{ 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */
};
static struct sembuf op_wait[2] = {
	{ 0, -1, SEM_UNDO }		/* Decrement semaphore */
};
static struct sembuf op_post[1] = {
	{ 0, 1, (IPC_NOWAIT|SEM_UNDO) }	/* Increment semaphore */
};

/* Create a blockable semaphore */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
	extern int _creating_thread_lock;	/* SDL_threads.c */
	SDL_sem *sem;
	union semun init;
	key_t key;

	sem = (SDL_sem *)malloc(sizeof(*sem));
	if ( sem == NULL ) {
		SDL_OutOfMemory();
		return(NULL);
	}
	/* This flag is true if we are creating the thread manager sem,
	   which is never freed.  This allows us to reuse the same sem.
	*/
	if ( _creating_thread_lock ) {
		key = 'S'+'D'+'L';
	} else {
		key = IPC_PRIVATE;
	}
	/* Keep trying to create sem while we don't own the requested key */
	do {
		if ( key != IPC_PRIVATE ) {
			++key;
		}
		sem->id = semget(key, 1, (0600|IPC_CREAT));
	} while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES));

	/* Report the error if we eventually failed */
	if ( sem->id < 0 ) {
		SDL_SetError("Couldn't create semaphore");
		free(sem);
		return(NULL);
	}
	init.val = initial_value;	/* Initialize semaphore */
	semctl(sem->id, 0, SETVAL, init);
	return(sem);
}

void SDL_DestroySemaphore(SDL_sem *sem)
{
	if ( sem ) {
#ifdef _SGI_SOURCE
		semctl(sem->id, 0, IPC_RMID);
#else
		union semun dummy;
		dummy.val = 0;
		semctl(sem->id, 0, IPC_RMID, dummy);
#endif
		free(sem);
	}
}

int SDL_SemTryWait(SDL_sem *sem)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

	retval = 0;
  tryagain:
	if ( semop(sem->id, op_trywait, 1) < 0 ) {
		if ( errno == EINTR ) {
			goto tryagain;
		}
		retval = SDL_MUTEX_TIMEDOUT;
	}
	return retval;
}

int SDL_SemWait(SDL_sem *sem)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

	retval = 0;
  tryagain:
	if ( semop(sem->id, op_wait, 1) < 0 ) {
		if ( errno == EINTR ) {
			goto tryagain;
		}
		SDL_SetError("Semaphore operation error");
		retval = -1;
	}
	return retval;
}

int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

	/* Try the easy cases first */
	if ( timeout == 0 ) {
		return SDL_SemTryWait(sem);
	}
	if ( timeout == SDL_MUTEX_MAXWAIT ) {
		return SDL_SemWait(sem);
	}

	/* Ack!  We have to busy wait... */
	timeout += SDL_GetTicks();
	do {
		retval = SDL_SemTryWait(sem);
		if ( retval == 0 ) {
			break;
		}
		SDL_Delay(1);
	} while ( SDL_GetTicks() < timeout );

	return retval;
}

Uint32 SDL_SemValue(SDL_sem *sem)
{
	int semval;
	Uint32 value;
	
	value = 0;
	if ( sem ) {
	  tryagain:
#ifdef _SGI_SOURCE
		semval = semctl(sem->id, 0, GETVAL);
#else
		{
		union semun arg;
		arg.val = 0;
		semval = semctl(sem->id, 0, GETVAL, arg);
		}
#endif
		if ( semval < 0 ) {
			if ( errno == EINTR ) {
				goto tryagain;
			}
		} else {
			value = (Uint32)semval;
		}
	}
	return value;
}

int SDL_SemPost(SDL_sem *sem)
{
	int retval;

	if ( ! sem ) {
		SDL_SetError("Passed a NULL semaphore");
		return -1;
	}

	retval = 0;
  tryagain:
	if ( semop(sem->id, op_post, 1) < 0 ) {
		if ( errno == EINTR ) {
			goto tryagain;
		}
		SDL_SetError("Semaphore operation error");
		retval = -1;
	}
	return retval;
}

#endif /* SDL_USE_PTHREADS */