diff options
author | Amit Shah <amit.shah@redhat.com> | 2010-08-23 13:55:16 +0530 |
---|---|---|
committer | Amit Shah <amit.shah@redhat.com> | 2010-08-23 13:55:16 +0530 |
commit | e1f100f1fbe0285c7efdfc30f38bece766e59efe (patch) | |
tree | 6c740d4f2d9c57b74ad27baa3db9abdd5cd85981 /auto-virtserial-guest.c | |
parent | 2c799e07ca9327ef662896c01856f2f6f5c0229d (diff) | |
download | test-virtserial-e1f100f1fbe0285c7efdfc30f38bece766e59efe.tar.gz test-virtserial-e1f100f1fbe0285c7efdfc30f38bece766e59efe.tar.xz test-virtserial-e1f100f1fbe0285c7efdfc30f38bece766e59efe.zip |
auto-test: Introduce a threaded read/write test
Introduce a test that creates a new thread and blocks on a port for
read. The parent thread then writes to the port, and later the host
writes something to the port, unblocking the read.
Signed-off-by: Amit Shah <amit.shah@redhat.com>
Diffstat (limited to 'auto-virtserial-guest.c')
-rw-r--r-- | auto-virtserial-guest.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/auto-virtserial-guest.c b/auto-virtserial-guest.c index eebf1f0..2c84c27 100644 --- a/auto-virtserial-guest.c +++ b/auto-virtserial-guest.c @@ -24,6 +24,7 @@ #include <error.h> #include <fcntl.h> #include <poll.h> +#include <pthread.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -44,6 +45,8 @@ static int g_bigfile_fd; static int g_length; /* The 'name' field in the sysfs port info */ static char g_sysfs_name[1024]; +/* The thread that performs a blocking read operation */ +static pthread_t g_read_thread_id; static ssize_t safewrite(int fd, const void *buf, size_t count) @@ -413,6 +416,50 @@ out: return ret; } +static void *t_blocked_read(void *arg) +{ + int port_nr = *(int *)arg; + int *ret; + + ret = malloc(sizeof(int)); + if (!ret) + return NULL; + + *ret = read_port(port_nr); + + return ret; +} + +static int create_read_thread(int nr) +{ + int ret; + + ret = pthread_create(&g_read_thread_id, NULL, &t_blocked_read, &nr); + if (ret) + return -errno; + + /* Give a chance to the thread to start and block */ + sleep(2); + + return 0; +} + +static int join_read_thread(int nr) +{ + int ret; + void *ret2; + + ret = pthread_join(g_read_thread_id, &ret2); + if (ret) + return ret; + + ret = *(int *)ret2; + + free(ret2); + + return ret; +} + static void send_report(int cfd, int ret) { struct guest_packet gpkt; @@ -539,6 +586,14 @@ back_to_open: ret = send_guest_csum(gpkt.value); send_report(cfd, ret); break; + case KEY_CREATE_READ_THREAD: + ret = create_read_thread(gpkt.value); + send_report(cfd, ret); + break; + case KEY_JOIN_READ_THREAD: + ret = join_read_thread(gpkt.value); + send_report(cfd, ret); + break; } } return 0; |