summaryrefslogtreecommitdiffstats
path: root/server/main_dispatcher.c
blob: 73856bfefd0966670a97ead0c4b02a7860d88263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>

#include "red_common.h"
#include "main_dispatcher.h"

/*
 * Main Dispatcher
 * ===============
 *
 * Communication channel between any non main thread and the main thread.
 *
 * The main thread is that from which spice_server_init is called.
 *
 * Messages are single sized, sent from the non-main thread to the main-thread.
 * No acknowledge is sent back. This prevents a possible deadlock with the main
 * thread already waiting on a response for the existing red_dispatcher used
 * by the worker thread.
 *
 * All events have three functions:
 * main_dispatcher_<event_name> - non static, public function
 * main_dispatcher_self_<event_name> - handler for main thread
 * main_dispatcher_handle_<event_name> - handler for callback from main thread
 *   seperate from self because it may send an ack or do other work in the future.
 */

typedef struct {
    SpiceCoreInterface *core;
    int main_fd;
    int other_fd;
    pthread_t self;
    pthread_mutex_t lock;
} MainDispatcher;

MainDispatcher main_dispatcher;

enum {
    MAIN_DISPATCHER_CHANNEL_EVENT = 0,

    MAIN_DISPATCHER_NUM_MESSAGES
};

typedef struct MainDispatcherMessage {
    uint32_t type;
    union {
        struct {
            int event;
            SpiceChannelEventInfo *info;
        } channel_event;
    } data;
} MainDispatcherMessage;

/* channel_event - calls core->channel_event, must be done in main thread */
static void main_dispatcher_self_handle_channel_event(
                                                int event,
                                                SpiceChannelEventInfo *info)
{
    main_dispatcher.core->channel_event(event, info);
}

static void main_dispatcher_handle_channel_event(MainDispatcherMessage *msg)
{
    main_dispatcher_self_handle_channel_event(msg->data.channel_event.event,
                                              msg->data.channel_event.info);
}

void main_dispatcher_channel_event(int event, SpiceChannelEventInfo *info)
{
    MainDispatcherMessage msg;
    ssize_t written = 0;
    ssize_t ret;

    if (pthread_self() == main_dispatcher.self) {
        main_dispatcher_self_handle_channel_event(event, info);
        return;
    }
    msg.type = MAIN_DISPATCHER_CHANNEL_EVENT;
    msg.data.channel_event.event = event;
    msg.data.channel_event.info = info;
    pthread_mutex_lock(&main_dispatcher.lock);
    while (written < sizeof(msg)) {
        ret = write(main_dispatcher.other_fd, &msg + written,
                    sizeof(msg) - written);
        if (ret == -1) {
            assert(errno == EINTR);
            continue;
        }
        written += ret;
    }
    pthread_mutex_unlock(&main_dispatcher.lock);
}


static void main_dispatcher_handle_read(int fd, int event, void *opaque)
{
    int ret;
    MainDispatcher *md = opaque;
    MainDispatcherMessage msg;
    int read_size = 0;

    while (read_size < sizeof(msg)) {
        /* blocks until sizeof(msg) is read */
        ret = read(md->main_fd, &msg + read_size, sizeof(msg) - read_size);
        if (ret == -1) {
            if (errno != EINTR) {
                red_printf("error reading from main dispatcher: %d\n", errno);
                /* TODO: close channel? */
                return;
            }
            continue;
        }
        read_size += ret;
    }
    switch (msg.type) {
        case MAIN_DISPATCHER_CHANNEL_EVENT:
            main_dispatcher_handle_channel_event(&msg);
            break;
        default:
            red_printf("error: unhandled main dispatcher message type %d\n",
                       msg.type);
    }
}

void main_dispatcher_init(SpiceCoreInterface *core)
{
    int channels[2];

    memset(&main_dispatcher, 0, sizeof(main_dispatcher));
    main_dispatcher.core = core;

    if (socketpair(AF_LOCAL, SOCK_STREAM, 0, channels) == -1) {
        red_error("socketpair failed %s", strerror(errno));
        return;
    }
    pthread_mutex_init(&main_dispatcher.lock, NULL);
    main_dispatcher.main_fd = channels[0];
    main_dispatcher.other_fd = channels[1];
    main_dispatcher.self = pthread_self();

    core->watch_add(main_dispatcher.main_fd, SPICE_WATCH_EVENT_READ,
                    main_dispatcher_handle_read, &main_dispatcher);
}