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