test/testlock.c
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 8149 681eb46b8ac4
equal deleted inserted replaced
7638:783d1cff9b20 7639:9406b7dd2f2d
    38 }
    38 }
    39 
    39 
    40 void
    40 void
    41 printid(void)
    41 printid(void)
    42 {
    42 {
    43     printf("Process %lu:  exiting\n", SDL_ThreadID());
    43     SDL_Log("Process %lu:  exiting\n", SDL_ThreadID());
    44 }
    44 }
    45 
    45 
    46 void
    46 void
    47 terminate(int sig)
    47 terminate(int sig)
    48 {
    48 {
    53 void
    53 void
    54 closemutex(int sig)
    54 closemutex(int sig)
    55 {
    55 {
    56     SDL_threadID id = SDL_ThreadID();
    56     SDL_threadID id = SDL_ThreadID();
    57     int i;
    57     int i;
    58     printf("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
    58     SDL_Log("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
    59     doterminate = 1;
    59     doterminate = 1;
    60     for (i = 0; i < 6; ++i)
    60     for (i = 0; i < 6; ++i)
    61         SDL_WaitThread(threads[i], NULL);
    61         SDL_WaitThread(threads[i], NULL);
    62     SDL_DestroyMutex(mutex);
    62     SDL_DestroyMutex(mutex);
    63     exit(sig);
    63     exit(sig);
    67 Run(void *data)
    67 Run(void *data)
    68 {
    68 {
    69     if (SDL_ThreadID() == mainthread)
    69     if (SDL_ThreadID() == mainthread)
    70         signal(SIGTERM, closemutex);
    70         signal(SIGTERM, closemutex);
    71     while (!doterminate) {
    71     while (!doterminate) {
    72         printf("Process %lu ready to work\n", SDL_ThreadID());
    72         SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
    73         if (SDL_LockMutex(mutex) < 0) {
    73         if (SDL_LockMutex(mutex) < 0) {
    74             fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
    74             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
    75             exit(1);
    75             exit(1);
    76         }
    76         }
    77         printf("Process %lu, working!\n", SDL_ThreadID());
    77         SDL_Log("Process %lu, working!\n", SDL_ThreadID());
    78         SDL_Delay(1 * 1000);
    78         SDL_Delay(1 * 1000);
    79         printf("Process %lu, done!\n", SDL_ThreadID());
    79         SDL_Log("Process %lu, done!\n", SDL_ThreadID());
    80         if (SDL_UnlockMutex(mutex) < 0) {
    80         if (SDL_UnlockMutex(mutex) < 0) {
    81             fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
    81             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
    82             exit(1);
    82             exit(1);
    83         }
    83         }
    84         /* If this sleep isn't done, then threads may starve */
    84         /* If this sleep isn't done, then threads may starve */
    85         SDL_Delay(10);
    85         SDL_Delay(10);
    86     }
    86     }
    87     if (SDL_ThreadID() == mainthread && doterminate) {
    87     if (SDL_ThreadID() == mainthread && doterminate) {
    88         printf("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
    88         SDL_Log("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
    89         raise(SIGTERM);
    89         raise(SIGTERM);
    90     }
    90     }
    91     return (0);
    91     return (0);
    92 }
    92 }
    93 
    93 
    95 main(int argc, char *argv[])
    95 main(int argc, char *argv[])
    96 {
    96 {
    97     int i;
    97     int i;
    98     int maxproc = 6;
    98     int maxproc = 6;
    99 
    99 
       
   100 	/* Enable standard application logging */
       
   101     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
       
   102 
   100     /* Load the SDL library */
   103     /* Load the SDL library */
   101     if (SDL_Init(0) < 0) {
   104     if (SDL_Init(0) < 0) {
   102         fprintf(stderr, "%s\n", SDL_GetError());
   105         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
   103         exit(1);
   106         exit(1);
   104     }
   107     }
   105     atexit(SDL_Quit_Wrapper);
   108     atexit(SDL_Quit_Wrapper);
   106 
   109 
   107     if ((mutex = SDL_CreateMutex()) == NULL) {
   110     if ((mutex = SDL_CreateMutex()) == NULL) {
   108         fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
   111         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
   109         exit(1);
   112         exit(1);
   110     }
   113     }
   111 
   114 
   112     mainthread = SDL_ThreadID();
   115     mainthread = SDL_ThreadID();
   113     printf("Main thread: %lu\n", mainthread);
   116     SDL_Log("Main thread: %lu\n", mainthread);
   114     atexit(printid);
   117     atexit(printid);
   115     for (i = 0; i < maxproc; ++i) {
   118     for (i = 0; i < maxproc; ++i) {
   116         char name[64];
   119         char name[64];
   117         SDL_snprintf(name, sizeof (name), "Worker%d", i);
   120         SDL_snprintf(name, sizeof (name), "Worker%d", i);
   118         if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
   121         if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
   119             fprintf(stderr, "Couldn't create thread!\n");
   122             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
   120     }
   123     }
   121     signal(SIGINT, terminate);
   124     signal(SIGINT, terminate);
   122     Run(NULL);
   125     Run(NULL);
   123 
   126 
   124     return (0);                 /* Never reached */
   127     return (0);                 /* Never reached */