~ubuntu-branches/debian/stretch/spice/stretch

« back to all changes in this revision

Viewing changes to server/main_dispatcher.c

  • Committer: Package Import Robot
  • Author(s): Liang Guo, Liang Guo, Michael Tokarev
  • Date: 2011-11-29 14:37:08 UTC
  • mfrom: (0.6.1) (0.4.2) (2.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20111129143708-jptkxyjl3a4rds2r
Tags: 0.10.0-1
[ Liang Guo ]
* New upstream release (Closes: #651262)
* Refresh debian/copyright
* Remove fix-typo-in-cmd_line_parser-cpp.patch, applied upstream
* Remove fix-typo-in-record-cpp.patch, applied upstream
* Remove use-requires-private-for-libspice-pkgconfig.patch, applied upstream
* Change Build-Depends on libspice-protocol-dev to (>= 0.9.1~)
* Refresh libspice-server1.symbols
* Update debian/rules clean target
* Ignore common/win/my_getopt-1.5/Makefile change when building package
* debian/control: set DMUA

[ Michael Tokarev ]
* use `rm -f' instead of `-rm' in debian/rules clean targets
* remove python_modules/*.pyc in clean target

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <unistd.h>
 
2
#include <string.h>
 
3
#include <errno.h>
 
4
#include <pthread.h>
 
5
#include <assert.h>
 
6
 
 
7
#include "red_common.h"
 
8
#include "dispatcher.h"
 
9
#include "main_dispatcher.h"
 
10
 
 
11
/*
 
12
 * Main Dispatcher
 
13
 * ===============
 
14
 *
 
15
 * Communication channel between any non main thread and the main thread.
 
16
 *
 
17
 * The main thread is that from which spice_server_init is called.
 
18
 *
 
19
 * Messages are single sized, sent from the non-main thread to the main-thread.
 
20
 * No acknowledge is sent back. This prevents a possible deadlock with the main
 
21
 * thread already waiting on a response for the existing red_dispatcher used
 
22
 * by the worker thread.
 
23
 *
 
24
 * All events have three functions:
 
25
 * main_dispatcher_<event_name> - non static, public function
 
26
 * main_dispatcher_self_<event_name> - handler for main thread
 
27
 * main_dispatcher_handle_<event_name> - handler for callback from main thread
 
28
 *   seperate from self because it may send an ack or do other work in the future.
 
29
 */
 
30
 
 
31
typedef struct {
 
32
    Dispatcher base;
 
33
    SpiceCoreInterface *core;
 
34
} MainDispatcher;
 
35
 
 
36
MainDispatcher main_dispatcher;
 
37
 
 
38
enum {
 
39
    MAIN_DISPATCHER_CHANNEL_EVENT = 0,
 
40
 
 
41
    MAIN_DISPATCHER_NUM_MESSAGES
 
42
};
 
43
 
 
44
typedef struct MainDispatcherChannelEventMessage {
 
45
    int event;
 
46
    SpiceChannelEventInfo *info;
 
47
} MainDispatcherChannelEventMessage;
 
48
 
 
49
/* channel_event - calls core->channel_event, must be done in main thread */
 
50
static void main_dispatcher_self_handle_channel_event(
 
51
                                                int event,
 
52
                                                SpiceChannelEventInfo *info)
 
53
{
 
54
    main_dispatcher.core->channel_event(event, info);
 
55
}
 
56
 
 
57
static void main_dispatcher_handle_channel_event(void *opaque,
 
58
                                                 void *payload)
 
59
{
 
60
    MainDispatcherChannelEventMessage *channel_event = payload;
 
61
 
 
62
    main_dispatcher_self_handle_channel_event(channel_event->event,
 
63
                                              channel_event->info);
 
64
}
 
65
 
 
66
void main_dispatcher_channel_event(int event, SpiceChannelEventInfo *info)
 
67
{
 
68
    MainDispatcherChannelEventMessage msg;
 
69
 
 
70
    if (pthread_self() == main_dispatcher.base.self) {
 
71
        main_dispatcher_self_handle_channel_event(event, info);
 
72
        return;
 
73
    }
 
74
    msg.event = event;
 
75
    msg.info = info;
 
76
    dispatcher_send_message(&main_dispatcher.base, MAIN_DISPATCHER_CHANNEL_EVENT,
 
77
                            &msg);
 
78
}
 
79
 
 
80
static void dispatcher_handle_read(int fd, int event, void *opaque)
 
81
{
 
82
    Dispatcher *dispatcher = opaque;
 
83
 
 
84
    dispatcher_handle_recv_read(dispatcher);
 
85
}
 
86
 
 
87
void main_dispatcher_init(SpiceCoreInterface *core)
 
88
{
 
89
    memset(&main_dispatcher, 0, sizeof(main_dispatcher));
 
90
    main_dispatcher.core = core;
 
91
    dispatcher_init(&main_dispatcher.base, MAIN_DISPATCHER_NUM_MESSAGES, &main_dispatcher.base);
 
92
    core->watch_add(main_dispatcher.base.recv_fd, SPICE_WATCH_EVENT_READ,
 
93
                    dispatcher_handle_read, &main_dispatcher.base);
 
94
    dispatcher_register_handler(&main_dispatcher.base, MAIN_DISPATCHER_CHANNEL_EVENT,
 
95
                                main_dispatcher_handle_channel_event,
 
96
                                sizeof(MainDispatcherChannelEventMessage), 0 /* no ack */);
 
97
}