xref: /haiku/src/add-ons/kernel/debugger/invalidate_on_exit/invalidate_on_exit.cpp (revision d0f2d8282f3f59a1af7fe2d340d2af0cb36a9b20)
1 /*
2  * Copyright 2008, Michael Lotz, mmlr@mlotz.ch
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <debug.h>
8 
9 #include <signal.h>
10 
11 #include <kscheduler.h>
12 #include <smp.h>
13 
14 
15 static sem_id sRequestSem = -1;
16 
17 
18 static int32
invalidate_loop(void * data)19 invalidate_loop(void *data)
20 {
21 	while (true) {
22 		if (acquire_sem(sRequestSem) != B_OK)
23 			break;
24 
25 		uint32 message[3];
26 		message[0] = sizeof(message);	// size
27 		message[1] = 'KDLE';			// message code
28 		message[2] = 0;					// flags
29 
30 		// where "d:0:baron' stands for desktop x of user y which both
31 		// currently are hardcoded and where '_PTL' is the port link code
32 		write_port(find_port("d:0:baron"), '_PTL', &message, sizeof(message));
33 	}
34 
35 	return 0;
36 }
37 
38 
39 static void
exit_debugger()40 exit_debugger()
41 {
42 	release_sem_etc(sRequestSem, 1, B_DO_NOT_RESCHEDULE);
43 }
44 
45 
46 static status_t
std_ops(int32 op,...)47 std_ops(int32 op, ...)
48 {
49 	if (op == B_MODULE_INIT) {
50 		sRequestSem = create_sem(0, "invalidate_loop_request");
51 		if (sRequestSem < B_OK)
52 			return sRequestSem;
53 
54 		thread_id thread = spawn_kernel_thread(&invalidate_loop,
55 			"invalidate_loop", B_NORMAL_PRIORITY, NULL);
56 		if (thread < B_OK)
57 			return thread;
58 
59 		resume_thread(thread);
60 		return B_OK;
61 	} else if (op == B_MODULE_UNINIT) {
62 		// deleting the sem will also cause the thread to exit
63 		delete_sem(sRequestSem);
64 		sRequestSem = -1;
65 		return B_OK;
66 	}
67 
68 	return B_BAD_VALUE;
69 }
70 
71 
72 static struct debugger_module_info sModuleInfo = {
73 	{
74 		"debugger/invalidate_on_exit/v1",
75 		B_KEEP_LOADED,
76 		&std_ops
77 	},
78 
79 	NULL,
80 	exit_debugger,
81 	NULL,
82 	NULL
83 };
84 
85 module_info *modules[] = {
86 	(module_info *)&sModuleInfo,
87 	NULL
88 };
89