test/testsem.c
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 8149 681eb46b8ac4
equal deleted inserted replaced
7638:783d1cff9b20 7639:9406b7dd2f2d
    28 ThreadFunc(void *data)
    28 ThreadFunc(void *data)
    29 {
    29 {
    30     int threadnum = (int) (uintptr_t) data;
    30     int threadnum = (int) (uintptr_t) data;
    31     while (alive) {
    31     while (alive) {
    32         SDL_SemWait(sem);
    32         SDL_SemWait(sem);
    33         fprintf(stderr,
    33         SDL_Log("Thread number %d has got the semaphore (value = %d)!\n",
    34                 "Thread number %d has got the semaphore (value = %d)!\n",
       
    35                 threadnum, SDL_SemValue(sem));
    34                 threadnum, SDL_SemValue(sem));
    36         SDL_Delay(200);
    35         SDL_Delay(200);
    37         SDL_SemPost(sem);
    36         SDL_SemPost(sem);
    38         fprintf(stderr,
    37         SDL_Log("Thread number %d has released the semaphore (value = %d)!\n",
    39                 "Thread number %d has released the semaphore (value = %d)!\n",
       
    40                 threadnum, SDL_SemValue(sem));
    38                 threadnum, SDL_SemValue(sem));
    41         SDL_Delay(1);           /* For the scheduler */
    39         SDL_Delay(1);           /* For the scheduler */
    42     }
    40     }
    43     printf("Thread number %d exiting.\n", threadnum);
    41     SDL_Log("Thread number %d exiting.\n", threadnum);
    44     return 0;
    42     return 0;
    45 }
    43 }
    46 
    44 
    47 static void
    45 static void
    48 killed(int sig)
    46 killed(int sig)
    57     Uint32 end_ticks;
    55     Uint32 end_ticks;
    58     Uint32 duration;
    56     Uint32 duration;
    59     int retval;
    57     int retval;
    60 
    58 
    61     sem = SDL_CreateSemaphore(0);
    59     sem = SDL_CreateSemaphore(0);
    62     printf("Waiting 2 seconds on semaphore\n");
    60     SDL_Log("Waiting 2 seconds on semaphore\n");
    63 
    61 
    64     start_ticks = SDL_GetTicks();
    62     start_ticks = SDL_GetTicks();
    65     retval = SDL_SemWaitTimeout(sem, 2000);
    63     retval = SDL_SemWaitTimeout(sem, 2000);
    66     end_ticks = SDL_GetTicks();
    64     end_ticks = SDL_GetTicks();
    67 
    65 
    68     duration = end_ticks - start_ticks;
    66     duration = end_ticks - start_ticks;
    69 
    67 
    70     /* Accept a little offset in the effective wait */
    68     /* Accept a little offset in the effective wait */
    71     if (duration > 1900 && duration < 2050)
    69     if (duration > 1900 && duration < 2050)
    72         printf("Wait done.\n");
    70         SDL_Log("Wait done.\n");
    73     else
    71     else
    74         fprintf(stderr, "Wait took %d milliseconds\n", duration);
    72         SDL_Log("Wait took %d milliseconds\n", duration);
    75 
    73 
    76     /* Check to make sure the return value indicates timed out */
    74     /* Check to make sure the return value indicates timed out */
    77     if (retval != SDL_MUTEX_TIMEDOUT)
    75     if (retval != SDL_MUTEX_TIMEDOUT)
    78         fprintf(stderr, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
    76         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
    79 }
    77 }
    80 
    78 
    81 int
    79 int
    82 main(int argc, char **argv)
    80 main(int argc, char **argv)
    83 {
    81 {
    84     SDL_Thread *threads[NUM_THREADS];
    82     SDL_Thread *threads[NUM_THREADS];
    85     uintptr_t i;
    83     uintptr_t i;
    86     int init_sem;
    84     int init_sem;
    87 
    85 
       
    86 	/* Enable standard application logging */
       
    87     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
       
    88 
    88     if (argc < 2) {
    89     if (argc < 2) {
    89         fprintf(stderr, "Usage: %s init_value\n", argv[0]);
    90         SDL_Log("Usage: %s init_value\n", argv[0]);
    90         return (1);
    91         return (1);
    91     }
    92     }
    92 
    93 
    93     /* Load the SDL library */
    94     /* Load the SDL library */
    94     if (SDL_Init(0) < 0) {
    95     if (SDL_Init(0) < 0) {
    95         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    96         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    96         return (1);
    97         return (1);
    97     }
    98     }
    98     signal(SIGTERM, killed);
    99     signal(SIGTERM, killed);
    99     signal(SIGINT, killed);
   100     signal(SIGINT, killed);
   100 
   101 
   101     init_sem = atoi(argv[1]);
   102     init_sem = atoi(argv[1]);
   102     sem = SDL_CreateSemaphore(init_sem);
   103     sem = SDL_CreateSemaphore(init_sem);
   103 
   104 
   104     printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
   105     SDL_Log("Running %d threads, semaphore value = %d\n", NUM_THREADS,
   105            init_sem);
   106            init_sem);
   106     /* Create all the threads */
   107     /* Create all the threads */
   107     for (i = 0; i < NUM_THREADS; ++i) {
   108     for (i = 0; i < NUM_THREADS; ++i) {
   108         char name[64];
   109         char name[64];
   109         SDL_snprintf(name, sizeof (name), "Thread%u", (unsigned int) i);
   110         SDL_snprintf(name, sizeof (name), "Thread%u", (unsigned int) i);
   112 
   113 
   113     /* Wait 10 seconds */
   114     /* Wait 10 seconds */
   114     SDL_Delay(10 * 1000);
   115     SDL_Delay(10 * 1000);
   115 
   116 
   116     /* Wait for all threads to finish */
   117     /* Wait for all threads to finish */
   117     printf("Waiting for threads to finish\n");
   118     SDL_Log("Waiting for threads to finish\n");
   118     alive = 0;
   119     alive = 0;
   119     for (i = 0; i < NUM_THREADS; ++i) {
   120     for (i = 0; i < NUM_THREADS; ++i) {
   120         SDL_WaitThread(threads[i], NULL);
   121         SDL_WaitThread(threads[i], NULL);
   121     }
   122     }
   122     printf("Finished waiting for threads\n");
   123     SDL_Log("Finished waiting for threads\n");
   123 
   124 
   124     SDL_DestroySemaphore(sem);
   125     SDL_DestroySemaphore(sem);
   125 
   126 
   126     TestWaitTimeout();
   127     TestWaitTimeout();
   127 
   128