aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/cygthread.cc
blob: bbfcd8218c1a2288f42f4240236227d0bb3fa521 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* cygthread.cc

   Copyright 1998, 1999, 2000, 2001, 2002 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. */

class cygthread
{
  DWORD avail;
  DWORD id;
  HANDLE h;
  HANDLE ev;
  const char *__name;
  LPTHREAD_START_ROUTINE func;
  VOID *arg;
  static DWORD main_thread_id;
  static DWORD WINAPI runner (VOID *);
  static DWORD WINAPI stub (VOID *);
 public:
  static const char * name (DWORD = 0);
  cygthread (LPTHREAD_START_ROUTINE, LPVOID, const char *);
  cygthread () {};
  static void init ();
  void detach ();
  operator HANDLE ();
  static bool is ();
  void * operator new (size_t);
};
#include "winsup.h"
#include "exceptions.h"
#include "security.h"
#include "cygthread.h"
#include <windows.h>

#undef CloseHandle

static cygthread NO_COPY threads[8];
#define NTHREADS (sizeof (threads) / sizeof (threads[0]))

static HANDLE NO_COPY hthreads[NTHREADS];

DWORD NO_COPY cygthread::main_thread_id;

/* Initial stub called by makethread. Performs initial per-thread
   initialization.  */
DWORD WINAPI
cygthread::stub (VOID *arg)
{
  DECLARE_TLS_STORAGE;
  exception_list except_entry;

  /* Initialize this thread's ability to respond to things like
     SIGSEGV or SIGFPE. */
  init_exceptions (&except_entry);

  cygthread *info = (cygthread *) arg;
  info->ev = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
  while (1)
    {
      if (!info->func)
	ExitThread (0);

      /* Cygwin threads should not call ExitThread */
      info->func (info->arg);

      info->__name = NULL;
      SetEvent (info->ev);
      SuspendThread (info->h);
    }
}

DWORD WINAPI
cygthread::runner (VOID *arg)
{
  for (unsigned i = 0; i < NTHREADS; i++)
    hthreads[i] = threads[i].h =
      CreateThread (&sec_none_nih, 0, cygthread::stub, &threads[i],
		    CREATE_SUSPENDED, &threads[i].avail);
  return 0;
}

void
cygthread::init ()
{
  DWORD tid;
  HANDLE h = CreateThread (&sec_none_nih, 0, cygthread::runner, NULL, 0, &tid);
  if (!h)
    api_fatal ("can't start thread_runner, %E");
  CloseHandle (h);
  main_thread_id = GetCurrentThreadId ();
}

bool
cygthread::is ()
{
  DWORD tid = GetCurrentThreadId ();

  for (DWORD i = 0; i < NTHREADS; i++)
    if (threads[i].id == tid)
      return 1;

  return 0;
}

void * cygthread::operator
new (size_t)
{
  DWORD id;
  cygthread *info; /* Various information needed by the newly created thread */

  for (;;)
    {
      /* Search the threads array for an empty slot to use */
      for (info = threads; info < threads + NTHREADS; info++)
	if ((id = (DWORD) InterlockedExchange ((LPLONG) &info->avail, 0)))
	  {
	    info->id = id;
	    return info;
	  }

      /* thread_runner may not be finished yet. */
      Sleep (0);
    }
}

cygthread::cygthread (LPTHREAD_START_ROUTINE start, LPVOID param,
		      const char *name): __name (name), func (start), arg (param)
{
  while (ResumeThread (h) == 0)
    Sleep (0);
}

/* Return the symbolic name of the current thread for debugging.
 */
const char *
cygthread::name (DWORD tid)
{
  const char *res = NULL;
  if (!tid)
    tid = GetCurrentThreadId ();

  if (tid == main_thread_id)
    return "main";

  for (DWORD i = 0; i < NTHREADS; i++)
    if (threads[i].id == tid)
      {
	res = threads[i].__name ?: "exiting thread";
	break;
      }

  if (!res)
    {
      static char buf[30] NO_COPY = {0};
      __small_sprintf (buf, "unknown (%p)", tid);
      res = buf;
    }

  return res;
}

cygthread::operator
HANDLE ()
{
  while (!ev)
    Sleep (0);
  return ev;
}

void
cygthread::detach ()
{
  if (!avail)
    {
      DWORD avail = id;
      if (__name)
	{
	  DWORD res = WaitForSingleObject (*this, INFINITE);
	  debug_printf ("WFSO returns %d", res);
	}
      id = 0;
      (void) InterlockedExchange ((LPLONG) &this->avail, avail);
    }
}