summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/libpthread/thread_internal.h
blob: 5eb3bbd0865bcdf208072d9fc2cffaba6f41997b (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
#ifndef __THREAD_INTERNAL_H__
#define __THREAD_INTERNAL_H__

#include <pthread.h>
#include <stdarg.h>
#include <setjmp.h>
#include "dietfeatures.h"
#ifndef WANT_THREAD_SAFE
#error "the diet libc is not compiled with thread safeness enabled!"
#endif

/* cleanup */
#define PTHREAD_MAX_CLEANUP 8
struct thread_cleanup_t {
  void (*func)(void*);
  void *arg;
};

/* the thread descriptor / internal */
struct _pthread_descr_struct {
  /* runtime handling */
  struct _pthread_descr_struct *joined; /* a joined thread or NULL */

  /* conditional variables */
  struct _pthread_descr_struct *waitnext; /* an other waiting thread or NULL */
  int  waiting;			/* internal waiting "lock" */

  /* thread/process data */
  int  pid;			/* Process id */

  int  policy;			/* thread scheduling policy */
  int  priority;		/* thread priority */

  /* errno handling */
  int   errno;
  int h_errno;

  /* stack handling */
  unsigned int stack_size;	/* stack size for setrlimit */
  void *stack_addr;		/* stack address for clone */
  void *stack_begin;		/* begin of lib-stack / lowest address (free) */

  /* thread exit handling */
  void  *retval;		/* thread return value */
  int   join;			/* thread waits for other to return */
  jmp_buf jmp_exit;		/* pthread_exit jump */

  /* thread flags */
  int  detached;		/* thread is detached */
  int  canceled;		/* thread canceled */

  /* cancel handling */
  int  cancelstate;		/* cancel state */
  int  canceltype;		/* type of cancellation */

  /* thread basics */
  void* (*func) (void* arg);	/* thread function */
  void* arg;			/* thread argument */

  /* create thread / manager thread lock */
  struct _pthread_fastlock *manager_lock;

  /* cleanup stack */
  struct thread_cleanup_t cleanup_stack[PTHREAD_MAX_CLEANUP];

} __attribute__ ((aligned(32)));

/* thread keys */
struct _thread_key {
  int used;
  void (*destructor)(const void*);
  const void *tkd[PTHREAD_THREADS_MAX];
};

/* internal stuff */
int __testandset(int *spinlock);

void __pthread_lock(struct _pthread_fastlock * lock);
int __pthread_trylock(struct _pthread_fastlock * lock);
int __pthread_unlock(struct _pthread_fastlock * lock);

int __clone(void* (*fn)(void*), void* stack, int flags, void *arg);

int __find_thread_id(int pid);
_pthread_descr __get_thread_struct(int id);

_pthread_descr __thread_get_free();
_pthread_descr __thread_self();

void __thread_cleanup(_pthread_descr th);

void __thread_wait_some_time();

#define __NO_ASYNC_CANCEL_BEGIN { int oldtype; pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
#define __NO_ASYNC_CANCEL_END pthread_setcanceltype(oldtype,0); pthread_testcancel(); }
#define __NO_ASYNC_CANCEL_STOP pthread_setcanceltype(oldtype,0); }

/* manager thread stuff */
int signal_manager_thread(_pthread_descr td);

/* init stuff */
extern pthread_once_t __thread_inited;
void __thread_init();
#define __THREAD_INIT() __pthread_once(&__thread_inited, __thread_init)
#define __TEST_CANCEL() pthread_testcancel()

/* diet libc syscalls */

void  __libc_free(void *ptr);
void *__libc_malloc(size_t size);

void __libc_closelog(void);
void __libc_openlog(const char *ident, int option, int facility);
void __libc_vsyslog(int priority, const char *format, va_list arg_ptr);

pid_t __libc_fork(void);

int __libc_close(int fd);
int __libc_creat(const char *pathname, mode_t mode);
int __libc_fcntl(int fd, int cmd, void *arg);
int __libc_fsync(int fd);
int __libc_nanosleep(const struct timespec *req, struct timespec *rem);
int __libc_open(const char *pathname, int flags, mode_t mode);
int __libc_pause(void);
int __libc_read(int fd, void *buf, size_t count);
int __libc_sigsuspend(const sigset_t *mask);
int __libc_tcdrain(int fd);
pid_t __libc_waitpid(pid_t pid, int *status, int options);
int __libc_write(int fd, const void *buf, size_t count);

#endif