1
/*
2
 * Copyright (c) 2007 Netlabs
3
 * Copyright (c) 2006 Mozilla Corporation
4
 * Copyright (c) 2006 Red Hat, Inc.
5
 * Copyright (c) 2011 Andrea Canciani
6
 *
7
 * Permission to use, copy, modify, distribute, and sell this software
8
 * and its documentation for any purpose is hereby granted without
9
 * fee, provided that the above copyright notice appear in all copies
10
 * and that both that copyright notice and this permission notice
11
 * appear in supporting documentation, and that the name of
12
 * the authors not be used in advertising or publicity pertaining to
13
 * distribution of the software without specific, written prior
14
 * permission. The authors make no representations about the
15
 * suitability of this software for any purpose.  It is provided "as
16
 * is" without express or implied warranty.
17
 *
18
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
19
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
21
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
23
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25
 *
26
 * Authors: Peter Weilbacher <mozilla@weilbacher.org>
27
 *	    Vladimir Vukicevic <vladimir@pobox.com> (win32/linux code)
28
 *	    Carl Worth <cworth@cworth.org> (win32/linux code)
29
 *          Andrea Canciani <ranma42@gmail.com>
30
 */
31

            
32
#include "cairo-perf.h"
33
#include "../src/cairo-time-private.h"
34

            
35
#if HAVE_UNISTD_H
36
#include <unistd.h>
37
#endif
38

            
39
#if defined(_WIN32)
40
#include <windows.h>
41
#elif defined(_POSIX_PRIORITY_SCHEDULING)
42
#include <sched.h>
43
#endif
44

            
45

            
46
/* timers */
47
static cairo_time_t timer;
48
static cairo_perf_timer_synchronize_t cairo_perf_timer_synchronize = NULL;
49
static void *cairo_perf_timer_synchronize_closure = NULL;
50

            
51
void
52
cairo_perf_timer_set_synchronize (cairo_perf_timer_synchronize_t  synchronize,
53
				  void				 *closure)
54
{
55
    cairo_perf_timer_synchronize = synchronize;
56
    cairo_perf_timer_synchronize_closure = closure;
57
}
58

            
59
void
60
cairo_perf_timer_start (void)
61
{
62
    timer = _cairo_time_get ();
63
}
64

            
65
void
66
cairo_perf_timer_stop (void)
67
{
68
    if (cairo_perf_timer_synchronize)
69
	cairo_perf_timer_synchronize (cairo_perf_timer_synchronize_closure);
70

            
71
    timer = _cairo_time_get_delta (timer);
72
}
73

            
74
cairo_time_t
75
cairo_perf_timer_elapsed (void)
76
{
77
    return timer;
78
}
79

            
80
void
81
cairo_perf_yield (void)
82
{
83
    /* try to deactivate this thread until the scheduler calls it again */
84

            
85
#if defined(_WIN32)
86
    SleepEx(0, TRUE);
87
#elif defined(_POSIX_PRIORITY_SCHEDULING)
88
    sched_yield ();
89
#else
90
    sleep (0);
91
#endif
92
}