Commit 22aac206 authored by Donald Haase's avatar Donald Haase
Browse files

Initial commit of simple threads test

parents
#pthread_tests makefile by Quzar
NAME = pthread_tests
TARGET = $(NAME).elf
OBJS= $(NAME).o
all: rm-elf $(TARGET)
include $(KOS_BASE)/Makefile.rules
clean: rm-elf
-rm -f $(OBJS)
rm-elf:
-rm -f $(TARGET)
$(TARGET): $(OBJS)
kos-cc -o $(TARGET) $(OBJS) -lpthread
run: $(TARGET)
$(KOS_LOADER) $(TARGET)
dist: $(TARGET)
-rm -f $(OBJS)
$(KOS_STRIP) $(TARGET)
\ No newline at end of file
/*
* pthread example test app for lldb, from Michael Sartain's post at
* http://linux-debugger-bits.blogspot.com/2013/07/linux-pthread-test-app-with-lldb.html
*
* clang -Wall -g -o mikesart_pthread -lpthread mikesart_pthread.c
* Taken from https://gist.github.com/mikesart/7c915b20df4af2cad7f0d0375be2d208
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <kos.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
uint32_t g_sleep_time = 3;
__thread pid_t g_tls = -1;
__thread char g_threadname[32];
pid_t gettid()
{
return (pid_t)thd_get_id(0);
}
void setname(const char *name)
{
pthread_setname_np(pthread_self(), g_threadname);
}
void logit(const char *format, ...)
{
va_list args;
char buf[1024];
snprintf(buf, sizeof(buf), "'%s' [#%d LWP:%d 0x%lx] %s", g_threadname, g_tls, gettid(), (unsigned long)pthread_self(), format);
va_start (args, format);
vprintf (buf, args);
va_end (args);
}
void *thread_proc(void *arg)
{
g_tls = (int)(intptr_t)arg;
snprintf(g_threadname, sizeof(g_threadname), "thread_%d", g_tls);
logit("pthread_setname_np('%s')\n", g_threadname);
setname(g_threadname);
uint64_t t0 = timer_us_gettime64();
if ( !t0 )
{
logit("sleep(%u)\n", g_sleep_time);
sleep(g_sleep_time);
}
else
{
uint64_t last = t0;
logit("busy loop %u\n", g_sleep_time);
for( ;; )
{
uint64_t t1 = timer_us_gettime64();
if ( t1 - t0 >= g_sleep_time * 1000000 )
break;
if ( t1 - last >= 1000000 )
{
logit( "%.2f seconds\n", ( t1 - t0 ) / 1000000.0f );
last = t1;
}
}
}
pid_t tid = gettid();
logit("pthread_exit(%d)\n", tid);
pthread_exit((void *)(intptr_t)tid);
return 0;
}
#define THREAD_COUNT 512
int main(int argc, char *argv[])
{
pthread_t threadids[THREAD_COUNT];
snprintf(g_threadname, sizeof(g_threadname), "thread_main");
setname(g_threadname);
logit("num_threads:%zu\n", THREAD_COUNT);
for(size_t i = 0; i < THREAD_COUNT; i++)
{
int err = pthread_create(&(threadids[i]), NULL, &thread_proc, (void *)(intptr_t)i);
logit("pthread_create:%d (%s) pthread_t:%lx\n", err, strerror(err), threadids[i]);
}
sleep(1);
for(size_t i = 0; i < THREAD_COUNT; i++)
{
logit("Waiting for thread #%zu\n", i);
void *status = NULL;
int rc = pthread_join(threadids[i], &status);
logit("Thread #%zu rc:%d status:%d\n", i, rc, (int)(intptr_t)status);
}
logit("done.\n");
return 0;
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment