]>
code.delx.au - pulseaudio/blob - src/tests/thread-test.c
4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulsecore/thread.h>
27 #include <pulsecore/mutex.h>
28 #include <pulsecore/log.h>
29 #include <pulsecore/core-util.h>
30 #include <pulse/xmalloc.h>
32 static pa_mutex
*mutex
= NULL
;
33 static pa_cond
*cond1
= NULL
, *cond2
= NULL
;
34 static pa_tls
*tls
= NULL
;
36 static int magic_number
= 0;
38 #define THREADS_MAX 20
40 static void thread_func(void *data
) {
41 pa_tls_set(tls
, data
);
43 pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls
));
50 pa_log("%s waiting ...", (char*) pa_tls_get(tls
));
57 if (magic_number
!= 0)
60 pa_cond_wait(cond1
, mutex
);
66 pa_mutex_unlock(mutex
);
68 pa_cond_signal(cond2
, 0);
70 pa_log("%s got number %i", (char*) pa_tls_get(tls
), k
);
73 for (n
= 0; n
< k
; n
++)
81 pa_mutex_unlock(mutex
);
83 pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls
));
86 int main(int argc
, char *argv
[]) {
88 pa_thread
* t
[THREADS_MAX
];
90 assert(pa_thread_is_running(pa_thread_self()));
92 mutex
= pa_mutex_new(0);
93 cond1
= pa_cond_new();
94 cond2
= pa_cond_new();
95 tls
= pa_tls_new(pa_xfree
);
97 for (i
= 0; i
< THREADS_MAX
; i
++) {
98 t
[i
] = pa_thread_new(thread_func
, pa_sprintf_malloc("Thread #%i", i
+1));
102 pa_mutex_lock(mutex
);
106 for (k
= 0; k
< 100; k
++) {
107 assert(magic_number
== 0);
110 magic_number
= (int) rand() % 0x10000;
112 pa_log("iteration %i (%i)", k
, magic_number
);
114 pa_cond_signal(cond1
, 0);
116 pa_cond_wait(cond2
, mutex
);
122 pa_cond_signal(cond1
, 1);
124 pa_mutex_unlock(mutex
);
126 for (i
= 0; i
< THREADS_MAX
; i
++)
127 pa_thread_free(t
[i
]);
129 pa_mutex_free(mutex
);