summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2018-02-18 16:45:09 +0200
committerJari Vetoniemi <mailroxas@gmail.com>2018-02-18 16:45:09 +0200
commitb1090c6c7ec455e2104dd4205ee48139009a0927 (patch)
tree596f765b805267659b0d923fa065739da6c5df31 /src
parentf378a448336d4fb3547b53c0712fd7307fc0ba3a (diff)
Implement more pthread
Diffstat (limited to 'src')
-rw-r--r--src/libpthread.c65
1 files changed, 43 insertions, 22 deletions
diff --git a/src/libpthread.c b/src/libpthread.c
index 3359c4a..db11fc5 100644
--- a/src/libpthread.c
+++ b/src/libpthread.c
@@ -52,9 +52,14 @@ typedef struct {
} bionic_condattr_t;
typedef int bionic_key_t;
-
_Static_assert(sizeof(bionic_key_t) == sizeof(pthread_key_t), "bionic_key_t and pthread_key_t size mismatch");
+typedef int bionic_pthread_once_t;
+_Static_assert(sizeof(bionic_pthread_once_t) == sizeof(pthread_once_t), "bionic_pthread_once_t and pthread_once_t size mismatch");
+
+typedef long bionic_pthread_t;
+_Static_assert(sizeof(bionic_pthread_t) == sizeof(pthread_t), "bionic_pthread_t and pthread_t size mismatch");
+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#define IS_MAPPED(x) is_mapped(x->glibc, sizeof(x))
#define INIT_IF_NOT_MAPPED(x, init) do { if (!IS_MAPPED(x)) init(x); } while(0)
@@ -102,6 +107,23 @@ bionic_pthread_mutexattr_init(bionic_mutexattr_t *attr)
return pthread_mutexattr_init(attr->glibc);
}
+static void
+default_pthread_mutex_init(bionic_mutex_t *mutex)
+{
+ assert(mutex && !IS_MAPPED(mutex));
+ mutex->glibc = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+
+ for (size_t i = 0; i < ARRAY_SIZE(bionic_mutex_init_map); ++i) {
+ if (!memcmp(&bionic_mutex_init_map[i].bionic, mutex, sizeof(*mutex)))
+ continue;
+
+ memcpy(mutex->glibc, &bionic_mutex_init_map[i].glibc, sizeof(bionic_mutex_init_map[i].glibc));
+ return;
+ }
+
+ assert(0 && "no such default initializer???");
+}
+
int
bionic_pthread_mutex_destroy(bionic_mutex_t *mutex)
{
@@ -123,29 +145,28 @@ bionic_pthread_mutex_init(bionic_mutex_t *mutex, const bionic_mutexattr_t *attr)
return pthread_mutex_init(mutex->glibc, (attr ? attr->glibc : NULL));
}
-static void
-default_pthread_mutex_init(bionic_mutex_t *mutex)
+int
+bionic_pthread_mutex_lock(bionic_mutex_t *mutex)
{
- assert(mutex && !IS_MAPPED(mutex));
- mutex->glibc = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
-
- for (size_t i = 0; i < ARRAY_SIZE(bionic_mutex_init_map); ++i) {
- if (!memcmp(&bionic_mutex_init_map[i].bionic, mutex, sizeof(*mutex)))
- continue;
-
- memcpy(mutex->glibc, &bionic_mutex_init_map[i].glibc, sizeof(bionic_mutex_init_map[i].glibc));
- return;
- }
-
- assert(0 && "no such default initializer???");
+ assert(mutex);
+ INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
+ return pthread_mutex_lock(mutex->glibc);
}
int
-bionic_pthread_mutex_lock(bionic_mutex_t *mutex)
+bionic_pthread_mutex_unlock(bionic_mutex_t *mutex)
{
assert(mutex);
INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init);
- return pthread_mutex_lock(mutex->glibc);
+ return pthread_mutex_unlock(mutex->glibc);
+}
+
+static void
+default_pthread_cond_init(bionic_cond_t *cond)
+{
+ assert(cond && !IS_MAPPED(cond));
+ cond->glibc = mmap(NULL, sizeof(pthread_cond_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+ memset(cond->glibc, 0, sizeof(pthread_cond_t));
}
int
@@ -169,10 +190,10 @@ bionic_pthread_cond_init(bionic_cond_t *cond, const bionic_condattr_t *attr)
return pthread_cond_init(cond->glibc, (attr ? attr->glibc : NULL));
}
-static void
-default_pthread_cond_init(bionic_cond_t *cond)
+int
+bionic_pthread_cond_signal(bionic_cond_t *cond)
{
- assert(cond && !IS_MAPPED(cond));
- cond->glibc = mmap(NULL, sizeof(pthread_cond_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
- memset(cond->glibc, 0, sizeof(pthread_cond_t));
+ assert(cond);
+ INIT_IF_NOT_MAPPED(cond, default_pthread_cond_init);
+ return pthread_cond_signal(cond->glibc);
}