test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8149 681eb46b8ac4
child 9356 e87d6e1e812a
permissions -rw-r--r--
Fixed bug 2786 - "UCS-2-INTERNAL" iconv encoding is not supported everywhere, use UTF-16LE instead

Jonas Kulla

src/main/windows/SDL_windows_main.c:137:
cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));

I'm trying to compile an SDL2 application for windows using the mingw-w64 32bit toolchain provided by my distro (Fedora 19). However, even the simplest test program that does nothing at all fails to startup with a "Fatal error - out of memory" message because the mingw iconv library provided by my distro does not support the "UCS-2-INTERNAL" encoding and the conversion returns null.

From my little bit of research, it turns out that even though this encoding is supported by the external GNU libiconv library, some glibc versions (?) don't support it with their internal iconv routines, and will instead provide the native endian encoding when "UCS-2" is specified.

Nonetheless, I wonder why the native endianness is considered in the first place when Windows doesn't even run on any big endian archs (to my knowledge). And true enough, 'WIN_StringToUTF8' from core/windows/SDL_windows.h is used everywhere else in the windows backend, which is just a macro to iconv with "UTF-16LE" as source. Therefore it would IMO make sense to use this macro here as well, which would solve my problem (patch attached).
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Simple test of the SDL semaphore code */
slouken@0
    14
slouken@0
    15
#include <stdio.h>
slouken@0
    16
#include <stdlib.h>
slouken@0
    17
#include <signal.h>
slouken@0
    18
slouken@0
    19
#include "SDL.h"
slouken@0
    20
#include "SDL_thread.h"
slouken@0
    21
slouken@0
    22
#define NUM_THREADS 10
slouken@0
    23
slouken@0
    24
static SDL_sem *sem;
slouken@0
    25
int alive = 1;
slouken@0
    26
slouken@1895
    27
int SDLCALL
slouken@1895
    28
ThreadFunc(void *data)
slouken@0
    29
{
slouken@1895
    30
    int threadnum = (int) (uintptr_t) data;
slouken@1895
    31
    while (alive) {
slouken@1895
    32
        SDL_SemWait(sem);
aschiffler@7639
    33
        SDL_Log("Thread number %d has got the semaphore (value = %d)!\n",
slouken@1895
    34
                threadnum, SDL_SemValue(sem));
slouken@1895
    35
        SDL_Delay(200);
slouken@1895
    36
        SDL_SemPost(sem);
aschiffler@7639
    37
        SDL_Log("Thread number %d has released the semaphore (value = %d)!\n",
slouken@1895
    38
                threadnum, SDL_SemValue(sem));
slouken@1895
    39
        SDL_Delay(1);           /* For the scheduler */
slouken@1895
    40
    }
aschiffler@7639
    41
    SDL_Log("Thread number %d exiting.\n", threadnum);
slouken@1895
    42
    return 0;
slouken@0
    43
}
slouken@0
    44
slouken@1895
    45
static void
slouken@1895
    46
killed(int sig)
slouken@0
    47
{
slouken@1895
    48
    alive = 0;
slouken@0
    49
}
slouken@0
    50
slouken@5106
    51
static void
slouken@5106
    52
TestWaitTimeout(void)
slouken@5106
    53
{
slouken@5106
    54
    Uint32 start_ticks;
slouken@5106
    55
    Uint32 end_ticks;
slouken@5106
    56
    Uint32 duration;
slouken@6298
    57
    int retval;
slouken@5106
    58
slouken@5106
    59
    sem = SDL_CreateSemaphore(0);
aschiffler@7639
    60
    SDL_Log("Waiting 2 seconds on semaphore\n");
slouken@5106
    61
slouken@5106
    62
    start_ticks = SDL_GetTicks();
slouken@6298
    63
    retval = SDL_SemWaitTimeout(sem, 2000);
slouken@5106
    64
    end_ticks = SDL_GetTicks();
slouken@5106
    65
slouken@5106
    66
    duration = end_ticks - start_ticks;
slouken@5106
    67
slouken@5106
    68
    /* Accept a little offset in the effective wait */
slouken@5106
    69
    if (duration > 1900 && duration < 2050)
aschiffler@7639
    70
        SDL_Log("Wait done.\n");
slouken@5106
    71
    else
aschiffler@7639
    72
        SDL_Log("Wait took %d milliseconds\n", duration);
slouken@6298
    73
slouken@6298
    74
    /* Check to make sure the return value indicates timed out */
slouken@7191
    75
    if (retval != SDL_MUTEX_TIMEDOUT)
aschiffler@7639
    76
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
slouken@5106
    77
}
slouken@5106
    78
slouken@1895
    79
int
slouken@1895
    80
main(int argc, char **argv)
slouken@0
    81
{
slouken@1895
    82
    SDL_Thread *threads[NUM_THREADS];
slouken@1895
    83
    uintptr_t i;
slouken@1895
    84
    int init_sem;
slouken@0
    85
aschiffler@7639
    86
	/* Enable standard application logging */
aschiffler@7639
    87
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    88
slouken@1895
    89
    if (argc < 2) {
aschiffler@7639
    90
        SDL_Log("Usage: %s init_value\n", argv[0]);
slouken@1895
    91
        return (1);
slouken@1895
    92
    }
slouken@0
    93
slouken@1895
    94
    /* Load the SDL library */
slouken@1895
    95
    if (SDL_Init(0) < 0) {
aschiffler@7639
    96
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    97
        return (1);
slouken@1895
    98
    }
slouken@1895
    99
    signal(SIGTERM, killed);
slouken@1895
   100
    signal(SIGINT, killed);
slouken@0
   101
slouken@1895
   102
    init_sem = atoi(argv[1]);
slouken@1895
   103
    sem = SDL_CreateSemaphore(init_sem);
slouken@0
   104
aschiffler@7639
   105
    SDL_Log("Running %d threads, semaphore value = %d\n", NUM_THREADS,
slouken@1895
   106
           init_sem);
slouken@1895
   107
    /* Create all the threads */
slouken@1895
   108
    for (i = 0; i < NUM_THREADS; ++i) {
icculus@5969
   109
        char name[64];
icculus@5969
   110
        SDL_snprintf(name, sizeof (name), "Thread%u", (unsigned int) i);
icculus@5969
   111
        threads[i] = SDL_CreateThread(ThreadFunc, name, (void *) i);
slouken@1895
   112
    }
slouken@0
   113
slouken@1895
   114
    /* Wait 10 seconds */
slouken@1895
   115
    SDL_Delay(10 * 1000);
slouken@1895
   116
slouken@1895
   117
    /* Wait for all threads to finish */
aschiffler@7639
   118
    SDL_Log("Waiting for threads to finish\n");
slouken@1895
   119
    alive = 0;
slouken@1895
   120
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
   121
        SDL_WaitThread(threads[i], NULL);
slouken@1895
   122
    }
aschiffler@7639
   123
    SDL_Log("Finished waiting for threads\n");
slouken@1895
   124
slouken@1895
   125
    SDL_DestroySemaphore(sem);
slouken@5106
   126
slouken@5106
   127
    TestWaitTimeout();
slouken@5106
   128
slouken@1895
   129
    SDL_Quit();
slouken@1895
   130
    return (0);
slouken@0
   131
}