src/thread/SDL_thread.c
changeset 5969 3a041d215edc
parent 5535 96594ac5fd1a
child 6044 35448a5ea044
equal deleted inserted replaced
5968:b229328c7ad4 5969:3a041d215edc
   186 } thread_args;
   186 } thread_args;
   187 
   187 
   188 void
   188 void
   189 SDL_RunThread(void *data)
   189 SDL_RunThread(void *data)
   190 {
   190 {
   191     thread_args *args;
   191     thread_args *args = (thread_args *) data;
   192     int (SDLCALL * userfunc) (void *);
   192     int (SDLCALL * userfunc) (void *) = args->func;
   193     void *userdata;
   193     void *userdata = args->data;
   194     int *statusloc;
   194     int *statusloc = &args->info->status;
   195 
   195 
   196     /* Perform any system-dependent setup
   196     /* Perform any system-dependent setup
   197        - this function cannot fail, and cannot use SDL_SetError()
   197        - this function cannot fail, and cannot use SDL_SetError()
   198      */
   198      */
   199     SDL_SYS_SetupThread();
   199     SDL_SYS_SetupThread(args->info->name);
   200 
   200 
   201     /* Get the thread id */
   201     /* Get the thread id */
   202     args = (thread_args *) data;
       
   203     args->info->threadid = SDL_ThreadID();
   202     args->info->threadid = SDL_ThreadID();
   204 
       
   205     /* Figure out what function to run */
       
   206     userfunc = args->func;
       
   207     userdata = args->data;
       
   208     statusloc = &args->info->status;
       
   209 
   203 
   210     /* Wake up the parent thread */
   204     /* Wake up the parent thread */
   211     SDL_SemPost(args->wait);
   205     SDL_SemPost(args->wait);
   212 
   206 
   213     /* Run the function */
   207     /* Run the function */
   215 }
   209 }
   216 
   210 
   217 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
   211 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
   218 #undef SDL_CreateThread
   212 #undef SDL_CreateThread
   219 DECLSPEC SDL_Thread *SDLCALL
   213 DECLSPEC SDL_Thread *SDLCALL
   220 SDL_CreateThread(int (SDLCALL * fn) (void *), void *data,
   214 SDL_CreateThread(int (SDLCALL * fn) (void *),
       
   215                  const char *name, void *data,
   221                  pfnSDL_CurrentBeginThread pfnBeginThread,
   216                  pfnSDL_CurrentBeginThread pfnBeginThread,
   222                  pfnSDL_CurrentEndThread pfnEndThread)
   217                  pfnSDL_CurrentEndThread pfnEndThread)
   223 #else
   218 #else
   224 DECLSPEC SDL_Thread *SDLCALL
   219 DECLSPEC SDL_Thread *SDLCALL
   225 SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
   220 SDL_CreateThread(int (SDLCALL * fn) (void *),
       
   221                  const char *name, void *data)
   226 #endif
   222 #endif
   227 {
   223 {
   228     SDL_Thread *thread;
   224     SDL_Thread *thread;
   229     thread_args *args;
   225     thread_args *args;
   230     int ret;
   226     int ret;
   237     }
   233     }
   238     SDL_memset(thread, 0, (sizeof *thread));
   234     SDL_memset(thread, 0, (sizeof *thread));
   239     thread->status = -1;
   235     thread->status = -1;
   240 
   236 
   241     /* Set up the arguments for the thread */
   237     /* Set up the arguments for the thread */
       
   238     if (name != NULL) {
       
   239         thread->name = SDL_strdup(name);
       
   240         if (thread->name == NULL) {
       
   241             SDL_OutOfMemory();
       
   242             SDL_free(thread);
       
   243             return (NULL);
       
   244         }
       
   245     }
       
   246 
       
   247     /* Set up the arguments for the thread */
   242     args = (thread_args *) SDL_malloc(sizeof(*args));
   248     args = (thread_args *) SDL_malloc(sizeof(*args));
   243     if (args == NULL) {
   249     if (args == NULL) {
   244         SDL_OutOfMemory();
   250         SDL_OutOfMemory();
       
   251         SDL_free(thread->name);
   245         SDL_free(thread);
   252         SDL_free(thread);
   246         return (NULL);
   253         return (NULL);
   247     }
   254     }
   248     args->func = fn;
   255     args->func = fn;
   249     args->data = data;
   256     args->data = data;
   250     args->info = thread;
   257     args->info = thread;
   251     args->wait = SDL_CreateSemaphore(0);
   258     args->wait = SDL_CreateSemaphore(0);
   252     if (args->wait == NULL) {
   259     if (args->wait == NULL) {
       
   260         SDL_free(thread->name);
   253         SDL_free(thread);
   261         SDL_free(thread);
   254         SDL_free(args);
   262         SDL_free(args);
   255         return (NULL);
   263         return (NULL);
   256     }
   264     }
   257 
   265 
   268         /* Wait for the thread function to use arguments */
   276         /* Wait for the thread function to use arguments */
   269         SDL_SemWait(args->wait);
   277         SDL_SemWait(args->wait);
   270     } else {
   278     } else {
   271         /* Oops, failed.  Gotta free everything */
   279         /* Oops, failed.  Gotta free everything */
   272         SDL_DelThread(thread);
   280         SDL_DelThread(thread);
       
   281         SDL_free(thread->name);
   273         SDL_free(thread);
   282         SDL_free(thread);
   274         thread = NULL;
   283         thread = NULL;
   275     }
   284     }
   276     SDL_DestroySemaphore(args->wait);
   285     SDL_DestroySemaphore(args->wait);
   277     SDL_free(args);
   286     SDL_free(args);
   291         id = SDL_ThreadID();
   300         id = SDL_ThreadID();
   292     }
   301     }
   293     return id;
   302     return id;
   294 }
   303 }
   295 
   304 
       
   305 const char *
       
   306 SDL_GetThreadName(SDL_Thread * thread)
       
   307 {
       
   308     return thread->name;
       
   309 }
       
   310 
   296 int
   311 int
   297 SDL_SetThreadPriority(SDL_ThreadPriority priority)
   312 SDL_SetThreadPriority(SDL_ThreadPriority priority)
   298 {
   313 {
   299     return SDL_SYS_SetThreadPriority(priority);
   314     return SDL_SYS_SetThreadPriority(priority);
   300 }
   315 }
   306         SDL_SYS_WaitThread(thread);
   321         SDL_SYS_WaitThread(thread);
   307         if (status) {
   322         if (status) {
   308             *status = thread->status;
   323             *status = thread->status;
   309         }
   324         }
   310         SDL_DelThread(thread);
   325         SDL_DelThread(thread);
       
   326         SDL_free(thread->name);
   311         SDL_free(thread);
   327         SDL_free(thread);
   312     }
   328     }
   313 }
   329 }
   314 
   330 
   315 /* vi: set ts=4 sw=4 expandtab: */
   331 /* vi: set ts=4 sw=4 expandtab: */