summaryrefslogtreecommitdiff
path: root/lib/libtask/task.c
diff options
context:
space:
mode:
authorPhilipp Stephani <phst@google.com>2016-10-30 20:46:23 +0100
committerPhilipp Stephani <phst@google.com>2016-10-30 20:46:23 +0100
commitfec6faa72a9fcc44286057b1abe8153448a98493 (patch)
tree31a93d1d8bc0ac5120dac92e74c32da97b816a5b /lib/libtask/task.c
parent47e9d6281c56cbfa26bb88f02782288377e75c3e (diff)
downloademacs-fec6faa72a9fcc44286057b1abe8153448a98493.tar.gz
Fix pthread implementation
Diffstat (limited to 'lib/libtask/task.c')
-rw-r--r--lib/libtask/task.c34
1 files changed, 29 insertions, 5 deletions
diff --git a/lib/libtask/task.c b/lib/libtask/task.c
index 4d68e7ee272..fce2a14ed34 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)