tee: Check for the removed pad flag also in the slow pushing path
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <gst/gst.h>
24 #include "gst/glib-compat-private.h"
25
26 static GstPoll *set;
27 static GList *fds = NULL;
28 static GMutex fdlock;
29 static GTimer *timer;
30
31 #define MAX_THREADS  100
32
33 static void
34 mess_some_more (void)
35 {
36   GList *walk;
37   gint random;
38   gint removed = 0;
39
40   g_mutex_lock (&fdlock);
41
42   for (walk = fds; walk;) {
43     GstPollFD *fd = (GstPollFD *) walk->data;
44
45     walk = g_list_next (walk);
46
47     random = (gint) (10.0 * rand () / (RAND_MAX + 1.0));
48     switch (random) {
49       case 0:
50       {
51         /*
52            GstPollFD *newfd = g_new0 (GstPollFD, 1);
53
54            gst_poll_add_fd (set, newfd);
55            fds = g_list_prepend (fds, newfd);
56          */
57         break;
58       }
59       case 1:
60         if ((gint) (10.0 * rand () / (RAND_MAX + 1.0)) < 2) {
61           gst_poll_remove_fd (set, fd);
62           fds = g_list_remove (fds, fd);
63           g_free (fd);
64           removed++;
65         }
66         break;
67
68       case 2:
69         gst_poll_fd_ctl_write (set, fd, TRUE);
70         break;
71       case 3:
72         gst_poll_fd_ctl_write (set, fd, FALSE);
73         break;
74       case 4:
75         gst_poll_fd_ctl_read (set, fd, TRUE);
76         break;
77       case 5:
78         gst_poll_fd_ctl_read (set, fd, FALSE);
79         break;
80
81       case 6:
82         gst_poll_fd_has_closed (set, fd);
83         break;
84       case 7:
85         gst_poll_fd_has_error (set, fd);
86         break;
87       case 8:
88         gst_poll_fd_can_read (set, fd);
89         break;
90       case 9:
91         gst_poll_fd_can_write (set, fd);
92         break;
93       default:
94         g_assert_not_reached ();
95         break;
96     }
97   }
98   if (g_list_length (fds) < 900) {
99     random = removed + (gint) (2.0 * rand () / (RAND_MAX + 1.0));
100     while (random) {
101       GstPollFD *newfd = g_new0 (GstPollFD, 1);
102
103       gst_poll_add_fd (set, newfd);
104       fds = g_list_prepend (fds, newfd);
105       random--;
106     }
107   }
108
109   g_mutex_unlock (&fdlock);
110 }
111
112 static void *
113 run_test (void *threadid)
114 {
115   gint id = GPOINTER_TO_INT (threadid);
116
117   while (TRUE) {
118     if (id == 0) {
119       gint res = gst_poll_wait (set, 10);
120
121       if (res < 0) {
122         g_print ("error %d %s\n", errno, g_strerror (errno));
123       }
124     } else {
125       mess_some_more ();
126       if (g_timer_elapsed (timer, NULL) > 0.5) {
127         g_mutex_lock (&fdlock);
128         g_print ("active fds :%u\n", g_list_length (fds));
129         g_timer_start (timer);
130         g_mutex_unlock (&fdlock);
131       }
132       g_usleep (1);
133     }
134   }
135
136   return NULL;
137 }
138
139 gint
140 main (gint argc, gchar * argv[])
141 {
142   GThread *threads[MAX_THREADS];
143   gint num_threads;
144   gint t;
145
146   gst_init (&argc, &argv);
147
148   g_mutex_init (&fdlock);
149   timer = g_timer_new ();
150
151   if (argc != 2) {
152     g_print ("usage: %s <num_threads>\n", argv[0]);
153     exit (-1);
154   }
155
156   num_threads = atoi (argv[1]);
157
158   set = gst_poll_new (TRUE);
159
160   for (t = 0; t < num_threads; t++) {
161     GError *error = NULL;
162
163     threads[t] = g_thread_try_new ("pollstresstest", run_test,
164         GINT_TO_POINTER (t), &error);
165
166     if (error) {
167       printf ("ERROR: g_thread_try_new() %s\n", error->message);
168       g_clear_error (&error);
169       exit (-1);
170     }
171   }
172   printf ("main(): Created %d threads.\n", t);
173
174   for (t = 0; t < num_threads; t++) {
175     g_thread_join (threads[t]);
176   }
177
178   gst_poll_free (set);
179
180   return 0;
181 }