2022-09-05 15:26:03 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_
|
2022-09-09 18:30:33 +00:00
|
|
|
#include "libc/calls/struct/sched_param.h"
|
2022-09-05 15:26:03 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-09-05 15:26:03 +00:00
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
|
2022-09-06 04:43:49 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Cosmopolitan POSIX Thread Internals
|
|
|
|
*/
|
|
|
|
|
2022-09-08 04:13:50 +00:00
|
|
|
// LEGAL TRANSITIONS ┌──> TERMINATED
|
|
|
|
// pthread_create ─┬─> JOINABLE ─┴┬─> DETACHED ──> ZOMBIE
|
|
|
|
// └──────────────┘
|
2022-09-05 15:26:03 +00:00
|
|
|
enum PosixThreadStatus {
|
2022-09-08 04:13:50 +00:00
|
|
|
|
|
|
|
// this is a running thread that needs pthread_join()
|
|
|
|
//
|
|
|
|
// the following transitions are possible:
|
|
|
|
//
|
|
|
|
// - kPosixThreadJoinable -> kPosixThreadTerminated if start_routine()
|
|
|
|
// returns, or is longjmp'd out of by pthread_exit(), and the thread
|
|
|
|
// is waiting to be joined.
|
|
|
|
//
|
|
|
|
// - kPosixThreadJoinable -> kPosixThreadDetached if pthread_detach()
|
|
|
|
// is called on this thread.
|
2022-09-07 12:23:44 +00:00
|
|
|
kPosixThreadJoinable,
|
2022-09-08 04:13:50 +00:00
|
|
|
|
|
|
|
// this is a managed thread that'll be cleaned up by the library.
|
|
|
|
//
|
|
|
|
// the following transitions are possible:
|
|
|
|
//
|
|
|
|
// - kPosixThreadDetached -> kPosixThreadZombie if start_routine()
|
|
|
|
// returns, or is longjmp'd out of by pthread_exit(), and the thread
|
|
|
|
// is waiting to be joined.
|
2022-09-05 15:26:03 +00:00
|
|
|
kPosixThreadDetached,
|
2022-09-08 04:13:50 +00:00
|
|
|
|
|
|
|
// this is a joinable thread that terminated.
|
|
|
|
//
|
|
|
|
// the following transitions are possible:
|
|
|
|
//
|
|
|
|
// - kPosixThreadTerminated -> _pthread_free() will happen when
|
|
|
|
// pthread_join() is called by the user.
|
2022-09-05 15:26:03 +00:00
|
|
|
kPosixThreadTerminated,
|
2022-09-08 04:13:50 +00:00
|
|
|
|
|
|
|
// this is a detached thread that terminated.
|
|
|
|
//
|
|
|
|
// the following transitions are possible:
|
|
|
|
//
|
|
|
|
// - kPosixThreadZombie -> _pthread_free() will happen whenever
|
|
|
|
// convenient, e.g. pthread_create() entry or atexit handler.
|
2022-09-05 15:26:03 +00:00
|
|
|
kPosixThreadZombie,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PosixThread {
|
|
|
|
void *(*start_routine)(void *);
|
2022-09-07 12:23:44 +00:00
|
|
|
void *arg; // start_routine's parameter
|
|
|
|
void *rc; // start_routine's return value
|
2022-09-09 11:07:08 +00:00
|
|
|
bool ownstack;
|
|
|
|
int tid;
|
|
|
|
int *ctid;
|
|
|
|
char *tls;
|
|
|
|
char *tib;
|
2022-09-05 15:26:03 +00:00
|
|
|
_Atomic(enum PosixThreadStatus) status;
|
|
|
|
jmp_buf exiter;
|
2022-09-07 12:23:44 +00:00
|
|
|
pthread_attr_t attr;
|
2022-09-05 15:26:03 +00:00
|
|
|
};
|
|
|
|
|
2022-09-10 09:56:25 +00:00
|
|
|
int _pthread_reschedule(struct PosixThread *) hidden;
|
|
|
|
int _pthread_setschedparam_freebsd(int, int, const struct sched_param *) hidden;
|
2022-09-08 04:13:50 +00:00
|
|
|
void _pthread_free(struct PosixThread *) hidden;
|
2022-09-10 09:56:25 +00:00
|
|
|
void _pthread_ungarbage(void) hidden;
|
2022-09-08 04:13:50 +00:00
|
|
|
void _pthread_wait(struct PosixThread *) hidden;
|
|
|
|
void _pthread_zombies_add(struct PosixThread *) hidden;
|
|
|
|
void _pthread_zombies_decimate(void) hidden;
|
|
|
|
void _pthread_zombies_harvest(void) hidden;
|
2022-09-05 15:26:03 +00:00
|
|
|
|
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_ */
|