xref: /haiku/src/tests/system/kernel/port_multi_read_test.cpp (revision 7d592ec4d20e77818a090c3a0c43b6f3c4a6cd27)
1 /*
2  * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include <OS.h>
11 
12 
13 #define THREAD_COUNT	20
14 
15 
16 status_t
read_thread(void * _data)17 read_thread(void* _data)
18 {
19 	port_id port = (port_id)_data;
20 
21 	printf("[%ld] read port...\n", find_thread(NULL));
22 
23 	while (true) {
24 		ssize_t bytes = port_buffer_size(port);
25 		printf("[%ld] buffer size %ld waiting\n", find_thread(NULL), bytes);
26 
27 		char buffer[256];
28 		int32 code;
29 		bytes = read_port(port, &code, buffer, sizeof(buffer));
30 		printf("[%ld] read port result (code %lx): %s\n", find_thread(NULL),
31 			code, strerror(bytes));
32 		if (bytes >= 0)
33 			break;
34 	}
35 
36 	return B_OK;
37 }
38 
39 
40 int
main()41 main()
42 {
43 	port_id port = create_port(1, "test port");
44 	printf("created port %ld\n", port);
45 
46 	thread_id threads[THREAD_COUNT];
47 	for (int32 i = 0; i < THREAD_COUNT; i++) {
48 		threads[i] = spawn_thread(read_thread, "read thread", B_NORMAL_PRIORITY,
49 			(void*)port);
50 		resume_thread(threads[i]);
51 	}
52 
53 	printf("snooze for a bit, all threads should be waiting now.\n");
54 	snooze(100000);
55 
56 	for (int32 i = 0; i < THREAD_COUNT; i++) {
57 		size_t bytes = 20 + i;
58 		char buffer[bytes];
59 		memset(buffer, 0x55, bytes);
60 
61 		printf("send %ld bytes\n", bytes);
62 		write_port(port, 0x42, buffer, bytes);
63 		snooze(10000);
64 	}
65 
66 	printf("waiting for threads to terminate\n");
67 	for (int32 i = 0; i < THREAD_COUNT; i++) {
68 		wait_for_thread(threads[i], NULL);
69 	}
70 
71 	return 0;
72 }
73