src/thread/os2/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
icculus@1190
     1
/*
icculus@1190
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1190
     4
icculus@1190
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
icculus@1190
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@1190
     9
icculus@1190
    10
    This library is distributed in the hope that it will be useful,
icculus@1190
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1190
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1190
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1190
    18
icculus@1190
    19
    Sam Lantinga
icculus@1190
    20
    slouken@libsdl.org
icculus@1190
    21
*/
icculus@1190
    22
icculus@1190
    23
/* OS/2 thread management routines for SDL */
icculus@1190
    24
icculus@1190
    25
#include <process.h>
icculus@1190
    26
#define INCL_DOSERRORS
icculus@1190
    27
#define INCL_DOSPROCESS
icculus@1190
    28
#include <os2.h>
icculus@1190
    29
icculus@1190
    30
#include "SDL_thread.h"
slouken@1361
    31
#include "../SDL_systhread.h"
icculus@1190
    32
icculus@1190
    33
typedef struct ThreadStartParms
icculus@1190
    34
{
icculus@1190
    35
  void *args;
icculus@1190
    36
  pfnSDL_CurrentEndThread pfnCurrentEndThread;
icculus@1190
    37
} tThreadStartParms, *pThreadStartParms;
icculus@1190
    38
icculus@1190
    39
static void threadfunc(void *pparm)
icculus@1190
    40
{
icculus@1190
    41
  pThreadStartParms pThreadParms = pparm;
icculus@1190
    42
  pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL;
icculus@1190
    43
icculus@1190
    44
  // Call the thread function!
icculus@1190
    45
  SDL_RunThread(pThreadParms->args);
icculus@1190
    46
icculus@1190
    47
  // Get the current endthread we have to use!
icculus@1190
    48
  if (pThreadParms)
icculus@1190
    49
  {
icculus@1190
    50
    pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread;
slouken@1336
    51
    SDL_free(pThreadParms);
icculus@1190
    52
  }
icculus@1190
    53
  // Call endthread!
icculus@1190
    54
  if (pfnCurrentEndThread)
icculus@1190
    55
    (*pfnCurrentEndThread)();
icculus@1190
    56
}
icculus@1190
    57
icculus@1190
    58
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
icculus@1190
    59
{
slouken@1336
    60
  pThreadStartParms pThreadParms = SDL_malloc(sizeof(tThreadStartParms));
icculus@1190
    61
  if (!pThreadParms)
icculus@1190
    62
  {
icculus@1190
    63
    SDL_SetError("Not enough memory to create thread");
icculus@1190
    64
    return(-1);
icculus@1190
    65
  }
icculus@1190
    66
icculus@1190
    67
  // Save the function which we will have to call to clear the RTL of calling app!
icculus@1190
    68
  pThreadParms->pfnCurrentEndThread = pfnEndThread;
icculus@1190
    69
  // Also save the real parameters we have to pass to thread function
icculus@1190
    70
  pThreadParms->args = args;
icculus@1190
    71
  // Start the thread using the runtime library of calling app!
slouken@1330
    72
  thread->threadid = thread->handle = (*pfnBeginThread)(threadfunc, NULL, 512*1024, pThreadParms);
icculus@1190
    73
  if (thread->threadid<=0)
icculus@1190
    74
  {
icculus@1190
    75
    SDL_SetError("Not enough resources to create thread");
icculus@1190
    76
    return(-1);
icculus@1190
    77
  }
icculus@1190
    78
  return(0);
icculus@1190
    79
}
icculus@1190
    80
icculus@1190
    81
void SDL_SYS_SetupThread(void)
icculus@1190
    82
{
icculus@1190
    83
  return;
icculus@1190
    84
}
icculus@1190
    85
icculus@1190
    86
DECLSPEC Uint32 SDLCALL SDL_ThreadID(void)
icculus@1190
    87
{
icculus@1190
    88
  PTIB tib;
icculus@1190
    89
  DosGetInfoBlocks(&tib, NULL);
icculus@1190
    90
  return((Uint32) (tib->tib_ptib2->tib2_ultid));
icculus@1190
    91
}
icculus@1190
    92
icculus@1190
    93
void SDL_SYS_WaitThread(SDL_Thread *thread)
icculus@1190
    94
{
icculus@1190
    95
  TID tid = thread->handle;
icculus@1190
    96
  DosWaitThread(&tid, DCWW_WAIT);
icculus@1190
    97
}
icculus@1190
    98
icculus@1190
    99
/* WARNING: This function is really a last resort.
icculus@1190
   100
 * Threads should be signaled and then exit by themselves.
icculus@1190
   101
 * TerminateThread() doesn't perform stack and DLL cleanup.
icculus@1190
   102
 */
icculus@1190
   103
void SDL_SYS_KillThread(SDL_Thread *thread)
icculus@1190
   104
{
icculus@1190
   105
  DosKillThread(thread->handle);
icculus@1190
   106
}