src/thread/pthread/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 25 Mar 2011 10:47:49 -0700
changeset 5506 82a09d5d0f07
parent 5262 b530ef003506
child 5509 5b1b4d820d10
permissions -rw-r--r--
Implemented SDL_SetThreadPriority()
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include <pthread.h>
    25 #include <signal.h>
    26 
    27 #include "SDL_thread.h"
    28 #include "../SDL_thread_c.h"
    29 #include "../SDL_systhread.h"
    30 
    31 /* List of signals to mask in the subthreads */
    32 static const int sig_list[] = {
    33     SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    34     SIGVTALRM, SIGPROF, 0
    35 };
    36 
    37 static void *
    38 RunThread(void *data)
    39 {
    40     SDL_RunThread(data);
    41     pthread_exit((void *) 0);
    42     return ((void *) 0);        /* Prevent compiler warning */
    43 }
    44 
    45 int
    46 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    47 {
    48     pthread_attr_t type;
    49 
    50     /* Set the thread attributes */
    51     if (pthread_attr_init(&type) != 0) {
    52         SDL_SetError("Couldn't initialize pthread attributes");
    53         return (-1);
    54     }
    55     pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    56 
    57     /* Create the thread and go! */
    58     if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
    59         SDL_SetError("Not enough resources to create thread");
    60         return (-1);
    61     }
    62 
    63     return (0);
    64 }
    65 
    66 void
    67 SDL_SYS_SetupThread(void)
    68 {
    69     int i;
    70     sigset_t mask;
    71 
    72     /* Mask asynchronous signals for this thread */
    73     sigemptyset(&mask);
    74     for (i = 0; sig_list[i]; ++i) {
    75         sigaddset(&mask, sig_list[i]);
    76     }
    77     pthread_sigmask(SIG_BLOCK, &mask, 0);
    78 
    79 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
    80     /* Allow ourselves to be asynchronously cancelled */
    81     {
    82         int oldstate;
    83         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
    84     }
    85 #endif
    86 }
    87 
    88 SDL_threadID
    89 SDL_ThreadID(void)
    90 {
    91     return ((SDL_threadID) pthread_self());
    92 }
    93 
    94 int
    95 SDL_SYS_SetThreadPriority(SDL_Thread * thread, SDL_ThreadPriority priority)
    96 {
    97     struct sched_param sched;
    98     int policy;
    99 
   100     if (pthread_getschedparam(thread->handle, &policy, &sched) < 0) {
   101         SDL_SetError("pthread_getschedparam() failed");
   102         return -1;
   103     }
   104     if (priority == SDL_THREAD_PRIORITY_LOW) {
   105         sched.sched_priority = sched_get_priority_min(policy);
   106     } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
   107         sched.sched_priority = sched_get_priority_max(policy);
   108     } else {
   109         int min_priority = sched_get_priority_min(policy);
   110         int max_priority = sched_get_priority_max(policy);
   111         int priority = (min_priority + (max_priority - min_priority) / 2);
   112         sched.sched_priority = priority;
   113     }
   114     if (pthread_setschedparam(thread->handle, policy, &sched) < 0) {
   115         SDL_SetError("pthread_setschedparam() failed");
   116         return -1;
   117     }
   118     return 0;
   119 }
   120 
   121 void
   122 SDL_SYS_WaitThread(SDL_Thread * thread)
   123 {
   124     pthread_join(thread->handle, 0);
   125 }
   126 
   127 /* vi: set ts=4 sw=4 expandtab: */