summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/cygtls.cc
blob: 2734cf5369af52d600f4201ea640515057cc3f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* cygtls.h

   Copyright 2003 Red Hat, Inc.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include "thread.h"
#include "cygtls.h"
#include "assert.h"
#include <syslog.h>

static _threadinfo NO_COPY dummy_thread;
_threadinfo NO_COPY *_last_thread = &dummy_thread;

CRITICAL_SECTION NO_COPY _threadinfo::protect_linked_list;

void
_threadinfo::set_state (bool is_exception)
{
  initialized = CYGTLS_INITIALIZED + is_exception;
}

void
_threadinfo::reset_exception ()
{
  if (initialized == CYGTLS_EXCEPTION)
    {
#ifdef DEBUGGING
      debug_printf ("resetting stack after an exception stack %p, stackptr %p", stack, stackptr);
#endif
      set_state (false);
      stackptr--;
    }
}

/* Two calls to get the stack right... */
void
_threadinfo::call (DWORD (*func) (void *, void *), void *arg)
{
  char buf[CYGTLS_PADSIZE];
  call2 (func, arg, buf);
}

void
_threadinfo::call2 (DWORD (*func) (void *, void *), void *arg, void *buf)
{
  _my_tls.init_thread (buf);
  ExitThread (func (arg, buf));
}

void
_threadinfo::init ()
{
  InitializeCriticalSection (&protect_linked_list);
}

void
_threadinfo::init_thread (void *x)
{
  if (x)
    {
      memset (this, 0, sizeof (*this));
      stackptr = stack;
      if (_GLOBAL_REENT)
	{
	  local_clib._stdin = _GLOBAL_REENT->_stdin;
	  local_clib._stdout = _GLOBAL_REENT->_stdout;
	  local_clib._stderr = _GLOBAL_REENT->_stderr;
	  local_clib.__sdidinit = _GLOBAL_REENT->__sdidinit;
	  local_clib.__cleanup = _GLOBAL_REENT->__cleanup;
	}
      local_clib._current_locale = "C";
      locals.process_logmask = LOG_UPTO (LOG_DEBUG);
    }

  EnterCriticalSection (&protect_linked_list);
  prev = _last_thread;
  _last_thread->next = this;
  _last_thread = this;
  LeaveCriticalSection (&protect_linked_list);

  set_state (false);
  errno_addr = &(local_clib._errno);
}

void
_threadinfo::remove ()
{
  EnterCriticalSection (&protect_linked_list);
  if (prev)
    {
      prev->next = next;
      if (next)
	next->prev = prev;
      if (this == _last_thread)
	_last_thread = prev;
      prev = next = NULL;
    }
  LeaveCriticalSection (&protect_linked_list);
}

void
_threadinfo::push (__stack_t addr, bool exception)
{
  *stackptr++ = (__stack_t) addr;
  set_state (exception);
}

__stack_t
_threadinfo::pop ()
{
#ifdef DEBUGGING
  assert (stackptr > stack);
#endif
  __stack_t res = *--stackptr;
#ifdef DEBUGGING
  *stackptr = 0;
  debug_printf ("popped %p, stack %p, stackptr %p", res, stack, stackptr);
#endif
  return res;
}