Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@devolution.com
  21. */
  22.  
  23.  
  24. /* System independent thread management routines for SDL */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #include "SDL_error.h"
  31. #include "SDL_mutex.h"
  32. #include "SDL_thread.h"
  33. #include "SDL_thread_c.h"
  34. #include "SDL_systhread.h"
  35.  
  36. #define ARRAY_CHUNKSIZE 32
  37. /* The array of threads currently active in the application
  38.    (except the main thread)
  39.    The manipulation of an array here is safer than using a linked list.
  40. */
  41. static int SDL_maxthreads = 0;
  42. static int SDL_numthreads = 0;
  43. static SDL_Thread **SDL_Threads = NULL;
  44. static SDL_mutex *thread_lock = NULL;
  45. int _creating_thread_lock = 0;
  46.  
  47. int SDL_ThreadsInit(void)
  48. {
  49.         int retval;
  50.  
  51.         retval = 0;
  52.         /* Set the thread lock creation flag so that we can reuse an
  53.            existing lock on the system - since this mutex never gets
  54.            destroyed (see SDL_ThreadsQuit()), we want to reuse it.
  55.         */
  56.         _creating_thread_lock = 1;
  57.         thread_lock = SDL_CreateMutex();
  58.         _creating_thread_lock = 0;
  59.         if ( thread_lock == NULL ) {
  60.                 retval = -1;
  61.         }
  62.         return(retval);
  63. }
  64.  
  65. /* This should never be called...
  66.    If this is called by SDL_Quit(), we don't know whether or not we should
  67.    clean up threads here.  If any threads are still running after this call,
  68.    they will no longer have access to any per-thread data.
  69.  */
  70. void SDL_ThreadsQuit()
  71. {
  72.         SDL_mutex *mutex;
  73.  
  74.         mutex = thread_lock;
  75.         thread_lock = NULL;
  76.         if ( mutex != NULL ) {
  77.                 SDL_DestroyMutex(mutex);
  78.         }
  79. }
  80.  
  81. /* Routines for manipulating the thread list */
  82. static void SDL_AddThread(SDL_Thread *thread)
  83. {
  84.         SDL_Thread **threads;
  85.  
  86.         /* WARNING:
  87.            If the very first threads are created simultaneously, then
  88.            there could be a race condition causing memory corruption.
  89.            In practice, this isn't a problem because by definition there
  90.            is only one thread running the first time this is called.
  91.         */
  92.         if ( thread_lock == NULL ) {
  93.                 if ( SDL_ThreadsInit() < 0 ) {
  94.                         return;
  95.                 }
  96.         }
  97.         SDL_mutexP(thread_lock);
  98.  
  99.         /* Expand the list of threads, if necessary */
  100. #ifdef DEBUG_THREADS
  101.         printf("Adding thread (%d already - %d max)\n",
  102.                         SDL_numthreads, SDL_maxthreads);
  103. #endif
  104.         if ( SDL_numthreads == SDL_maxthreads ) {
  105.                 threads=(SDL_Thread **)malloc((SDL_maxthreads+ARRAY_CHUNKSIZE)*
  106.                                               (sizeof *threads));
  107.                 if ( threads == NULL ) {
  108.                         SDL_OutOfMemory();
  109.                         goto done;
  110.                 }
  111.                 memcpy(threads, SDL_Threads, SDL_numthreads*(sizeof *threads));
  112.                 SDL_maxthreads += ARRAY_CHUNKSIZE;
  113.                 if ( SDL_Threads ) {
  114.                         free(SDL_Threads);
  115.                 }
  116.                 SDL_Threads = threads;
  117.         }
  118.         SDL_Threads[SDL_numthreads++] = thread;
  119. done:
  120.         SDL_mutexV(thread_lock);
  121. }
  122.  
  123. static void SDL_DelThread(SDL_Thread *thread)
  124. {
  125.         int i;
  126.  
  127.         if ( thread_lock ) {
  128.                 SDL_mutexP(thread_lock);
  129.                 for ( i=0; i<SDL_numthreads; ++i ) {
  130.                         if ( thread == SDL_Threads[i] ) {
  131.                                 break;
  132.                         }
  133.                 }
  134.                 if ( i < SDL_numthreads ) {
  135.                         --SDL_numthreads;
  136.                         while ( i < SDL_numthreads ) {
  137.                                 SDL_Threads[i] = SDL_Threads[i+1];
  138.                                 ++i;
  139.                         }
  140. #ifdef DEBUG_THREADS
  141.                         printf("Deleting thread (%d left - %d max)\n",
  142.                                         SDL_numthreads, SDL_maxthreads);
  143. #endif
  144.                 }
  145.                 SDL_mutexV(thread_lock);
  146.         }
  147. }
  148.  
  149. /* The default (non-thread-safe) global error variable */
  150. static SDL_error SDL_global_error;
  151.  
  152. /* Routine to get the thread-specific error variable */
  153. SDL_error *SDL_GetErrBuf(void)
  154. {
  155.         SDL_error *errbuf;
  156.  
  157.         errbuf = &SDL_global_error;
  158.         if ( SDL_Threads ) {
  159.                 int i;
  160.                 Uint32 this_thread;
  161.  
  162.                 this_thread = SDL_ThreadID();
  163.                 SDL_mutexP(thread_lock);
  164.                 for ( i=0; i<SDL_numthreads; ++i ) {
  165.                         if ( this_thread == SDL_Threads[i]->threadid ) {
  166.                                 errbuf = &SDL_Threads[i]->errbuf;
  167.                                 break;
  168.                         }
  169.                 }
  170.                 SDL_mutexV(thread_lock);
  171.         }
  172.         return(errbuf);
  173. }
  174.  
  175.  
  176. /* Arguments and callback to setup and run the user thread function */
  177. typedef struct {
  178.         int (*func)(void *);
  179.         void *data;
  180.         SDL_Thread *info;
  181.         SDL_sem *wait;
  182. } thread_args;
  183.  
  184. void SDL_RunThread(void *data)
  185. {
  186.         thread_args *args;
  187.         int (*userfunc)(void *);
  188.         void *userdata;
  189.         int *statusloc;
  190.  
  191.         /* Perform any system-dependent setup
  192.            - this function cannot fail, and cannot use SDL_SetError()
  193.          */
  194.         SDL_SYS_SetupThread();
  195.  
  196.         /* Get the thread id */
  197.         args = (thread_args *)data;
  198.         args->info->threadid = SDL_ThreadID();
  199.  
  200.         /* Figure out what function to run */
  201.         userfunc = args->func;
  202.         userdata = args->data;
  203.         statusloc = &args->info->status;
  204.  
  205.         /* Wake up the parent thread */
  206.         SDL_SemPost(args->wait);
  207.  
  208.         /* Run the function */
  209.         *statusloc = userfunc(userdata);
  210. }
  211.  
  212. SDL_Thread *SDL_CreateThread(int (*fn)(void *), void *data)
  213. {
  214.         SDL_Thread *thread;
  215.         thread_args *args;
  216.         int ret;
  217.  
  218.         /* Allocate memory for the thread info structure */
  219.         thread = (SDL_Thread *)malloc(sizeof(*thread));
  220.         if ( thread == NULL ) {
  221.                 SDL_OutOfMemory();
  222.                 return(NULL);
  223.         }
  224.         memset(thread, 0, (sizeof *thread));
  225.         thread->status = -1;
  226.  
  227.         /* Set up the arguments for the thread */
  228.         args = (thread_args *)malloc(sizeof(*args));
  229.         if ( args == NULL ) {
  230.                 SDL_OutOfMemory();
  231.                 free(thread);
  232.                 return(NULL);
  233.         }
  234.         args->func = fn;
  235.         args->data = data;
  236.         args->info = thread;
  237.         args->wait = SDL_CreateSemaphore(0);
  238.         if ( args->wait == NULL ) {
  239.                 free(thread);
  240.                 free(args);
  241.                 return(NULL);
  242.         }
  243.  
  244.         /* Add the thread to the list of available threads */
  245.         SDL_AddThread(thread);
  246.  
  247.         /* Create the thread and go! */
  248.         ret = SDL_SYS_CreateThread(thread, args);
  249.         if ( ret >= 0 ) {
  250.                 /* Wait for the thread function to use arguments */
  251.                 SDL_SemWait(args->wait);
  252.         } else {
  253.                 /* Oops, failed.  Gotta free everything */
  254.                 SDL_DelThread(thread);
  255.                 free(thread);
  256.                 thread = NULL;
  257.         }
  258.         SDL_DestroySemaphore(args->wait);
  259.         free(args);
  260.  
  261.         /* Everything is running now */
  262.         return(thread);
  263. }
  264.  
  265. void SDL_WaitThread(SDL_Thread *thread, int *status)
  266. {
  267.         if ( thread ) {
  268.                 SDL_SYS_WaitThread(thread);
  269.                 if ( status ) {
  270.                         *status = thread->status;
  271.                 }
  272.                 SDL_DelThread(thread);
  273.                 free(thread);
  274.         }
  275. }
  276.  
  277. Uint32 SDL_GetThreadID(SDL_Thread *thread)
  278. {
  279.         Uint32 id;
  280.  
  281.         if ( thread ) {
  282.                 id = thread->threadid;
  283.         } else {
  284.                 id = SDL_ThreadID();
  285.         }
  286.         return(id);
  287. }
  288.  
  289. void SDL_KillThread(SDL_Thread *thread)
  290. {
  291.         if ( thread ) {
  292.                 SDL_SYS_KillThread(thread);
  293.                 SDL_WaitThread(thread, NULL);
  294.         }
  295. }
  296.  
  297.