test/testlock.c
changeset 10003 d91a2c45825e
parent 9998 f67cf37e9cd4
equal deleted inserted replaced
10002:c9cce8633f13 10003:d91a2c45825e
    21 #include "SDL.h"
    21 #include "SDL.h"
    22 
    22 
    23 static SDL_mutex *mutex = NULL;
    23 static SDL_mutex *mutex = NULL;
    24 static SDL_threadID mainthread;
    24 static SDL_threadID mainthread;
    25 static SDL_Thread *threads[6];
    25 static SDL_Thread *threads[6];
    26 static volatile int doterminate = 0;
    26 static SDL_atomic_t doterminate;
    27 
    27 
    28 /*
    28 /*
    29  * SDL_Quit() shouldn't be used with atexit() directly because
    29  * SDL_Quit() shouldn't be used with atexit() directly because
    30  *  calling conventions may differ...
    30  *  calling conventions may differ...
    31  */
    31  */
    43 
    43 
    44 void
    44 void
    45 terminate(int sig)
    45 terminate(int sig)
    46 {
    46 {
    47     signal(SIGINT, terminate);
    47     signal(SIGINT, terminate);
    48     doterminate = 1;
    48     SDL_AtomicSet(&doterminate, 1);
    49 }
    49 }
    50 
    50 
    51 void
    51 void
    52 closemutex(int sig)
    52 closemutex(int sig)
    53 {
    53 {
    54     SDL_threadID id = SDL_ThreadID();
    54     SDL_threadID id = SDL_ThreadID();
    55     int i;
    55     int i;
    56     SDL_Log("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
    56     SDL_Log("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
    57     doterminate = 1;
    57     SDL_AtomicSet(&doterminate, 1);
    58     for (i = 0; i < 6; ++i)
    58     for (i = 0; i < 6; ++i)
    59         SDL_WaitThread(threads[i], NULL);
    59         SDL_WaitThread(threads[i], NULL);
    60     SDL_DestroyMutex(mutex);
    60     SDL_DestroyMutex(mutex);
    61     exit(sig);
    61     exit(sig);
    62 }
    62 }
    64 int SDLCALL
    64 int SDLCALL
    65 Run(void *data)
    65 Run(void *data)
    66 {
    66 {
    67     if (SDL_ThreadID() == mainthread)
    67     if (SDL_ThreadID() == mainthread)
    68         signal(SIGTERM, closemutex);
    68         signal(SIGTERM, closemutex);
    69     while (!doterminate) {
    69     while (!SDL_AtomicGet(&doterminate)) {
    70         SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
    70         SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
    71         if (SDL_LockMutex(mutex) < 0) {
    71         if (SDL_LockMutex(mutex) < 0) {
    72             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
    72             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
    73             exit(1);
    73             exit(1);
    74         }
    74         }
    80             exit(1);
    80             exit(1);
    81         }
    81         }
    82         /* If this sleep isn't done, then threads may starve */
    82         /* If this sleep isn't done, then threads may starve */
    83         SDL_Delay(10);
    83         SDL_Delay(10);
    84     }
    84     }
    85     if (SDL_ThreadID() == mainthread && doterminate) {
    85     if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
    86         SDL_Log("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
    86         SDL_Log("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
    87         raise(SIGTERM);
    87         raise(SIGTERM);
    88     }
    88     }
    89     return (0);
    89     return (0);
    90 }
    90 }
   103         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
   103         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
   104         exit(1);
   104         exit(1);
   105     }
   105     }
   106     atexit(SDL_Quit_Wrapper);
   106     atexit(SDL_Quit_Wrapper);
   107 
   107 
       
   108     SDL_AtomicSet(&doterminate, 0);
       
   109 
   108     if ((mutex = SDL_CreateMutex()) == NULL) {
   110     if ((mutex = SDL_CreateMutex()) == NULL) {
   109         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
   111         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
   110         exit(1);
   112         exit(1);
   111     }
   113     }
   112 
   114