source: trunk/libs/newlib/src/newlib/libc/sys/linux/linuxthreads/tst-context.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.0 KB
Line 
1#include <errno.h>
2#include <error.h>
3#include <pthread.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <ucontext.h>
7
8#include "pt-machine.h"
9
10
11#define N       4
12
13#ifdef FLOATING_STACKS
14static char stacks[N][8192];
15static ucontext_t ctx[N][2];
16static volatile int failures;
17
18static void
19fct (long int n)
20{
21  /* Just to use the thread local descriptor.  */
22  printf ("%ld: in %s now\n", n, __FUNCTION__);
23  errno = 0;
24}
25
26static void *
27threadfct (void *arg)
28{
29  int n = (int) (long int) arg;
30
31  if (getcontext (&ctx[n][1]) != 0)
32    {
33      printf ("%d: cannot get context: %m\n", n);
34      exit (1);
35    }
36
37  printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
38
39  ctx[n][1].uc_stack.ss_sp = stacks[n];
40  ctx[n][1].uc_stack.ss_size = 8192;
41  ctx[n][1].uc_link = &ctx[n][0];
42  makecontext (&ctx[n][1], (void (*) (void)) fct, 1, (long int) n);
43
44  printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
45
46  if (swapcontext (&ctx[n][0], &ctx[n][1]) != 0)
47    {
48      ++failures;
49      printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
50    }
51  else
52    printf ("%d: back in %s\n", n, __FUNCTION__);
53
54  return NULL;
55}
56#endif
57
58
59#ifdef FLOATING_STACKS
60static volatile int global;
61#endif
62
63int
64main (void)
65{
66#ifndef FLOATING_STACKS
67  puts ("not supported");
68  return 0;
69#else
70  int n;
71  pthread_t th[N];
72  ucontext_t mctx;
73
74  puts ("making contexts");
75  if (getcontext (&mctx) != 0)
76    {
77      if (errno == ENOSYS)
78        {
79          puts ("context handling not supported");
80          exit (0);
81        }
82
83      printf ("%s: getcontext: %m\n", __FUNCTION__);
84      exit (1);
85    }
86
87  /* Play some tricks with this context.  */
88  if (++global == 1)
89    if (setcontext (&mctx) != 0)
90      {
91        printf ("%s: setcontext: %m\n", __FUNCTION__);
92        exit (1);
93      }
94  if (global != 2)
95    {
96      printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
97      exit (1);
98    }
99
100  for (n = 0; n < N; ++n)
101    if (pthread_create (&th[n], NULL, threadfct, (void *) n) != 0)
102      error (EXIT_FAILURE, errno, "cannot create all threads");
103
104  for (n = 0; n < N; ++n)
105    pthread_join (th[n], NULL);
106
107  return failures;
108#endif
109}
Note: See TracBrowser for help on using the repository browser.