summaryrefslogtreecommitdiff
path: root/create.c
blob: 2a94baf2b76d8a16c9449d1cd07d527c23f7df00 (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
/*
 * create.c
 *
 * Description:
 * This translation unit implements routines associated with spawning a new
 * thread.
 */

#include <windows.h>
#include <process.h>
#include <string.h>

#include "pthread.h"
#include "implement.h"

unsigned
_pthread_start_call(void * us_arg)
{
  /* We're now in a running thread. Any local variables here are on
     this threads private stack so we're safe to leave data in them
     until we leave. */
  _pthread_threads_thread__t * us;
  pthread_mutex_t * us_thread_mutex;
  _pthread_call_t * call;
  unsigned (*func)(void *);
  void * arg;
  unsigned ret;
  int from;

  us = (_pthread_threads_thread__t *) us_arg;

  /* FIXME: For now, if priority setting fails then at least ensure
     that our records reflect true reality. */
  if (SetThreadPriority((HANDLE) us->thread, us->attr.priority) == FALSE)
    {
      us->attr.priority = GetThreadPriority((HANDLE) us->thread);
    }

  func = us->call.routine;
  arg = us->call.arg;

  us_thread_mutex = _PTHREAD_THREAD_MUTEX(us);

  /* FIXME: Should we be using sigsetjmp() here instead. */
  from = setjmp(us->call.env);

  if (from == 0)
    {
      /* Normal return from setjmp(). */
      ret = (*func)(arg);

      _pthread_vacuum();

      /* Remove the thread entry on exit only if pthread_detach()
	 was called and there are no waiting joins. */

      /* CRITICAL SECTION */
      pthread_mutex_lock(us_thread_mutex);

      if (us->detach == TRUE
	  && us->join_count == 0)
	{
	  _pthread_delete_thread_entry(us);
	}

      pthread_mutex_lock(us_thread_mutex);
      /* END CRITICAL SECTION */
    }
  else
    {
      /* longjmp() teleported us here.
	 func() called pthread_exit() which called longjmp(). */
      _pthread_vacuum();

      /* Remove the thread entry on exit only if pthread_detach()
	 was called and there are no waiting joins. */

      /* CRITICAL SECTION */
      pthread_mutex_lock(us_thread_mutex);

      if (us->detach == TRUE
	  && us->join_count == 0)
	{
	  _pthread_delete_thread_entry(us);
	}

      pthread_mutex_lock(us_thread_mutex);
      /* END CRITICAL SECTION */

      ret = 0;
    }

  /* From Win32's point of view we always return naturally from our
     start routine and so it should clean up it's own thread residue. */
  return ret;
}

int
pthread_create(pthread_t *thread, 
	       const pthread_attr_t *attr,
	       void * (*start_routine) (void *), 
	       void * arg)
{
  HANDLE   handle = NULL;
  unsigned flags;
  void *   security = NULL;
  DWORD  threadID;
  pthread_attr_t * attr_copy;
  _pthread_threads_thread_t * this;
  /* Success unless otherwise set. */
  int ret = 0;

  if (_pthread_new_thread_entry((pthread_t) handle, this) == 0)
    {
      attr_copy = &(this->attr);

      /* Map given attributes otherwise just use default values. */
      if (attr != NULL) 
	{
	  if (attr_copy->stacksize == 0)
	    {
	      attr_copy->stacksize = PTHREAD_STACK_MIN;
	    }

	  attr_copy->cancelstate = attr->cancelstate;
	  attr_copy->canceltype = attr->canceltype;
	  attr_copy->detachedstate = attr->detachedstate;
	  attr_copy->priority = attr->priority;

#if HAVE_SIGSET_T
	  memcpy(&(attr_copy->sigmask), &(attr->sigmask), sizeof(sigset_t)); 
#endif /* HAVE_SIGSET_T */

	  this->detach = (attr->detachedstate == PTHREAD_CREATE_DETACHED);
	}

      /* Start running, not suspended. */
      flags = 0;

      handle = (HANDLE) _beginthreadex(security,
				       attr_copy->stacksize,
				       _pthread_start_call,
				       (void *) this,
				       flags,
				       &threadID);

      if (handle == NULL)
	{
	  ret = EAGAIN;
	}
    }
  else
    {
      ret = EAGAIN;
    }

  if (ret == 0)
    {
      /* Let the caller know the thread handle. */
      *thread = (pthread_t) handle;
    }
  else
    {
      /* Remove the failed thread entry. */
      _pthread_delete_thread_entry(this);
    }

  return ret;
}