1 #include <stdio.h> 2 #include <signal.h> 3 #include <unistd.h> 4 #include <Application.h> 5 6 static thread_id gBAppThreadID; 7 static thread_id gMainThreadID; 8 static int bapp_ref_count = 0; 9 10 static int32 11 bapp_quit_thread(void *arg) 12 { 13 printf("%s: calling Lock()\n", __FUNCTION__); 14 be_app->Lock(); 15 printf("%s: calling Quit()\n", __FUNCTION__); 16 be_app->Quit(); 17 return 0; 18 } 19 20 21 static void 22 sighandler(int sig) 23 { 24 resume_thread(spawn_thread(bapp_quit_thread, "BApplication->Quit()", B_NORMAL_PRIORITY, NULL)); 25 } 26 27 28 static int32 29 bapp_thread(void *arg) 30 { 31 signal(SIGINT, sighandler); /* on ^C exit the bapp_thread too */ 32 printf("%s: calling Lock()\n", __FUNCTION__); 33 be_app->Lock(); 34 printf("%s: calling Run()\n", __FUNCTION__); 35 be_app->Run(); 36 printf("%s: exitting...\n", __FUNCTION__); 37 return 0; 38 } 39 40 41 void 42 beos_launch_bapplication(void) 43 { 44 bapp_ref_count++; 45 if (be_app) 46 return; /* done already! */ 47 new BApplication("application/x-vnd.mmu_man-threaded-BApp-test"); 48 gBAppThreadID = spawn_thread(bapp_thread, "BApplication(test)", B_NORMAL_PRIORITY, (void *)find_thread(NULL)); 49 if (gBAppThreadID < B_OK) 50 return; /* #### handle errors */ 51 if (resume_thread(gBAppThreadID) < B_OK) 52 return; 53 // This way we ensure we don't create BWindows before be_app is created 54 // (creating it in the thread doesn't ensure this) 55 printf("%s: calling Unlock()\n", __FUNCTION__); 56 be_app->Unlock(); 57 } 58 59 60 void beos_uninit_bapplication(void) 61 { 62 status_t err; 63 if (--bapp_ref_count) 64 return; 65 if (!be_app->Lock()) 66 debugger("cannot lock BApp!"); 67 printf("%s: calling Quit()\n", __FUNCTION__); 68 be_app->Quit(); 69 printf("%s: waiting for app thread\n", __FUNCTION__); 70 wait_for_thread(gBAppThreadID, &err); 71 delete be_app; 72 be_app = NULL; 73 } 74 75 76 int main() 77 { 78 printf("%s: calling beos_launch_bapplication()\n", __FUNCTION__); 79 beos_launch_bapplication(); 80 sleep(10); 81 printf("%s: calling beos_uninit_bapplication()\n", __FUNCTION__); 82 beos_uninit_bapplication(); 83 } 84