/*
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "gfiber.h"

#include <ucontext.h>

struct _GFiber
{
  GObject parent_instance;

  ucontext_t context;
  ucontext_t main_context;

  gchar *stack;
  gsize stack_size;

  GAsyncResult *async_result;
};

struct _GFiberClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (GFiber, g_fiber, G_TYPE_OBJECT);

static void
g_fiber_init (GFiber *fiber)
{
}

static void
g_fiber_finalize (GObject *object)
{
  GFiber *fiber;

  fiber = G_FIBER (object);

  g_free (fiber->stack);

  G_OBJECT_CLASS (g_fiber_parent_class)->finalize (object);
}

static void
g_fiber_class_init (GFiberClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_fiber_finalize;
}

static void
run_fiber_func (GFiberFunc fiber_func, GFiber *fiber, gpointer user_data)
{
  ucontext_t main_context;

  fiber_func (fiber, user_data);

  main_context = fiber->main_context;
  g_object_unref (fiber);
  setcontext (&main_context);
}

void
g_fiber_run (GFiberFunc fiber_func, gpointer user_data)
{
  GFiber *fiber;

  fiber = G_FIBER (g_object_new (G_TYPE_FIBER, NULL));

  fiber->stack = g_malloc0 (SIGSTKSZ);
  fiber->stack_size = SIGSTKSZ;

  getcontext (&fiber->context);

  fiber->context.uc_link = 0;
  fiber->context.uc_stack.ss_sp = fiber->stack;
  fiber->context.uc_stack.ss_size = fiber->stack_size;
  fiber->context.uc_stack.ss_flags = 0;

  makecontext (&fiber->context, (void (*)(void)) run_fiber_func, 3, fiber_func, fiber, user_data);
  swapcontext (&fiber->main_context, &fiber->context);
}

static void
generic_fiber_async_cb (GObject *source_object,
                        GAsyncResult *res,
                        gpointer user_data)
{
  GFiber *fiber = G_FIBER (user_data);

  fiber->async_result = res;
  swapcontext (&fiber->main_context, &fiber->context);
}

GAsyncReadyCallback
g_fiber_get_async_ready_callback (GFiber *fiber)
{
  return generic_fiber_async_cb;
}

gpointer
g_fiber_get_async_ready_user_data (GFiber *fiber)
{
  return fiber;
}

void
g_fiber_yield (GFiber *fiber)
{
  swapcontext (&fiber->context, &fiber->main_context);
}

GAsyncResult *
g_fiber_get_async_result (GFiber *fiber)
{
  return fiber->async_result;
}

static gboolean
fiber_wakeup_func (gpointer user_data)
{
  GFiber *fiber = G_FIBER (user_data);

  swapcontext (&fiber->main_context, &fiber->context);

  return FALSE;
}

void
g_fiber_sleep (GFiber *fiber, guint msec)
{
  g_timeout_add (msec, fiber_wakeup_func, fiber);
  g_fiber_yield (fiber);
}

