test/testloadso.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 07:32:26 +0000
branchSDL-1.2
changeset 4209 62e86ab81e3c
parent 3893 f1699aecb65d
child 2120 2c835d58faad
permissions -rw-r--r--
Fixed bug #570

SDL_SemWaitTimeout in src/thread/generic/SDL_syssem.c line 179 (SVN trunk):

--sem->count;

should be

if (retval == 0) {
--sem->count;
}

Without this, sem->count will underflow on timeout effectively breaking the
semaphore. It appears that the implementation has been wrong since the initial
revision.
icculus@3892
     1
icculus@3892
     2
/* Test program to test dynamic loading with the loadso subsystem. 
icculus@3892
     3
*/
icculus@3892
     4
icculus@3892
     5
#include <stdio.h>
icculus@3892
     6
#include <stdlib.h>
icculus@3892
     7
icculus@3892
     8
#include "SDL.h"
icculus@3892
     9
icculus@3892
    10
typedef int (*fntype)(const char *);
icculus@3892
    11
icculus@3892
    12
int main(int argc, char *argv[])
icculus@3892
    13
{
icculus@3892
    14
	int retval = 0;
icculus@3892
    15
	int hello = 0;
icculus@3892
    16
	const char *libname = NULL;
icculus@3892
    17
	const char *symname = NULL;
icculus@3892
    18
	void *lib = NULL;
icculus@3892
    19
	fntype fn = NULL;
icculus@3892
    20
icculus@3892
    21
	if (argc != 3) {
icculus@3893
    22
		const char *app = argv[0];
icculus@3893
    23
		fprintf(stderr, "USAGE: %s <library> <functionname>\n", app);
icculus@3893
    24
		fprintf(stderr, "       %s --hello <lib with puts()>\n", app);
icculus@3892
    25
		return 1;
icculus@3892
    26
	}
icculus@3892
    27
icculus@3892
    28
	/* Initialize SDL */
icculus@3892
    29
	if ( SDL_Init(0) < 0 ) {
icculus@3892
    30
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@3892
    31
		return 2;
icculus@3892
    32
	}
icculus@3892
    33
icculus@3892
    34
	if (strcmp(argv[1], "--hello") == 0) {
icculus@3892
    35
		hello = 1;
icculus@3892
    36
		libname = argv[2];
icculus@3892
    37
		symname = "puts";
icculus@3892
    38
	} else {
icculus@3892
    39
		libname = argv[1];
icculus@3892
    40
		symname = argv[2];
icculus@3892
    41
	}
icculus@3892
    42
icculus@3892
    43
	lib = SDL_LoadObject(libname);
icculus@3892
    44
        if (lib == NULL) {
icculus@3892
    45
		fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n",
icculus@3892
    46
		        libname, SDL_GetError());
icculus@3892
    47
		retval = 3;
icculus@3892
    48
	} else {
icculus@3892
    49
		fn = (fntype) SDL_LoadFunction(lib, symname);
icculus@3892
    50
		if (fn == NULL) {
icculus@3892
    51
			fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n",
icculus@3892
    52
			        symname, SDL_GetError());
icculus@3892
    53
			retval = 4;
icculus@3892
    54
		} else {
icculus@3893
    55
			printf("Found %s in %s at %p\n", symname, libname, fn);
icculus@3892
    56
			if (hello) {
icculus@3892
    57
				printf("Calling function...\n");
icculus@3892
    58
				fflush(stdout);
icculus@3892
    59
				fn("     HELLO, WORLD!\n");
icculus@3892
    60
				printf("...apparently, we survived.  :)\n");
icculus@3892
    61
				printf("Unloading library...\n");
icculus@3892
    62
				fflush(stdout);
icculus@3892
    63
			}
icculus@3892
    64
		}
icculus@3892
    65
		SDL_UnloadObject(lib);
icculus@3892
    66
	}
icculus@3892
    67
	SDL_Quit();
icculus@3892
    68
	return(0);
icculus@3892
    69
}
icculus@3892
    70
icculus@3892
    71