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(__OS2__)
40 #define INCL_BASE
41 #include <os2.h>
42 #elif defined(_WIN32)
43 #define WIN32_LEAN_AND_MEAN
44 #include <windows.h>
45 #elif defined(_POSIX_PRIORITY_SCHEDULING)
46 #include <sched.h>
47 #endif
48 
49 
50 /* timers */
51 static cairo_time_t timer;
52 static cairo_perf_timer_synchronize_t cairo_perf_timer_synchronize = NULL;
53 static void *cairo_perf_timer_synchronize_closure = NULL;
54 
55 void
cairo_perf_timer_set_synchronize(cairo_perf_timer_synchronize_t synchronize,void * closure)56 cairo_perf_timer_set_synchronize (cairo_perf_timer_synchronize_t  synchronize,
57 				  void				 *closure)
58 {
59     cairo_perf_timer_synchronize = synchronize;
60     cairo_perf_timer_synchronize_closure = closure;
61 }
62 
63 void
cairo_perf_timer_start(void)64 cairo_perf_timer_start (void)
65 {
66     timer = _cairo_time_get ();
67 }
68 
69 void
cairo_perf_timer_stop(void)70 cairo_perf_timer_stop (void)
71 {
72     if (cairo_perf_timer_synchronize)
73 	cairo_perf_timer_synchronize (cairo_perf_timer_synchronize_closure);
74 
75     timer = _cairo_time_get_delta (timer);
76 }
77 
78 cairo_time_t
cairo_perf_timer_elapsed(void)79 cairo_perf_timer_elapsed (void)
80 {
81     return timer;
82 }
83 
84 void
cairo_perf_yield(void)85 cairo_perf_yield (void)
86 {
87     /* try to deactivate this thread until the scheduler calls it again */
88 
89 #if defined(__OS2__)
90     DosSleep (0);
91 #elif defined(_WIN32)
92     SleepEx(0, TRUE);
93 #elif defined(_POSIX_PRIORITY_SCHEDULING)
94     sched_yield ();
95 #else
96     sleep (0);
97 #endif
98 }
99