aboutsummaryrefslogtreecommitdiffstats
path: root/distrib/sdl-1.2.15/test/testsem.c
diff options
context:
space:
mode:
authorJesse Hall <jessehall@google.com>2012-07-09 11:27:07 -0700
committerJesse Hall <jessehall@google.com>2012-07-22 00:35:08 -0700
commit9682c8870b8ff5e4ac2e4c70b759f791c6f38c1f (patch)
treeded6ee18c4e1f33df235e53615a6d65e2d64f4ef /distrib/sdl-1.2.15/test/testsem.c
parent74b55003f76dbca96e4a26d98fe464081ca5341f (diff)
downloadexternal_qemu-9682c8870b8ff5e4ac2e4c70b759f791c6f38c1f.zip
external_qemu-9682c8870b8ff5e4ac2e4c70b759f791c6f38c1f.tar.gz
external_qemu-9682c8870b8ff5e4ac2e4c70b759f791c6f38c1f.tar.bz2
Import SDL release-1.2.15
Change-Id: I505c4aea24325cad475f217db5589814b4c75dbf
Diffstat (limited to 'distrib/sdl-1.2.15/test/testsem.c')
-rw-r--r--distrib/sdl-1.2.15/test/testsem.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/distrib/sdl-1.2.15/test/testsem.c b/distrib/sdl-1.2.15/test/testsem.c
new file mode 100644
index 0000000..c906432
--- /dev/null
+++ b/distrib/sdl-1.2.15/test/testsem.c
@@ -0,0 +1,103 @@
+
+/* Simple test of the SDL semaphore code */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+
+#include "SDL.h"
+#include "SDL_thread.h"
+
+#define NUM_THREADS 10
+
+static SDL_sem *sem;
+int alive = 1;
+
+int SDLCALL ThreadFunc(void *data)
+{
+ int threadnum = (int)(uintptr_t)data;
+ while ( alive ) {
+ SDL_SemWait(sem);
+ fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
+ SDL_Delay(200);
+ SDL_SemPost(sem);
+ fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
+ SDL_Delay(1); /* For the scheduler */
+ }
+ printf("Thread number %d exiting.\n", threadnum);
+ return 0;
+}
+
+static void killed(int sig)
+{
+ alive = 0;
+}
+
+static void TestWaitTimeout(void)
+{
+ Uint32 start_ticks;
+ Uint32 end_ticks;
+ Uint32 duration;
+
+ sem = SDL_CreateSemaphore(0);
+ printf("Waiting 2 seconds on semaphore\n");
+
+ start_ticks = SDL_GetTicks();
+ SDL_SemWaitTimeout(sem, 2000);
+ end_ticks = SDL_GetTicks();
+
+ duration = end_ticks - start_ticks;
+
+ /* Accept a little offset in the effective wait */
+ if (duration > 1900 && duration < 2050)
+ printf("Wait done.\n");
+ else
+ fprintf(stderr, "Wait took %d milliseconds\n", duration);
+}
+
+int main(int argc, char **argv)
+{
+ SDL_Thread *threads[NUM_THREADS];
+ uintptr_t i;
+ int init_sem;
+
+ if(argc < 2) {
+ fprintf(stderr,"Usage: %s init_value\n", argv[0]);
+ return(1);
+ }
+
+ /* Load the SDL library */
+ if ( SDL_Init(0) < 0 ) {
+ fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
+ return(1);
+ }
+ signal(SIGTERM, killed);
+ signal(SIGINT, killed);
+
+ init_sem = atoi(argv[1]);
+ sem = SDL_CreateSemaphore(init_sem);
+
+ printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
+ /* Create all the threads */
+ for( i = 0; i < NUM_THREADS; ++i ) {
+ threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
+ }
+
+ /* Wait 10 seconds */
+ SDL_Delay(10 * 1000);
+
+ /* Wait for all threads to finish */
+ printf("Waiting for threads to finish\n");
+ alive = 0;
+ for( i = 0; i < NUM_THREADS; ++i ) {
+ SDL_WaitThread(threads[i], NULL);
+ }
+ printf("Finished waiting for threads\n");
+
+ SDL_DestroySemaphore(sem);
+
+ TestWaitTimeout();
+
+ SDL_Quit();
+ return(0);
+}