Add to git.
[pthrlib.git] / src / test_pseudothread.c
1 /* Test the pseudothreads.
2  * Copyright (C) 2001 Richard W.M. Jones <rich@annexia.org>
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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_pseudothread.c,v 1.5 2002/12/01 14:29:31 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34
35 #include <pool.h>
36
37 #include "pthr_pseudothread.h"
38
39 static void set_flag (void *data) { int *flag = (int *) data; *flag = 1; }
40
41 static pool test_pool;
42 static pseudothread test_pth;
43
44 static pool pool1;
45 static pseudothread pth1;
46 static int pool_gone = 0;
47 static int thread_has_run = 0;
48
49 static void
50 test_exiting (void *data)
51 {
52   assert (current_pth == pth1);
53   thread_has_run = 1;
54   pth_exit ();
55   abort ();
56 }
57
58 static void
59 test_timeout (void *data)
60 {
61   assert (current_pth == pth1);
62   thread_has_run = 1;
63   pth_timeout (1);
64   pth_sleep (1000);
65 }
66
67 static void
68 do_test (void *data)
69 {
70   /* Check current_pth set correctly on thread start. */
71   assert (current_pth == test_pth);
72
73   /* Launch a thread and check that the thread runs and the pool is
74    * deleted at the end.
75    */
76   pool1 = new_pool ();
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);
85
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);
93
94   /* Check pth_exit function. */
95   pool1 = new_pool ();
96   pth1 = new_pseudothread (pool1, test_exiting, 0, "exiting thread");
97   thread_has_run = 0;
98   pth_start (pth1);
99   assert (thread_has_run == 1);
100   assert (current_pth == test_pth);
101
102   /* Check timeout for system calls. */
103   pool1 = new_pool ();
104   pool_register_cleanup_fn (pool1, set_flag, &pool_gone);
105   pth1 = new_pseudothread (pool1, test_timeout, 0, "timeout thread");
106   thread_has_run = 0;
107   pool_gone = 0;
108   pth_start (pth1);
109   assert (thread_has_run == 1);
110   assert (current_pth == test_pth);
111   while (!pool_gone) { pth_millisleep (100); }
112 }
113
114 int
115 main ()
116 {
117   test_pool = new_pool ();
118   test_pth = new_pseudothread (test_pool, do_test, 0, "testing thread");
119
120   /* Check that pth_start restores current_pth correctly. */
121   current_pth = (pseudothread) 0x1234;
122   pth_start (test_pth);
123
124   assert (current_pth == (pseudothread) 0x1234);
125
126   while (pseudothread_count_threads () > 0)
127     reactor_invoke ();
128
129   exit (0);
130 }