test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8583 fb2933ca805f
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@8582
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     3
slouken@8582
     4
  This software is provided 'as-is', without any express or implied
slouken@8582
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     6
  arising from the use of this software.
slouken@8582
     7
slouken@8582
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
     9
  including commercial applications, and to alter it and redistribute it
slouken@8582
    10
  freely.
slouken@8582
    11
*/
slouken@8582
    12
slouken@8582
    13
/* Simple test of the SDL threading code */
slouken@8582
    14
slouken@8582
    15
#include <stdio.h>
slouken@8582
    16
#include <stdlib.h>
slouken@8582
    17
#include <signal.h>
slouken@8582
    18
slouken@8582
    19
#include "SDL.h"
slouken@8582
    20
#include "SDL_thread.h"
slouken@8582
    21
slouken@8582
    22
static SDL_TLSID tls;
slouken@8582
    23
static int alive = 0;
slouken@8582
    24
slouken@8582
    25
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@8582
    26
static void
slouken@8582
    27
quit(int rc)
slouken@8582
    28
{
slouken@8582
    29
    SDL_Quit();
slouken@8582
    30
    exit(rc);
slouken@8582
    31
}
slouken@8582
    32
slouken@8582
    33
int SDLCALL
slouken@8582
    34
ThreadFunc(void *data)
slouken@8582
    35
{
slouken@8582
    36
    SDL_TLSSet(tls, "baby thread", NULL);
slouken@8582
    37
    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
slouken@8582
    38
           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
slouken@8582
    39
    while (alive) {
slouken@8582
    40
        SDL_Log("Thread '%s' is alive!\n", (char *) data);
slouken@8582
    41
        SDL_Delay(1 * 1000);
slouken@8582
    42
    }
slouken@8582
    43
    SDL_Log("Thread '%s' exiting!\n", (char *) data);
slouken@8582
    44
    return (0);
slouken@8582
    45
}
slouken@8582
    46
slouken@8582
    47
static void
slouken@8582
    48
killed(int sig)
slouken@8582
    49
{
slouken@8582
    50
    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@8582
    51
    SDL_Delay(5 * 1000);
slouken@8582
    52
    alive = 0;
slouken@8582
    53
    quit(0);
slouken@8582
    54
}
slouken@8582
    55
slouken@8582
    56
int
slouken@8582
    57
main(int argc, char *argv[])
slouken@8582
    58
{
slouken@8582
    59
    SDL_Thread *thread;
slouken@8582
    60
slouken@8582
    61
	/* Enable standard application logging */
slouken@8582
    62
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
    63
slouken@8582
    64
    /* Load the SDL library */
slouken@8582
    65
    if (SDL_Init(0) < 0) {
slouken@8582
    66
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
    67
        return (1);
slouken@8582
    68
    }
slouken@8582
    69
slouken@8582
    70
    tls = SDL_TLSCreate();
slouken@8582
    71
    SDL_assert(tls);
slouken@8582
    72
    SDL_TLSSet(tls, "main thread", NULL);
slouken@8582
    73
    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
    74
slouken@8582
    75
    alive = 1;
slouken@8582
    76
    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
slouken@8582
    77
    if (thread == NULL) {
slouken@8582
    78
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
    79
        quit(1);
slouken@8582
    80
    }
slouken@8582
    81
    SDL_Delay(5 * 1000);
slouken@8582
    82
    SDL_Log("Waiting for thread #1\n");
slouken@8582
    83
    alive = 0;
slouken@8582
    84
    SDL_WaitThread(thread, NULL);
slouken@8582
    85
slouken@8582
    86
    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
    87
slouken@8582
    88
    alive = 1;
slouken@8582
    89
    signal(SIGTERM, killed);
slouken@8582
    90
    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
slouken@8582
    91
    if (thread == NULL) {
slouken@8582
    92
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
    93
        quit(1);
slouken@8582
    94
    }
slouken@8582
    95
    raise(SIGTERM);
slouken@8582
    96
slouken@8582
    97
    SDL_Quit();                 /* Never reached */
slouken@8582
    98
    return (0);                 /* Never reached */
slouken@8582
    99
}