5fd1486ce5
2017-01-24 Pekka Jääskeläinen <pekka@parmance.com> Martin Jambor <mjambor@suse.cz> * Makefile.def (target_modules): Added libhsail-rt. (languages): Added language brig. * Makefile.in: Regenerated. * configure.ac (TOPLEVEL_CONFIGURE_ARGUMENTS): Added tgarget-libhsail-rt. Make brig unsupported on untested architectures. * configure: Regenerated. gcc/ * brig-builtins.def: New file. * builtins.def (DEF_HSAIL_BUILTIN): New macro. (DEF_HSAIL_ATOMIC_BUILTIN): Likewise. (DEF_HSAIL_SAT_BUILTIN): Likewise. (DEF_HSAIL_INTR_BUILTIN): Likewise. (DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN): Likewise. * builtin-types.def (BT_INT8): New. (BT_INT16): Likewise. (BT_UINT8): Likewise. (BT_UINT16): Likewise. (BT_FN_ULONG): Likewise. (BT_FN_UINT_INT): Likewise. (BT_FN_UINT_ULONG): Likewise. (BT_FN_UINT_LONG): Likewise. (BT_FN_UINT_PTR): Likewise. (BT_FN_ULONG_PTR): Likewise. (BT_FN_INT8_FLOAT): Likewise. (BT_FN_INT16_FLOAT): Likewise. (BT_FN_UINT32_FLOAT): Likewise. (BT_FN_UINT16_FLOAT): Likewise. (BT_FN_UINT8_FLOAT): Likewise. (BT_FN_UINT64_FLOAT): Likewise. (BT_FN_UINT16_UINT32): Likewise. (BT_FN_UINT32_UINT16): Likewise. (BT_FN_UINT16_UINT16_UINT16): Likewise. (BT_FN_INT_PTR_INT): Likewise. (BT_FN_UINT_PTR_UINT): Likewise. (BT_FN_LONG_PTR_LONG): Likewise. (BT_FN_ULONG_PTR_ULONG): Likewise. (BT_FN_VOID_UINT64_UINT64): Likewise. (BT_FN_UINT8_UINT8_UINT8): Likewise. (BT_FN_INT8_INT8_INT8): Likewise. (BT_FN_INT16_INT16_INT16): Likewise. (BT_FN_INT_INT_INT): Likewise. (BT_FN_UINT_FLOAT_UINT): Likewise. (BT_FN_FLOAT_UINT_UINT): Likewise. (BT_FN_ULONG_UINT_UINT): Likewise. (BT_FN_ULONG_UINT_PTR): Likewise. (BT_FN_ULONG_ULONG_ULONG): Likewise. (BT_FN_UINT_UINT_UINT): Likewise. (BT_FN_VOID_UINT_PTR): Likewise. (BT_FN_UINT_UINT_PTR: Likewise. (BT_FN_UINT32_UINT64_PTR): Likewise. (BT_FN_INT_INT_UINT_UINT): Likewise. (BT_FN_UINT_UINT_UINT_UINT): Likewise. (BT_FN_UINT_UINT_UINT_PTR): Likewise. (BT_FN_UINT_ULONG_ULONG_UINT): Likewise. (BT_FN_ULONG_ULONG_ULONG_ULONG): Likewise. (BT_FN_LONG_LONG_UINT_UINT): Likewise. (BT_FN_ULONG_ULONG_UINT_UINT): Likewise. (BT_FN_VOID_UINT32_UINT64_PTR): Likewise. (BT_FN_VOID_UINT32_UINT32_PTR): Likewise. (BT_FN_UINT_UINT_UINT_UINT_UINT): Likewise. (BT_FN_UINT_FLOAT_FLOAT_FLOAT_FLOAT): Likewise. (BT_FN_ULONG_ULONG_ULONG_UINT_UINT): Likewise. * doc/frontends.texi: List BRIG FE. * doc/install.texi (Testing): Add BRIG tesring requirements. * doc/invoke.texi (Overall Options): Mention BRIG. * doc/standards.texi (Standards): Doucment BRIG HSA version. gcc/brig/ * Make-lang.in: New file. * brig-builtins.h: Likewise. * brig-c.h: Likewise. * brig-lang.c: Likewise. * brigspec.c: Likewise. * config-lang.in: Likewise. * lang-specs.h: Likewise. * lang.opt: Likewise. * brigfrontend/brig-arg-block-handler.cc: Likewise. * brigfrontend/brig-atomic-inst-handler.cc: Likewise. * brigfrontend/brig-basic-inst-handler.cc: Likewise. * brigfrontend/brig-branch-inst-handler.cc: Likewise. * brigfrontend/brig-cmp-inst-handler.cc: Likewise. * brigfrontend/brig-code-entry-handler.cc: Likewise. * brigfrontend/brig-code-entry-handler.h: Likewise. * brigfrontend/brig-comment-handler.cc: Likewise. * brigfrontend/brig-control-handler.cc: Likewise. * brigfrontend/brig-copy-move-inst-handler.cc: Likewise. * brigfrontend/brig-cvt-inst-handler.cc: Likewise. * brigfrontend/brig-fbarrier-handler.cc: Likewise. * brigfrontend/brig-function-handler.cc: Likewise. * brigfrontend/brig-function.cc: Likewise. * brigfrontend/brig-function.h: Likewise. * brigfrontend/brig-inst-mod-handler.cc: Likewise. * brigfrontend/brig-label-handler.cc: Likewise. * brigfrontend/brig-lane-inst-handler.cc: Likewise. * brigfrontend/brig-machine.c: Likewise. * brigfrontend/brig-machine.h: Likewise. * brigfrontend/brig-mem-inst-handler.cc: Likewise. * brigfrontend/brig-module-handler.cc: Likewise. * brigfrontend/brig-queue-inst-handler.cc: Likewise. * brigfrontend/brig-seg-inst-handler.cc: Likewise. * brigfrontend/brig-signal-inst-handler.cc: Likewise. * brigfrontend/brig-to-generic.cc: Likewise. * brigfrontend/brig-to-generic.h: Likewise. * brigfrontend/brig-util.cc: Likewise. * brigfrontend/brig-util.h: Likewise. * brigfrontend/brig-variable-handler.cc: Likewise. * brigfrontend/phsa.h: Likewise. gcc/testsuite/ * lib/brig-dg.exp: New file. * lib/brig.exp: Likewise. * brig.dg/README: Likewise. * brig.dg/dg.exp: Likewise. * brig.dg/test/gimple/alloca.hsail: Likewise. * brig.dg/test/gimple/atomics.hsail: Likewise. * brig.dg/test/gimple/branches.hsail: Likewise. * brig.dg/test/gimple/fbarrier.hsail: Likewise. * brig.dg/test/gimple/function_calls.hsail: Likewise. * brig.dg/test/gimple/kernarg.hsail: Likewise. * brig.dg/test/gimple/mem.hsail: Likewise. * brig.dg/test/gimple/mulhi.hsail: Likewise. * brig.dg/test/gimple/packed.hsail: Likewise. * brig.dg/test/gimple/smoke_test.hsail: Likewise. * brig.dg/test/gimple/variables.hsail: Likewise. * brig.dg/test/gimple/vector.hsail: Likewise. include/ * hsa.h: Moved here from libgomp/plugin/hsa.h. libgomp/ * plugin/hsa.h: Moved to top level include. * plugin/plugin-hsa.c: Chanfgd include of hsa.h accordingly. libhsail-rt/ * Makefile.am: New file. * target-config.h.in: Likewise. * configure.ac: Likewise. * configure: Likewise. * config.h.in: Likewise. * aclocal.m4: Likewise. * README: Likewise. * Makefile.in: Likewise. * include/internal/fibers.h: Likewise. * include/internal/phsa-queue-interface.h: Likewise. * include/internal/phsa-rt.h: Likewise. * include/internal/workitems.h: Likewise. * rt/arithmetic.c: Likewise. * rt/atomics.c: Likewise. * rt/bitstring.c: Likewise. * rt/fbarrier.c: Likewise. * rt/fibers.c: Likewise. * rt/fp16.c: Likewise. * rt/misc.c: Likewise. * rt/multimedia.c: Likewise. * rt/queue.c: Likewise. * rt/sat_arithmetic.c: Likewise. * rt/segment.c: Likewise. * rt/workitems.c: Likewise. Co-Authored-By: Martin Jambor <mjambor@suse.cz> From-SVN: r244867
221 lines
6.5 KiB
C
221 lines
6.5 KiB
C
/* fibers.c -- extremely simple lightweight thread (fiber) implementation
|
|
Copyright (C) 2016 Free Software Foundation, Inc.
|
|
Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
|
|
for General Processor Tech.
|
|
|
|
Copyright (C) 2015-2016 Free Software Foundation, Inc.
|
|
Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
|
|
for General Processor Tech.
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
|
copy of this software and associated documentation files
|
|
(the "Software"), to deal in the Software without restriction, including
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
|
|
#include "target-config.h"
|
|
|
|
#include "fibers.h"
|
|
|
|
void
|
|
phsa_fatal_error (int code);
|
|
|
|
ucontext_t main_context;
|
|
|
|
/* The last fiber in the linked list. */
|
|
static fiber_t *tail_fiber = NULL;
|
|
/* The first fiber in the linked list. */
|
|
static fiber_t *head_fiber = NULL;
|
|
/* The fiber currently being executed. */
|
|
static fiber_t *current_fiber = NULL;
|
|
|
|
/* Makecontext accepts only integer arguments. We need to split the
|
|
pointer argument in case pointer does not fit into int. This helper
|
|
function can be used to restore the pointer from the arguments. */
|
|
|
|
void *
|
|
fiber_int_args_to_ptr (int arg0, int arg1)
|
|
{
|
|
void *ptr = NULL;
|
|
#if SIZEOF_VOIDP == 8 && SIZEOF_INT == 4
|
|
ptr = (void*)(((uint64_t) arg0 & (uint64_t) 0xFFFFFFFF)
|
|
| ((uint64_t) arg1 << 32));
|
|
#elif SIZEOF_VOIDP == 4 && SIZEOF_INT == 4
|
|
ptr = (void*)arg0;
|
|
#else
|
|
# error Unsupported pointer/int size.
|
|
#endif
|
|
return ptr;
|
|
}
|
|
|
|
void
|
|
fiber_init (fiber_t *fiber, fiber_function_t start_function, void *arg,
|
|
size_t stack_size, size_t stack_align)
|
|
{
|
|
int arg0, arg1;
|
|
if (getcontext (&fiber->context) != 0)
|
|
phsa_fatal_error (3);
|
|
if (posix_memalign (&fiber->context.uc_stack.ss_sp, stack_align, stack_size)
|
|
!= 0)
|
|
phsa_fatal_error (4);
|
|
fiber->context.uc_stack.ss_size = stack_size;
|
|
fiber->context.uc_link = &main_context;
|
|
|
|
/* makecontext () accepts only integer arguments. Split the
|
|
pointer argument to two args in the case pointer does not fit
|
|
into one int. */
|
|
#if SIZEOF_VOIDP == 8 && SIZEOF_INT == 4
|
|
arg0 = (int32_t) 0xFFFFFFFF & (uint64_t)arg;
|
|
arg1 = (int32_t) 0xFFFFFFFF & ((uint64_t)arg >> 32);
|
|
#elif SIZEOF_VOIDP == 4 && SIZEOF_INT == 4
|
|
arg0 = (int)arg;
|
|
arg1 = 0;
|
|
#else
|
|
# error Unsupported pointer/int size.
|
|
#endif
|
|
|
|
makecontext (&fiber->context, (void*)start_function, 2, arg0, arg1);
|
|
|
|
fiber->status = FIBER_STATUS_READY;
|
|
fiber->next = NULL;
|
|
fiber->prev = NULL;
|
|
|
|
/* Create a linked list of the created fibers. Append the new one at
|
|
the end. */
|
|
if (tail_fiber == NULL)
|
|
tail_fiber = fiber;
|
|
else
|
|
{
|
|
tail_fiber->next = fiber;
|
|
fiber->prev = tail_fiber;
|
|
tail_fiber = fiber;
|
|
}
|
|
|
|
if (head_fiber == NULL)
|
|
head_fiber = fiber;
|
|
}
|
|
|
|
void
|
|
fiber_exit ()
|
|
{
|
|
fiber_status_t old_status = current_fiber->status;
|
|
current_fiber->status = FIBER_STATUS_EXITED;
|
|
if (old_status == FIBER_STATUS_JOINED)
|
|
/* In case this thread has been joined, return back to the joiner. */
|
|
swapcontext (¤t_fiber->context, &main_context);
|
|
else
|
|
/* In case the thread exited while being yielded from another thread,
|
|
switch back to another fiber. */
|
|
fiber_yield ();
|
|
}
|
|
|
|
void
|
|
fiber_join (fiber_t *fiber)
|
|
{
|
|
fiber_t *next_ready_fiber = NULL;
|
|
current_fiber = fiber;
|
|
if (fiber->status != FIBER_STATUS_EXITED)
|
|
{
|
|
fiber->status = FIBER_STATUS_JOINED;
|
|
while (fiber->status != FIBER_STATUS_EXITED)
|
|
swapcontext (&main_context, &fiber->context);
|
|
}
|
|
|
|
/* Remove the successfully joined fiber from the linked list so we won't
|
|
access it later (the fiber itself might be freed after the join). */
|
|
if (fiber->prev != NULL)
|
|
fiber->prev->next = fiber->next;
|
|
|
|
if (fiber->next != NULL)
|
|
fiber->next->prev = fiber->prev;
|
|
|
|
if (head_fiber == fiber)
|
|
head_fiber = fiber->next;
|
|
|
|
if (tail_fiber == fiber)
|
|
tail_fiber = fiber->prev;
|
|
|
|
free (fiber->context.uc_stack.ss_sp);
|
|
}
|
|
|
|
void
|
|
fiber_yield ()
|
|
{
|
|
fiber_t *next_ready_fiber = current_fiber;
|
|
|
|
if (current_fiber == head_fiber
|
|
&& current_fiber == tail_fiber)
|
|
{
|
|
/* If the last fiber exits independently, there is no
|
|
fiber to switch to. Switch to the main context in that
|
|
case. */
|
|
if (current_fiber->status == FIBER_STATUS_EXITED)
|
|
swapcontext (¤t_fiber->context, &main_context);
|
|
}
|
|
|
|
do {
|
|
next_ready_fiber = next_ready_fiber->next != NULL
|
|
? next_ready_fiber->next : head_fiber;
|
|
} while (next_ready_fiber != current_fiber
|
|
&& next_ready_fiber->status == FIBER_STATUS_EXITED);
|
|
|
|
fiber_t *old_current_fiber = current_fiber;
|
|
current_fiber = next_ready_fiber;
|
|
swapcontext (&old_current_fiber->context, &next_ready_fiber->context);
|
|
}
|
|
|
|
size_t
|
|
fiber_barrier_reach (fiber_barrier_t *barrier)
|
|
{
|
|
/* Yield once to ensure that there are no fibers waiting for
|
|
a previous triggering of the barrier in the waiting_count
|
|
loop. This should release them before we update the reached
|
|
counter again. */
|
|
fiber_yield ();
|
|
|
|
barrier->reached++;
|
|
++barrier->waiting_count;
|
|
while (barrier->reached < barrier->threshold)
|
|
fiber_yield ();
|
|
--barrier->waiting_count;
|
|
|
|
/* Wait until all the fibers have reached this point. */
|
|
while (barrier->waiting_count > 0)
|
|
fiber_yield ();
|
|
|
|
/* Now all fibers have been released from the barrier waiting
|
|
loop. We can now safely reset the reach count for new triggering. */
|
|
if (barrier->reached > 0)
|
|
{
|
|
barrier->reached = 0;
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void
|
|
fiber_barrier_init (fiber_barrier_t *barrier, size_t threshold)
|
|
{
|
|
barrier->threshold = threshold;
|
|
barrier->waiting_count = 0;
|
|
barrier->reached = 0;
|
|
}
|