1 /* Test the pseudothreads.
2 * Copyright (C) 2001 Richard W.M. Jones <rich@annexia.org>
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_pseudothread.c,v 1.5 2002/12/01 14:29:31 rich Exp $
37 #include "pthr_pseudothread.h"
39 static void set_flag (void *data) { int *flag = (int *) data; *flag = 1; }
41 static pool test_pool;
42 static pseudothread test_pth;
45 static pseudothread pth1;
46 static int pool_gone = 0;
47 static int thread_has_run = 0;
50 test_exiting (void *data)
52 assert (current_pth == pth1);
59 test_timeout (void *data)
61 assert (current_pth == pth1);
70 /* Check current_pth set correctly on thread start. */
71 assert (current_pth == test_pth);
73 /* Launch a thread and check that the thread runs and the pool is
77 pool_register_cleanup_fn (pool1, set_flag, &pool_gone);
78 pth1 = new_pseudothread (pool1, set_flag, &thread_has_run, "pth1");
79 assert (pool_gone == 0);
80 assert (thread_has_run == 0);
81 pth_start (pth1); /* The thread actually runs and exits here. */
82 assert (pool_gone == 1);
83 assert (thread_has_run == 1);
84 assert (current_pth == test_pth);
86 /* Check pth_get_* functions. */
87 assert (pth_get_pool (test_pth) == test_pool);
88 assert (strcmp (pth_get_name (test_pth), "testing thread") == 0);
89 assert (pth_get_thread_num (test_pth) == 0);
90 assert (pth_get_run (test_pth) == do_test);
91 assert (pth_get_data (test_pth) == 0);
92 assert (pth_get_language (test_pth) == 0);
94 /* Check pth_exit function. */
96 pth1 = new_pseudothread (pool1, test_exiting, 0, "exiting thread");
99 assert (thread_has_run == 1);
100 assert (current_pth == test_pth);
102 /* Check timeout for system calls. */
104 pool_register_cleanup_fn (pool1, set_flag, &pool_gone);
105 pth1 = new_pseudothread (pool1, test_timeout, 0, "timeout thread");
109 assert (thread_has_run == 1);
110 assert (current_pth == test_pth);
111 while (!pool_gone) { pth_millisleep (100); }
117 test_pool = new_pool ();
118 test_pth = new_pseudothread (test_pool, do_test, 0, "testing thread");
120 /* Check that pth_start restores current_pth correctly. */
121 current_pth = (pseudothread) 0x1234;
122 pth_start (test_pth);
124 assert (current_pth == (pseudothread) 0x1234);
126 while (pseudothread_count_threads () > 0)