pthread init and implementing pthread_self
This commit is contained in:
parent
bdb6a08704
commit
3e85210410
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
.globl _start
|
.globl _start
|
||||||
_start:
|
_start:
|
||||||
|
call __pthread_init
|
||||||
call main
|
call main
|
||||||
movq %rax, %rdi
|
movq %rax, %rdi
|
||||||
call OSExit
|
call OSExit
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -7,6 +9,8 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include <core/mutex.h>
|
||||||
|
|
||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
|
|
||||||
@ -17,6 +21,7 @@ struct pthread_attr {
|
|||||||
struct pthread {
|
struct pthread {
|
||||||
uint64_t tid;
|
uint64_t tid;
|
||||||
int errno;
|
int errno;
|
||||||
|
TAILQ_ENTRY(pthread) threadTable;
|
||||||
|
|
||||||
// Initialization
|
// Initialization
|
||||||
void *(*entry)(void *);
|
void *(*entry)(void *);
|
||||||
@ -24,13 +29,60 @@ struct pthread {
|
|||||||
|
|
||||||
// Termination
|
// Termination
|
||||||
void *result;
|
void *result;
|
||||||
|
|
||||||
|
// Condition Variables
|
||||||
|
TAILQ_ENTRY(pthread) threadTable;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef TAILQ_HEAD(pthreadList, pthread) pthreadList;
|
||||||
|
|
||||||
|
#define THREAD_HASH_SLOTS 32
|
||||||
|
|
||||||
|
static CoreMutex __threadTableLock;
|
||||||
|
static pthreadList __threads[THREAD_HASH_SLOTS];
|
||||||
|
|
||||||
|
void
|
||||||
|
__pthread_init(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
struct pthread *thr = (struct pthread *)malloc(sizeof(*thr));
|
||||||
|
|
||||||
|
for (i = 0; i < THREAD_HASH_SLOTS; i++) {
|
||||||
|
TAILQ_INIT(&__threads[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (thr == NULL) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
thr->tid = OSGetTID();
|
||||||
|
thr->entry = NULL;
|
||||||
|
thr->arg = 0;
|
||||||
|
|
||||||
|
CoreMutex_Init(&__threadTableLock);
|
||||||
|
|
||||||
|
CoreMutex_Lock(&__threadTableLock);
|
||||||
|
TAILQ_INSERT_HEAD(&__threads[thr->tid % THREAD_HASH_SLOTS], thr, threadTable);
|
||||||
|
CoreMutex_Unlock(&__threadTableLock);
|
||||||
|
}
|
||||||
|
|
||||||
pthread_t
|
pthread_t
|
||||||
pthread_self(void)
|
pthread_self(void)
|
||||||
{
|
{
|
||||||
// XXX Implement
|
int tid = OSGetTID();
|
||||||
return NULL;
|
struct pthread *thr;
|
||||||
|
|
||||||
|
CoreMutex_Lock(&__threadTableLock);
|
||||||
|
TAILQ_FOREACH(thr, &__threads[tid % THREAD_HASH_SLOTS], threadTable) {
|
||||||
|
if (thr->tid == tid) {
|
||||||
|
CoreMutex_Unlock(&__threadTableLock);
|
||||||
|
return thr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CoreMutex_Unlock(&__threadTableLock);
|
||||||
|
|
||||||
|
printf("pthread_self failed to find current thread!\n");
|
||||||
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -66,6 +118,10 @@ pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|||||||
|
|
||||||
thr->tid = SYSCALL_VALUE(status);
|
thr->tid = SYSCALL_VALUE(status);
|
||||||
|
|
||||||
|
CoreMutex_Lock(&__threadTableLock);
|
||||||
|
TAILQ_INSERT_HEAD(&__threads[thr->tid % THREAD_HASH_SLOTS], thr, threadTable);
|
||||||
|
CoreMutex_Unlock(&__threadTableLock);
|
||||||
|
|
||||||
*thread = thr;
|
*thread = thr;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -92,6 +148,10 @@ pthread_join(pthread_t thread, void **value_ptr)
|
|||||||
|
|
||||||
*value_ptr = (void *)thr->result;
|
*value_ptr = (void *)thr->result;
|
||||||
|
|
||||||
|
CoreMutex_Lock(&__threadTableLock);
|
||||||
|
TAILQ_REMOVE(&__threads[thr->tid % THREAD_HASH_SLOTS], thr, threadTable);
|
||||||
|
CoreMutex_Unlock(&__threadTableLock);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
free(thr);
|
free(thr);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user