File: picture_queue.c

package info (click to toggle)
termdebug 2.2+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 456 kB
  • ctags: 685
  • sloc: ansic: 5,008; sh: 560; makefile: 48; lex: 46
file content (109 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download
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
/* Copyright (C) 2013 G.P. Halkes
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3, as
   published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>

#include "common.h"
#include "window.h"

extern const char *picture_series;

typedef struct picture_queue_t picture_queue_t;
struct picture_queue_t {
	window_t *winA, *winB;
	char *name;
	picture_queue_t *next;
};

static picture_queue_t *picture_queue_head, *picture_queue_tail;
static pthread_t picture_thread;
static pthread_mutex_t picture_queue_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t picture_queue_cond = PTHREAD_COND_INITIALIZER;
static td_bool finished;

void enqueue_pictures(window_t *winA, window_t *winB, const char *name) {
	picture_queue_t *item = malloc(sizeof(picture_queue_t));

	if (item == NULL)
		return;

	item->winA = win_copy(winA);
	item->winB = win_copy(winB);
	item->name = port_strdup(name);
	item->next = NULL;

	if (item->winA == NULL || item->winB == NULL || item->name == NULL) {
		win_del(item->winA);
		win_del(item->winB);
		free(item->name);
		free(item);
		return;
	}

	pthread_mutex_lock(&picture_queue_lock);
	if (picture_queue_head == NULL) {
		picture_queue_head = picture_queue_tail = item;
	} else {
		picture_queue_tail->next = item;
		picture_queue_tail = item;
	}
	pthread_cond_signal(&picture_queue_cond);
	pthread_mutex_unlock(&picture_queue_lock);
}

static void *picture_writer_thead(void *arg) {
	picture_queue_t *tmp;

	(void) arg;

	pthread_mutex_lock(&picture_queue_lock);
	pthread_cond_signal(&picture_queue_cond);
	do {
		if (picture_queue_head == NULL)
			pthread_cond_wait(&picture_queue_cond, &picture_queue_lock);

		tmp = picture_queue_head;
		if (picture_queue_head != NULL)
			picture_queue_head = picture_queue_head->next;
		pthread_mutex_unlock(&picture_queue_lock);

		if (tmp != NULL) {
			win_write_image(tmp->winA, tmp->winB, tmp->name);
			win_del(tmp->winA);
			win_del(tmp->winB);
			free(tmp);
		}

		pthread_mutex_lock(&picture_queue_lock);
	} while (!finished || picture_queue_head != NULL);
	pthread_mutex_unlock(&picture_queue_lock);
	return NULL;
}

static void join_picture_thread(void) {
	void *retval;

	pthread_mutex_lock(&picture_queue_lock);
	finished = td_true;
	pthread_cond_signal(&picture_queue_cond);
	pthread_mutex_unlock(&picture_queue_lock);
	pthread_join(picture_thread, &retval);
}

void start_picture_writer_thread(void) {
	pthread_mutex_lock(&picture_queue_lock);
	pthread_create(&picture_thread, NULL, picture_writer_thead, NULL);
	pthread_cond_wait(&picture_queue_cond, &picture_queue_lock);
	pthread_mutex_unlock(&picture_queue_lock);
	atexit(join_picture_thread);
}