File: glue-main.c

package info (click to toggle)
fauhdlc 20180504-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,956 kB
  • sloc: cpp: 23,188; ansic: 6,077; yacc: 3,764; lex: 763; makefile: 605; sh: 494; xml: 403
file content (119 lines) | stat: -rw-r--r-- 2,593 bytes parent folder | download | duplicates (3)
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
/* $Id$ 
 * Tiny reimplementation of time_virt() and time_call_at()
 *
 * Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include "glue-main.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

static int failure = 0;
static unsigned long long simulation_time = 0;
static bool terminate = false;

static struct {
	unsigned int n_calls;
	struct {
		void (*func)(void *data);
		void *data;
		unsigned long long tsc;
	} calls[10];
} time_calls;

unsigned long long
fauhdli_time_virt(void)
{
	return simulation_time;
}

void
fauhdli_time_call_at(unsigned long long tsc, void (*func)(void *data), void *data)
{
	assert(time_calls.n_calls < 10);

	time_calls.calls[time_calls.n_calls].func = func;
	time_calls.calls[time_calls.n_calls].data = data;
	time_calls.calls[time_calls.n_calls].tsc = tsc;
	time_calls.n_calls++;
}

int
fauhdli_time_call_delete(void (*func)(void *), void *data)
{
	unsigned int i;
	for (i = 0; i < time_calls.n_calls; i++) {

		if (data == time_calls.calls[i].data) {
			size_t rs = 
				sizeof(time_calls.calls) 
				/ sizeof(time_calls.calls[0])
				- i - 1;

			if (rs == 0) {
				return 0;
			}

			memmove(&time_calls.calls[i], 
				&time_calls.calls[i + 1], 
				rs * sizeof(time_calls.calls[0]));
			return 0;
		}
	}

	return 1;
}

/** enter the main event loop.
 *  May not be called anywhere in the library, as it's not part of
 *  the standard glue-main.
 *  -> only useful in interpreter.c which ships it's own "main"
 */
int
fauhdli_main_event_loop(void)
{
	while (! terminate) {
		unsigned int i;
		int entry = -1;
		unsigned long long min_time = INT64_MAX;

		if (time_calls.n_calls == 0) {
			break;
		}
		
		for (i = 0; i < time_calls.n_calls; i++) {
			if (time_calls.calls[i].tsc <= min_time) {
				min_time = time_calls.calls[i].tsc;
				entry = i;
			}
		}

		assert(entry != -1);
		simulation_time = min_time;
		time_calls.calls[entry].func(time_calls.calls[entry].data);
		
		/* remove entry from queue */
		for (i = entry + 1; i < time_calls.n_calls; i++) {
			time_calls.calls[i - 1].func = 
				time_calls.calls[i].func;
			time_calls.calls[i - 1].data = 
				time_calls.calls[i].data;
			time_calls.calls[i - 1].tsc = time_calls.calls[i].tsc;
		}
		time_calls.n_calls--;
	}

	return failure;
}

void
fauhdli_simulation_quit(int status)
{
	terminate = true;
	failure = status;
}