btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / tests / system / kernel / port_multi_read_test.cpp
blob2cdc8771bb7057ac49a21b0a46d101f53f9294a3
1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <stdio.h>
8 #include <string.h>
10 #include <OS.h>
13 #define THREAD_COUNT 20
16 status_t
17 read_thread(void* _data)
19 port_id port = (port_id)_data;
21 printf("[%ld] read port...\n", find_thread(NULL));
23 while (true) {
24 ssize_t bytes = port_buffer_size(port);
25 printf("[%ld] buffer size %ld waiting\n", find_thread(NULL), bytes);
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;
36 return B_OK;
40 int
41 main()
43 port_id port = create_port(1, "test port");
44 printf("created port %ld\n", port);
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]);
53 printf("snooze for a bit, all threads should be waiting now.\n");
54 snooze(100000);
56 for (int32 i = 0; i < THREAD_COUNT; i++) {
57 size_t bytes = 20 + i;
58 char buffer[bytes];
59 memset(buffer, 0x55, bytes);
61 printf("send %ld bytes\n", bytes);
62 write_port(port, 0x42, buffer, bytes);
63 snooze(10000);
66 printf("waiting for threads to terminate\n");
67 for (int32 i = 0; i < THREAD_COUNT; i++) {
68 wait_for_thread(threads[i], NULL);
71 return 0;