OpenMPI  0.1.1
thread-st.h
1 /*
2  * $Id:$
3  * sproc version
4  * by Wolfram Gloger 2001, 2004
5  */
6 
7 #include <stdio.h>
8 #include <sys/wait.h>
9 #include <sys/types.h>
10 
11 #ifndef STACKSIZE
12 #define STACKSIZE 32768
13 #endif
14 
15 struct thread_st {
16  char *sp; /* stack pointer, can be 0 */
17  void (*func)(struct thread_st* st); /* must be set by user */
18  thread_id id;
19  int flags;
20  struct user_data u;
21 };
22 
23 static void
24 thread_init(void)
25 {
26  printf("Using sproc() threads.\n");
27 }
28 
29 static void
30 thread_wrapper(void *ptr, size_t stack_len)
31 {
32  struct thread_st *st = (struct thread_st*)ptr;
33 
34  /*printf("begin %p\n", st->sp);*/
35  st->func(st);
36  /*printf("end %p\n", st->sp);*/
37 }
38 
39 /* Create a thread. */
40 static int
41 thread_create(struct thread_st *st)
42 {
43  st->flags = 0;
44  if(!st->sp)
45  st->sp = malloc(STACKSIZE);
46  if(!st->sp) return -1;
47  st->id = sprocsp(thread_wrapper, PR_SALL, st, st->sp+STACKSIZE, STACKSIZE);
48  if(st->id < 0) {
49  return -1;
50  }
51  return 0;
52 }
53 
54 /* Wait for one of several subthreads to finish. */
55 static void
56 wait_for_thread(struct thread_st st[], int n_thr,
57  int (*end_thr)(struct thread_st*))
58 {
59  int i;
60  int id;
61 
62  int status = 0;
63  id = wait(&status);
64  if(status != 0) {
65  if(WIFSIGNALED(status))
66  printf("thread %id terminated by signal %d\n",
67  id, WTERMSIG(status));
68  else
69  printf("thread %id exited with status %d\n",
70  id, WEXITSTATUS(status));
71  }
72  for(i=0; i<n_thr; i++)
73  if(id == st[i].id) {
74  if(end_thr)
75  end_thr(&st[i]);
76  break;
77  }
78 }
79 
80 /*
81  * Local variables:
82  * tab-width: 4
83  * End:
84  */
Definition: thread-st.h:9
Definition: t-test1.c:28