benchmarks: Remove unneeded g_thread_exit()
[platform/upstream/gstreamer.git] / tests / benchmarks / gstpollstress.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <gst/gst.h>
24
25 static GstPoll *set;
26 static GList *fds = NULL;
27 static GMutex *fdlock;
28 static GTimer *timer;
29
30 #define MAX_THREADS  100
31
32 static void
33 mess_some_more (void)
34 {
35   GList *walk;
36   gint random;
37   gint removed = 0;
38
39   g_mutex_lock (fdlock);
40
41   for (walk = fds; walk;) {
42     GstPollFD *fd = (GstPollFD *) walk->data;
43
44     walk = g_list_next (walk);
45
46     random = (gint) (10.0 * rand () / (RAND_MAX + 1.0));
47     switch (random) {
48       case 0:
49       {
50         /*
51            GstPollFD *newfd = g_new0 (GstPollFD, 1);
52
53            gst_poll_add_fd (set, newfd);
54            fds = g_list_prepend (fds, newfd);
55          */
56         break;
57       }
58       case 1:
59         if ((gint) (10.0 * rand () / (RAND_MAX + 1.0)) < 2) {
60           gst_poll_remove_fd (set, fd);
61           fds = g_list_remove (fds, fd);
62           g_free (fd);
63           removed++;
64         }
65         break;
66
67       case 2:
68         gst_poll_fd_ctl_write (set, fd, TRUE);
69         break;
70       case 3:
71         gst_poll_fd_ctl_write (set, fd, FALSE);
72         break;
73       case 4:
74         gst_poll_fd_ctl_read (set, fd, TRUE);
75         break;
76       case 5:
77         gst_poll_fd_ctl_read (set, fd, FALSE);
78         break;
79
80       case 6:
81         gst_poll_fd_has_closed (set, fd);
82         break;
83       case 7:
84         gst_poll_fd_has_error (set, fd);
85         break;
86       case 8:
87         gst_poll_fd_can_read (set, fd);
88         break;
89       case 9:
90         gst_poll_fd_can_write (set, fd);
91         break;
92       default:
93         g_assert_not_reached ();
94         break;
95     }
96   }
97   if (g_list_length (fds) < 900) {
98     random = removed + (gint) (2.0 * rand () / (RAND_MAX + 1.0));
99     while (random) {
100       GstPollFD *newfd = g_new0 (GstPollFD, 1);
101
102       gst_poll_add_fd (set, newfd);
103       fds = g_list_prepend (fds, newfd);
104       random--;
105     }
106   }
107
108   g_mutex_unlock (fdlock);
109 }
110
111 static void *
112 run_test (void *threadid)
113 {
114   gint id = GPOINTER_TO_INT (threadid);
115
116   while (TRUE) {
117     if (id == 0) {
118       gint res = gst_poll_wait (set, 10);
119
120       if (res < 0) {
121         g_print ("error %d %s\n", errno, g_strerror (errno));
122       }
123     } else {
124       mess_some_more ();
125       if (g_timer_elapsed (timer, NULL) > 0.5) {
126         g_mutex_lock (fdlock);
127         g_print ("active fds :%d\n", g_list_length (fds));
128         g_timer_start (timer);
129         g_mutex_unlock (fdlock);
130       }
131       g_usleep (1);
132     }
133   }
134
135   return NULL;
136 }
137
138 gint
139 main (gint argc, gchar * argv[])
140 {
141   GThread *threads[MAX_THREADS];
142   gint num_threads;
143   gint t;
144
145   gst_init (&argc, &argv);
146
147   fdlock = g_mutex_new ();
148   timer = g_timer_new ();
149
150   if (argc != 2) {
151     g_print ("usage: %s <num_threads>\n", argv[0]);
152     exit (-1);
153   }
154
155   num_threads = atoi (argv[1]);
156
157   set = gst_poll_new (TRUE);
158
159   for (t = 0; t < num_threads; t++) {
160     GError *error = NULL;
161
162     threads[t] = g_thread_create (run_test, GINT_TO_POINTER (t), TRUE, &error);
163     if (error) {
164       printf ("ERROR: g_thread_create() %s\n", error->message);
165       exit (-1);
166     }
167   }
168   printf ("main(): Created %d threads.\n", t);
169
170   for (t = 0; t < num_threads; t++) {
171     g_thread_join (threads[t]);
172   }
173
174   gst_poll_free (set);
175
176   return 0;
177 }