summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libpthread/thread_internal.c
blob: a93806203b4d52c58e9fe00ffb75c0020741c5c8 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include <sched.h>
#include <sys/resource.h>

#include <stdio.h>
#include "thread_internal.h"

static struct _pthread_fastlock __thread_struct_lock = {0};
static struct _pthread_descr_struct threads[PTHREAD_THREADS_MAX];
static int _max_used_thread_id=1;
pthread_once_t __thread_inited;

static struct _pthread_fastlock __manager_thread_signal_lock = {0};
static struct _pthread_fastlock __manager_thread_data_lock = {1};
static struct _pthread_fastlock __manager_thread_data_go_lock = {1};

//#define DEBUG

/* find thread */
int __find_thread_id(int pid)
{
  register int i;
  for (i=0; i<_max_used_thread_id; i++)
    if (threads[i].pid==pid)
      return i;
  return -1;
}

/* get thread */
_pthread_descr __get_thread_struct(int id)
{
  return threads+id;
}

/* thread errno location */
int *__errno_location(void)
{
  int id=0;
  if (__thread_inited) id=__find_thread_id(getpid());
  if (id<0)
    return 0;
  else
    return &threads[id].errno;
}

/* thread self */
_pthread_descr __thread_self()
{
  register int i=__find_thread_id(getpid());
  if (i<0)
    return 0;
  else
    return threads+i;
}

/* allocate a thread slot */
_pthread_descr __thread_get_free()
{
  _pthread_descr ret=0;
  int i;

  __NO_ASYNC_CANCEL_BEGIN;
  __pthread_lock(&__thread_struct_lock);

  for (i=0; i<PTHREAD_THREADS_MAX; i++) {
    if (threads[i].pid==0) {
      threads[i].pid=1; /* mark as taken */
      ret = threads+i;
      if (i>=_max_used_thread_id) _max_used_thread_id=i+1;
      break;
    }
  }

  __pthread_unlock(&__thread_struct_lock);
  __NO_ASYNC_CANCEL_END;
  return ret;
}

/* sleep a little (reschedule for this time) */
void __thread_wait_some_time()
{
  struct timespec reg;
  reg.tv_sec=0;
  reg.tv_nsec=SPIN_SLEEP_DURATION;
  __libc_nanosleep(&reg,0);
}

/* cleanup a thread struct */
void __thread_cleanup(_pthread_descr th)
{
  /* lib provided stack should be freed */
  if (th->stack_begin) free(th->stack_begin);

  /* an other thread has joined this on */
  if (th->joined) {
    th->joined->retval=th->retval;
    th->joined->join=0;
    th->joined=0;
  }
  th->pid=0;	/* mark struct as free */
}

/* SIGHUP handler (thread cnacel) PTHREAD_CANCEL_ASYNCHRONOUS */
static void __thread_cancel_handler(int sig)
{
  _pthread_descr this;
  this = __thread_self();
  this->canceled=1;
  if (this->canceltype==PTHREAD_CANCEL_ASYNCHRONOUS)
    pthread_exit(PTHREAD_CANCELED);
  signal( SIGHUP, __thread_cancel_handler );
}

/* kill ALL threads / other then prime task and manager thread */
static void __kill_all_threads()
{
  int i;

  for (i=2; i<_max_used_thread_id; i++) {
    if (threads[i].pid>1) {
#ifdef DEBUG
      printf("CANCEL ! %d\n",threads[i].pid);
#endif
      threads[i].canceled=1;
      kill(threads[i].pid, SIGHUP);	/* cancel thread */
    }
  }

  __thread_wait_some_time();

  for (i=2; i<_max_used_thread_id; i++) {
    if (threads[i].pid>1) {
#ifdef DEBUG
      printf("KILL ! %d\n",threads[i].pid);
#endif
      kill(threads[i].pid, SIGTERM);	/* KILL thread */
    }
  }
}

__attribute__((weak)) volatile void __thread_start__key(int id) { return; }
__attribute__((weak,alias("__thread_start__key"))) volatile void __thread_exit__key(int id);

/* support for manager */
static void *__mthread_starter(void *arg)
{
  _pthread_descr td = (_pthread_descr)arg;
  int i = td->stack_size-4096;

  /* just to be sure */
  td->pid=getpid();

  /* signal handling for a thread */
  signal(SIGTERM, _exit);
  signal(SIGCHLD, SIG_DFL);
  signal(SIGHUP, __thread_cancel_handler );

  /* limit stack so that we NEVER have to worry */
  setrlimit(RLIMIT_STACK, (struct rlimit *)(&i));

  /* set scheduler */
  if (td->policy!=SCHED_OTHER) {
    struct sched_param sp;
    sp.sched_priority=td->priority;
    sched_setscheduler(td->pid,td->policy, &sp);
  }

  /* thread_key glue */
  __thread_start__key(td-threads);

#ifdef DEBUG
  printf("in starter %d, parameter %8p\n", td->pid, td->func);
#endif

  if (!td->canceled) {
    if (!(setjmp(td->jmp_exit))) {
      td->retval=td->func(td->arg);
#ifdef DEBUG
    } else {
      printf("pthread_exit called in %d\n", td->pid);
#endif
    }
  }
  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,0);

  /* thread_key glue */
  __thread_exit__key(td-threads);

#ifdef DEBUG
  printf("end starter %d, retval %8p\n", td->pid, td->retval);
#endif

  /* wake joined thread and put retval */
  if (td->joined) {
    td->joined->retval=td->retval;
    td->joined->join=0;
    td->joined=0;
  }

  /* execute all functions on the cleanup-stack */
  for (i=PTHREAD_MAX_CLEANUP;i;) {
    if (td->cleanup_stack[--i].func) {
      td->cleanup_stack[i].func(td->cleanup_stack[i].arg);
    }
  }

  return 0;
}


/* manager thread and signal handler */
static char __manager_thread_stack[12*1024];
static volatile _pthread_descr __manager_thread_data;
static void __manager_SIGCHLD(int sig)
{
  int pid, status, i;

  while(1) {
    pid = __libc_waitpid (-1, &status, WNOHANG);
    if (pid <= 0) break;

    for (i=0; i<_max_used_thread_id; i++) {
      if (threads[i].pid==pid) {
	__thread_cleanup(threads+i);
	break;
      }
    }
  }
}

static void __manager_SIGTERM(int sig)
{
  __kill_all_threads();
  _exit(0);
}

static void* __manager_thread(void *arg)
{
  struct sigaction sig_action_chld;
  sig_action_chld.sa_handler = __manager_SIGCHLD;
  sigemptyset(&sig_action_chld.sa_mask);
  sig_action_chld.sa_flags = SA_RESTART;

  sigaction(SIGCHLD, &sig_action_chld, 0);
  signal(SIGTERM, __manager_SIGTERM);
  signal(SIGHUP, SIG_IGN);

  __pthread_unlock(&__manager_thread_data_go_lock);	/* release init */
  while(1) {
    do {
      __thread_wait_some_time();
      if (getppid()<0) __manager_SIGTERM(0);
    } while (__pthread_trylock(&__manager_thread_data_lock));

    __manager_thread_data->pid =
      __clone(__mthread_starter,
	      __manager_thread_data->stack_addr,
	      CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD,
	      __manager_thread_data);
    __thread_wait_some_time();
#ifdef DEBUG
    printf("manager new thread %d\n",__manager_thread_data->pid);
#endif
    __pthread_unlock(&__manager_thread_data_go_lock);	/* release sender */
  }
  return 0;
}

/* pthread_create bottom half */
int signal_manager_thread(_pthread_descr td)
{
  __NO_ASYNC_CANCEL_BEGIN;

  __pthread_lock(&__manager_thread_signal_lock);	/* lock */

  __manager_thread_data = td;
  __thread_wait_some_time();
  __pthread_unlock(&__manager_thread_data_lock);	/* signal manager to start */
  __thread_wait_some_time();
  __pthread_lock(&__manager_thread_data_go_lock);	/* wait for manager */

  __pthread_unlock(&__manager_thread_signal_lock);	/* unlock */

  __NO_ASYNC_CANCEL_END;

  return td->pid;
}


/* thread stop */
static void __thread_main_exit()
{
  if (getpid()!=threads[0].pid) {
#ifdef DEBUG
    printf("A THREAD ? %d\n",getpid());
#endif
    kill(threads[0].pid, SIGTERM);
    while(1) __thread_wait_some_time();
  }
#ifdef DEBUG
  else
    printf("EXIT ! %d\n",getpid());
#endif

  /* stop ALL threads */
  kill(threads[1].pid, SIGTERM);
  __thread_wait_some_time();
  __kill_all_threads();
}

/* thread intern init */
void __thread_init()
{
  if (atexit(__thread_main_exit)==-1)
    exit(42);

#ifdef DEBUG
  printf("INIT ! %d\n",getpid());
  memset(threads,0,sizeof(threads));
#endif

  threads[0].pid = getpid();

  ++_max_used_thread_id;
  threads[1].stack_size=sizeof(__manager_thread_stack);
  threads[1].stack_addr=&__manager_thread_stack[sizeof(__manager_thread_stack)];
  threads[1].stack_begin=0;
  threads[1].func=__manager_thread;

  threads[1].pid = __clone(__mthread_starter, threads[1].stack_addr,
			   CLONE_VM | CLONE_FS | CLONE_FILES, threads+1);

#ifdef DEBUG
  printf("manager thread @ : %d\n",threads[1].pid);
#endif
  __pthread_lock(&__manager_thread_data_go_lock);	/* wait for manager to be ready */
}