Add pthread attributes and other libc functions

This commit is contained in:
Justine Tunney 2022-09-07 05:23:44 -07:00
parent d5c9308a43
commit 4339d9f15e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
81 changed files with 1111 additions and 428 deletions

View file

@ -50,7 +50,7 @@ TEST(qsort, test) {
free(M);
}
BENCH(qsort, equivalence_random) {
TEST(qsort, equivalence_random) {
size_t i;
size_t n = 1000;
long *a = gc(malloc(n * sizeof(long)));
@ -68,7 +68,7 @@ BENCH(qsort, equivalence_random) {
ASSERT_EQ(0, memcmp(b, c, n * sizeof(long)));
}
BENCH(qsort, equivalence_reverse) {
TEST(qsort, equivalence_reverse) {
size_t i;
size_t n = 1000;
long *a = gc(malloc(n * sizeof(long)));

View file

@ -16,24 +16,96 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(l64a, test) {
EXPECT_STREQ("", l64a(0));
EXPECT_STREQ("zzzzz1", l64a(-1));
EXPECT_STREQ("zzzzz/", l64a(0x7fffffff));
}
TEST(a64l, test) {
EXPECT_EQ(0, a64l(""));
EXPECT_EQ(-1, a64l("zzzzz1"));
EXPECT_EQ(0x7fffffff, a64l("zzzzz/"));
}
dontinline long openbsd_a64l(const char *s) {
long value, digit, shift;
int i;
if (s == NULL) {
errno = EINVAL;
return (-1L);
}
value = 0;
shift = 0;
for (i = 0; *s && i < 6; i++, s++) {
if (*s >= '.' && *s <= '/')
digit = *s - '.';
else if (*s >= '0' && *s <= '9')
digit = *s - '0' + 2;
else if (*s >= 'A' && *s <= 'Z')
digit = *s - 'A' + 12;
else if (*s >= 'a' && *s <= 'z')
digit = *s - 'a' + 38;
else {
errno = EINVAL;
return (-1L);
}
value |= digit << shift;
shift += 6;
}
return (value);
}
dontinline char *openbsd_l64a(long value) {
static char buf[8];
char *s = buf;
int digit;
int i;
if (value < 0) {
errno = EINVAL;
return (NULL);
}
for (i = 0; value != 0 && i < 6; i++) {
digit = value & 0x3f;
if (digit < 2)
*s = digit + '.';
else if (digit < 12)
*s = digit + '0' - 2;
else if (digit < 38)
*s = digit + 'A' - 12;
else
*s = digit + 'a' - 38;
value >>= 6;
s++;
}
*s = '\0';
return (buf);
}
TEST(openbsd, consistency) {
long i;
for (i = 0; i < 512; ++i) {
ASSERT_STREQ(openbsd_l64a(i), l64a(i));
ASSERT_EQ(openbsd_a64l(openbsd_l64a(i)), a64l(l64a(i)));
}
for (i = INT_MAX; i > INT_MAX - 100; --i) {
ASSERT_STREQ(openbsd_l64a(i), l64a(i));
ASSERT_EQ(openbsd_a64l(openbsd_l64a(i)), a64l(l64a(i)));
}
}
BENCH(a64l, bench) {
EZBENCH2("l64a", donothing, l64a(INT_MAX));
EZBENCH2(
"a64l", donothing,
a64l("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
EZBENCH2("openbsd_l64a", donothing, openbsd_l64a(INT_MAX));
EZBENCH2(
"openbsd_a64l", donothing,
openbsd_a64l(
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}

View file

@ -16,24 +16,85 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/intrin/pthread.h"
#include "libc/mem/mem.h"
#include "libc/runtime/stack.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
pthread_t thread;
static void *ReturnArg(void *arg) {
return arg;
static void *Increment(void *arg) {
ASSERT_EQ(gettid(), pthread_getthreadid_np());
return (void *)((uintptr_t)arg + 1);
}
TEST(pthread_create, testCreateReturnJoin) {
void *exitcode;
ASSERT_EQ(0, pthread_create(&thread, 0, ReturnArg, ReturnArg));
ASSERT_EQ(0, pthread_join(thread, &exitcode));
ASSERT_EQ(ReturnArg, exitcode);
void *rc;
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, Increment, (void *)1));
ASSERT_EQ(0, pthread_join(id, &rc));
ASSERT_EQ((void *)2, rc);
}
static void *IncExit(void *arg) {
pthread_exit((void *)((uintptr_t)arg + 1));
}
TEST(pthread_create, testCreateExitJoin) {
void *rc;
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, IncExit, (void *)2));
ASSERT_EQ(0, pthread_join(id, &rc));
ASSERT_EQ((void *)3, rc);
}
TEST(pthread_detach, testCreateReturn) {
ASSERT_EQ(0, pthread_create(&thread, 0, ReturnArg, ReturnArg));
ASSERT_EQ(0, pthread_detach(thread));
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, Increment, 0));
ASSERT_EQ(0, pthread_detach(id));
}
TEST(pthread_detach, testDetachUponCreation) {
pthread_attr_t attr;
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
ASSERT_EQ(0, pthread_create(0, &attr, Increment, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
}
static void *CheckStack(void *arg) {
char buf[1024 * 1024];
CheckLargeStackAllocation(buf, 1024 * 1024);
return 0;
}
TEST(pthread_detach, testBigStack) {
pthread_t id;
pthread_attr_t attr;
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setstacksize(&attr, 2 * 1024 * 1024));
ASSERT_EQ(0, pthread_create(&id, &attr, CheckStack, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
ASSERT_EQ(0, pthread_join(id, 0));
}
TEST(pthread_detach, testCustomStack_withReallySmallSize) {
char *stk;
size_t siz;
pthread_t id;
pthread_attr_t attr;
siz = PTHREAD_STACK_MIN;
stk = malloc(siz);
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setstack(&attr, stk, siz));
ASSERT_EQ(0, pthread_create(&id, &attr, Increment, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
ASSERT_EQ(0, pthread_join(id, 0));
// we still own the stack memory
ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setstack(&attr, stk, siz));
ASSERT_EQ(0, pthread_create(&id, &attr, Increment, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
ASSERT_EQ(0, pthread_join(id, 0));
free(stk);
}

View file

@ -52,6 +52,10 @@ o/$(MODE)/test/libc/thread/%.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/test/libc/thread/pthread_create_test.o: \
private OVERRIDE_CPPFLAGS += \
-DSTACK_FRAME_UNLIMITED
.PHONY: o/$(MODE)/test/libc/thread
o/$(MODE)/test/libc/thread: \
$(TEST_LIBC_THREAD_BINS) \