[Spice-devel] [PATCH spice-server 6/8] record: Allows to use recording function for multiple purposes
Jonathon Jongsma
jjongsma at redhat.com
Wed Nov 9 22:29:52 UTC 2016
On Fri, 2016-11-04 at 13:16 +0000, Frediano Ziglio wrote:
> Allocate recording file globally from reds.c.
I think this line makes more sense as the commit summary
Acked-by: Jonathon Jongsma <jjongsma at redhat.com>
> This will allow to register multiple screen VM or recording
> additional stuff like sound.
> The mutex code added to red-record-qxl.c is required to avoid
> mixing writing from multiple threads.
>
> Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> ---
> server/red-record-qxl.c | 26 ++++++++++++++++++++++----
> server/red-record-qxl.h | 3 ++-
> server/red-worker.c | 6 +-----
> server/reds-private.h | 2 ++
> server/reds.c | 17 +++++++++++++++++
> server/reds.h | 5 +++++
> 6 files changed, 49 insertions(+), 10 deletions(-)
>
> diff --git a/server/red-record-qxl.c b/server/red-record-qxl.c
> index bd8869e..ad4b709 100644
> --- a/server/red-record-qxl.c
> +++ b/server/red-record-qxl.c
> @@ -31,7 +31,9 @@
>
> struct RedRecord {
> FILE *fd;
> + pthread_mutex_t lock;
> unsigned int counter;
> + gint refs;
> };
>
> #if 0
> @@ -795,12 +797,14 @@ void
> red_record_primary_surface_create(RedRecord *record,
> {
> FILE *fd = record->fd;
>
> + pthread_mutex_lock(&record->lock);
> fprintf(fd, "%d %d %d %d\n", surface->width, surface->height,
> surface->stride, surface->format);
> fprintf(fd, "%d %d %d %d\n", surface->position, surface-
> >mouse_mode,
> surface->flags, surface->type);
> write_binary(fd, "data", line_0 ? abs(surface->stride)*surface-
> >height : 0,
> line_0);
> + pthread_mutex_unlock(&record->lock);
> }
>
> static void red_record_event_start(RedRecord *record, int what,
> uint32_t type)
> @@ -816,13 +820,16 @@ static void red_record_event_start(RedRecord
> *record, int what, uint32_t type)
>
> void red_record_event(RedRecord *record, int what, uint32_t type)
> {
> + pthread_mutex_lock(&record->lock);
> red_record_event_start(record, what, type);
> + pthread_mutex_unlock(&record->lock);
> }
> void red_record_qxl_command(RedRecord *record, RedMemSlotInfo
> *slots,
> QXLCommandExt ext_cmd)
> {
> FILE *fd = record->fd;
>
> + pthread_mutex_lock(&record->lock);
> red_record_event_start(record, 0, ext_cmd.cmd.type);
>
> switch (ext_cmd.cmd.type) {
> @@ -842,6 +849,7 @@ void red_record_qxl_command(RedRecord *record,
> RedMemSlotInfo *slots,
> red_record_cursor_cmd(fd, slots, ext_cmd.group_id,
> ext_cmd.cmd.data);
> break;
> }
> + pthread_mutex_unlock(&record->lock);
> }
>
> /**
> @@ -904,15 +912,25 @@ RedRecord *red_record_new(const char *filename)
> }
>
> record = g_new(RedRecord, 1);
> + record->refs = 1;
> record->fd = f;
> record->counter = 0;
> + pthread_mutex_init(&record->lock, NULL);
> return record;
> }
>
> -void red_record_free(RedRecord *record)
> +RedRecord *red_record_ref(RedRecord *record)
> {
> - if (record) {
> - fclose(record->fd);
> - g_free(record);
> + g_atomic_int_inc(&record->refs);
> + return record;
> +}
> +
> +void red_record_unref(RedRecord *record)
> +{
> + if (!record || !g_atomic_int_dec_and_test(&record->refs)) {
> + return;
> }
> + fclose(record->fd);
> + pthread_mutex_destroy(&record->lock);
> + g_free(record);
> }
> diff --git a/server/red-record-qxl.h b/server/red-record-qxl.h
> index 0685393..293e24a 100644
> --- a/server/red-record-qxl.h
> +++ b/server/red-record-qxl.h
> @@ -33,7 +33,8 @@ typedef struct RedRecord RedRecord;
> */
> RedRecord* red_record_new(const char *filename);
>
> -void red_record_free(RedRecord *record);
> +RedRecord *red_record_ref(RedRecord *record);
> +void red_record_unref(RedRecord *record);
>
> void red_record_primary_surface_create(RedRecord *record,
> QXLDevSurfaceCreate *surface,
> diff --git a/server/red-worker.c b/server/red-worker.c
> index e1765c1..322c2b3 100644
> --- a/server/red-worker.c
> +++ b/server/red-worker.c
> @@ -1306,7 +1306,6 @@ RedWorker* red_worker_new(QXLInstance *qxl,
> QXLDevInitInfo init_info;
> RedWorker *worker;
> Dispatcher *dispatcher;
> - const char *record_filename;
> RedsState *reds = red_qxl_get_server(qxl->st);
> RedChannel *channel;
>
> @@ -1316,10 +1315,7 @@ RedWorker* red_worker_new(QXLInstance *qxl,
> worker->core = event_loop_core;
> worker->core.main_context = g_main_context_new();
>
> - record_filename = getenv("SPICE_WORKER_RECORD_FILENAME");
> - if (record_filename) {
> - worker->record = red_record_new(record_filename);
> - }
> + worker->record = reds_get_record(reds);
> dispatcher = red_qxl_get_dispatcher(qxl);
> dispatcher_set_opaque(dispatcher, worker);
>
> diff --git a/server/reds-private.h b/server/reds-private.h
> index ce78945..85ad279 100644
> --- a/server/reds-private.h
> +++ b/server/reds-private.h
> @@ -24,6 +24,7 @@
> #include "main-dispatcher.h"
> #include "main-channel.h"
> #include "inputs-channel.h"
> +#include "red-record-qxl.h"
>
> #define MIGRATE_TIMEOUT (MSEC_PER_SEC * 10)
> #define MM_TIME_DELTA 400 /*ms*/
> @@ -143,6 +144,7 @@ struct RedsState {
> SpiceCoreInterfaceInternal *core;
> GList *qxl_instances;
> MainDispatcher *main_dispatcher;
> + RedRecord *record;
> };
>
> #define FOREACH_QXL_INSTANCE(_reds, _iter, _qxl) \
> diff --git a/server/reds.c b/server/reds.c
> index 30b9165..d14ccb2 100644
> --- a/server/reds.c
> +++ b/server/reds.c
> @@ -4517,3 +4517,20 @@ static RedCharDeviceVDIPort
> *red_char_device_vdi_port_new(RedsState *reds)
> "opaque", reds,
> NULL);
> }
> +
> +RedRecord *reds_get_record(RedsState *reds)
> +{
> + if (!reds->record) {
> + const char *record_filename;
> + record_filename = getenv("SPICE_WORKER_RECORD_FILENAME");
> + if (record_filename) {
> + reds->record = red_record_new(record_filename);
> + }
> + }
> +
> + if (reds->record) {
> + return red_record_ref(reds->record);
> + }
> +
> + return NULL;
> +}
> diff --git a/server/reds.h b/server/reds.h
> index 28e3444..2748102 100644
> --- a/server/reds.h
> +++ b/server/reds.h
> @@ -101,6 +101,11 @@ SpiceCoreInterfaceInternal*
> reds_get_core_interface(RedsState *reds);
> void reds_update_client_mouse_allowed(RedsState *reds);
> MainDispatcher* reds_get_main_dispatcher(RedsState *reds);
>
> +/* Get the recording object stored in RedsState.
> + * You should free with red_record_unref.
> + */
> +struct RedRecord *reds_get_record(RedsState *reds);
> +
> /* fd watches/timers */
> SpiceWatch *reds_core_watch_add(RedsState *reds,
> int fd, int event_mask,
More information about the Spice-devel
mailing list