[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] concurrency-libtask fec6faa: Fix pthread implementation
From: |
Philipp Stephani |
Subject: |
[Emacs-diffs] concurrency-libtask fec6faa: Fix pthread implementation |
Date: |
Sun, 30 Oct 2016 19:47:10 +0000 (UTC) |
branch: concurrency-libtask
commit fec6faa72a9fcc44286057b1abe8153448a98493
Author: Philipp Stephani <address@hidden>
Commit: Philipp Stephani <address@hidden>
Fix pthread implementation
---
lib/libtask/context.c | 25 ++++++++++---------------
lib/libtask/task.c | 34 +++++++++++++++++++++++++++++-----
lib/libtask/taskimpl.h | 2 +-
3 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/lib/libtask/context.c b/lib/libtask/context.c
index fcb27d8..a8207b8 100644
--- a/lib/libtask/context.c
+++ b/lib/libtask/context.c
@@ -162,26 +162,21 @@ swapcontext (ucontext_t *oucp, const ucontext_t *ucp)
#ifdef LIBTASK_USE_PTHREAD
#include <pthread.h>
-static bool oucp_is_valid = false;
int
swapcontext (ucontext_t *oucp, /* const */ ucontext_t *ucp)
{
- oucp->running = false;
+ if (pthread_mutex_lock (&ucp->mutex) != 0) abort ();
ucp->running = true;
- pthread_mutex_lock (&ucp->mutex);
- pthread_cond_signal (&ucp->cond);
- pthread_mutex_unlock (&ucp->mutex);
- if (! oucp_is_valid)
- {
- pthread_mutex_init (&oucp->mutex, NULL);
- pthread_cond_init (&oucp->cond, NULL);
- oucp->thread = pthread_self ();
- oucp_is_valid = true;
- }
- pthread_mutex_lock (&oucp->mutex);
+ if (pthread_cond_signal (&ucp->cond) != 0) abort ();
+ if (pthread_mutex_unlock (&ucp->mutex) != 0) abort ();
+ if (pthread_mutex_lock (&oucp->mutex) != 0) abort ();
+ oucp->running = false;
while (! oucp->running)
- pthread_cond_wait (&oucp->cond, &oucp->mutex);
- pthread_mutex_unlock (&oucp->mutex);
+ {
+ if (pthread_cond_wait (&oucp->cond, &oucp->mutex) != 0) abort ();
+ }
+ if (pthread_mutex_unlock (&oucp->mutex) != 0) abort ();
+ return 0;
}
#endif
diff --git a/lib/libtask/task.c b/lib/libtask/task.c
index 4d68e7e..fce2a14 100644
--- a/lib/libtask/task.c
+++ b/lib/libtask/task.c
@@ -92,10 +92,12 @@ thread_func (void *arg)
{
Task *t = arg;
ucontext_t *uc = &t->context.uc;
- pthread_mutex_lock (&uc->mutex);
+ if (pthread_mutex_lock (&uc->mutex) != 0) abort ();
while (! uc->running)
- pthread_cond_wait (&uc->cond, &uc->mutex);
- pthread_mutex_unlock (&uc->mutex);
+ {
+ if (pthread_cond_wait (&uc->cond, &uc->mutex) != 0) abort ();
+ }
+ if (pthread_mutex_unlock (&uc->mutex) != 0) abort ();
t->startfn (t->startarg);
return NULL;
}
@@ -132,8 +134,22 @@ taskalloc(void (*fn)(void*), void *arg, uint stack)
abort ();
if (pthread_cond_init (&t->context.uc.cond, NULL) != 0)
abort ();
- if (pthread_create (&t->context.uc.thread, NULL, thread_func, t) != 0)
- abort ();
+ {
+ pthread_attr_t attr;
+ if (pthread_attr_init (&attr) != 0)
+ abort ();
+ if (stack < PTHREAD_STACK_MIN)
+ stack = PTHREAD_STACK_MIN;
+ long pagesize = sysconf (_SC_PAGESIZE);
+ if (stack % pagesize != 0)
+ stack += pagesize - stack % pagesize;
+ if (pthread_attr_setstacksize (&attr, stack) != 0)
+ abort ();
+ if (pthread_create (&t->context.uc.thread, &attr, thread_func, t) !=
0)
+ abort ();
+ if (pthread_attr_destroy (&attr) != 0)
+ abort ();
+ }
#else
t->stk = (uchar*)(t+1);
t->stksize = stack;
@@ -283,6 +299,14 @@ taskscheduler(void)
int i;
Task *t;
+#ifdef LIBTASK_USE_PTHREAD
+ memset (&taskschedcontext, 0, sizeof taskschedcontext);
+ if (pthread_mutex_init (&taskschedcontext.uc.mutex, NULL) != 0) abort
();
+ if (pthread_cond_init (&taskschedcontext.uc.cond, NULL) != 0) abort ();
+ taskschedcontext.uc.thread = pthread_self ();
+ taskschedcontext.uc.running = true;
+#endif
+
taskdebug("scheduler enter");
for(;;){
if(taskcount == 0)
diff --git a/lib/libtask/taskimpl.h b/lib/libtask/taskimpl.h
index 0d23373..3cd7013 100644
--- a/lib/libtask/taskimpl.h
+++ b/lib/libtask/taskimpl.h
@@ -30,7 +30,7 @@
#ifdef WINDOWSNT
#define LIBTASK_USE_FIBER
#else
-//#define LIBTASK_USE_PTHREAD
+#define LIBTASK_USE_PTHREAD
#endif
#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] concurrency-libtask fec6faa: Fix pthread implementation,
Philipp Stephani <=