build: sprintf, sscanf need stdio.h
[platform/upstream/gstreamer.git] / tests / benchmarks / gstclockstress.c
1 /* GStreamer
2  * Copyright (C) <2009> Wim Taymans <wim taymans at gmail dot com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <gst/gst.h>
23
24 #define MAX_THREADS  100
25
26 static gboolean running = TRUE;
27 static gint count = 0;
28
29 static void *
30 run_test (void *user_data)
31 {
32   gint prev;
33   GstClock *sysclock = GST_CLOCK_CAST (user_data);
34
35   while (running) {
36     gst_clock_get_time (sysclock);
37     prev = g_atomic_int_exchange_and_add (&count, 1);
38     if (prev == G_MAXINT)
39       g_warning ("overflow");
40   }
41   g_thread_exit (NULL);
42   return NULL;
43 }
44
45 gint
46 main (gint argc, gchar * argv[])
47 {
48   GThread *threads[MAX_THREADS];
49   gint num_threads;
50   gint t;
51   GstClock *sysclock;
52
53   gst_init (&argc, &argv);
54
55   if (argc != 2) {
56     g_print ("usage: %s <num_threads>\n", argv[0]);
57     exit (-1);
58   }
59
60   num_threads = atoi (argv[1]);
61
62   sysclock = gst_system_clock_obtain ();
63
64   for (t = 0; t < num_threads; t++) {
65     GError *error = NULL;
66
67     threads[t] = g_thread_create (run_test, sysclock, TRUE, &error);
68     if (error) {
69       printf ("ERROR: g_thread_create() %s\n", error->message);
70       exit (-1);
71     }
72   }
73   printf ("main(): Created %d threads.\n", t);
74
75   /* run for 5 seconds */
76   g_usleep (G_USEC_PER_SEC * 5);
77
78   printf ("main(): Stopping threads...\n");
79
80   running = FALSE;
81
82   for (t = 0; t < num_threads; t++) {
83     g_thread_join (threads[t]);
84   }
85
86   g_print ("performed %d get_time operations\n", count);
87
88   gst_object_unref (sysclock);
89
90   return 0;
91 }