1 #include <stdio.h> 2 #include <string.h> 3 4 #include <OS.h> 5 6 7 static sem_id sSemaphore = -1; 8 9 10 static status_t 11 thread_function1(void* data) 12 { 13 const char* threadName = (const char*)data; 14 printf("%s: going to acquire sem...\n", threadName); 15 status_t error = acquire_sem_etc(sSemaphore, 2, 0, 0); 16 printf("%s: acquire_sem_etc() returned: %s\n", threadName, strerror(error)); 17 return error; 18 } 19 20 21 static status_t 22 thread_function2(void*) 23 { 24 printf("thread2: going to acquire sem...\n"); 25 status_t error = acquire_sem_etc(sSemaphore, 1, 0, 0); 26 printf("thread2: acquire_sem_etc() returned: %s\n", strerror(error)); 27 return error; 28 } 29 30 31 32 int 33 main() 34 { 35 sSemaphore = create_sem(1, "test sem"); 36 37 thread_id thread1 = spawn_thread(&thread_function1, "thread1", 38 B_NORMAL_PRIORITY, (void*)"thread1"); 39 resume_thread(thread1); 40 41 snooze(100000); 42 43 thread_id thread2 = spawn_thread(&thread_function2, "thread2", 44 B_NORMAL_PRIORITY, NULL); 45 resume_thread(thread2); 46 47 snooze(100000); 48 49 thread_id thread3 = spawn_thread(&thread_function1, "thread3", 50 B_NORMAL_PRIORITY, (void*)"thread3"); 51 resume_thread(thread3); 52 53 snooze(100000); 54 55 int32 semCount = 42; 56 get_sem_count(sSemaphore, &semCount); 57 printf("sem count: %ld\n", semCount); 58 59 printf("killing thread1...\n"); 60 kill_thread(thread1); 61 62 snooze(2000000); 63 64 return 0; 65 } 66