File: timerstack.c

package info (click to toggle)
jmp 0.48-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,692 kB
  • ctags: 1,732
  • sloc: ansic: 13,985; sh: 8,611; makefile: 526; yacc: 318; java: 18
file content (92 lines) | stat: -rw-r--r-- 2,042 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
#include <stdio.h>
#include <stdlib.h>
#include <timerstack.h>
#include <ui.h>
#include <jmp.h>
#include <jvmpi.h>

static int need_lock = 0;

timerstack* timerstack_new (size_t size) {
    char buf[64];
    timerstack *ts;

    if ((ts = (timerstack *)malloc(sizeof (*ts))) == NULL)
	return NULL;

    if ((ts->times = (methodtime *)malloc(sizeof (*ts->times) * size)) == NULL) {
	timerstack_free (ts);
	return NULL;
    }

    ts->top = 0;
    ts->max = size;
    ts->last_contentation = -1;
    ts->contendtime = 0;
    ts->cpu_time = 0;
    ts->waiting = 0;
    ts->timeout = 0;

    snprintf (buf, 64, "_timerstack %p", ts);
    /* XXX Do we need to strdup this? */
    ts->monitor = jmp_create_monitor (buf);

    return ts;
}


void timerstack_free (timerstack* s) {
    if (s == NULL)
	return;

    free (s->times);
    jmp_delete_monitor (s->monitor);
    free (s);
}

int timerstacks_get_need_locks () {
    return need_lock;
}

/** Set the need to lock flag. */
void timerstacks_set_need_locks (int i) {
    need_lock = i;
}

/** Lock a given timerstack */
void timerstack_lock (timerstack *s) {
    if (need_lock)
	jmp_lock_monitor (s->monitor);
}

/** Unlock a given timerstack */
void timerstack_unlock(timerstack *s) {
    if (need_lock)
	jmp_unlock_monitor (s->monitor);
}

/** Expand the given stack.
 * It should be locked by the caller!
 */
void timerstack_expand (timerstack* s) {
    methodtime* ns;

    if (s == NULL)
	return;

    ns = realloc (s->times, sizeof (*s->times) * s->max * 2);
    if (ns) {
	s->times = ns;
	s->max *= 2;
    } else {
	fprintf (stderr, "timerstack_expand: failed to realloc methodtimes.\n");
    }
}

/* Emacs Local Variables: */
/* Emacs mode:C */
/* Emacs c-indentation-style:"gnu" */
/* Emacs c-hanging-braces-alist:((brace-list-open)(brace-entry-open)(defun-open after)(substatement-open after)(block-close . c-snug-do-while)(extern-lang-open after)) */
/* Emacs c-cleanup-list:(brace-else-brace brace-elseif-brace space-before-funcall) */
/* Emacs c-basic-offset:4 */
/* Emacs End: */