/*
 * rtl_cc.c
 * Version 0.1
 *
 * Written by Miguel Masmano Tello <mmasmano@disca.upv.es>
 * Copyright (C) March, 2004
 * Release under the terms of the GNU General Public License Version 2
 *
 */

#include <rtl.h>
#include <rtl_sched.h>

MODULE_AUTHOR("Miguel Masmano <mmasmano@disca.upv.es>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("C++ for RTLinux");

int _GLOBAL_OFFSET_TABLE_ = 0;
static pthread_t main_thread;
extern int init_malloc(void);
extern void cleanup_malloc(void);

extern int main (void);

void *wrap_main (void *arg) {
  main();
  return (void *) 0;
}

int __do_global_ctors_aux(void);
int __do_global_dtors_aux(void);

int init_module (void) {
  pthread_attr_t attr;
  //struct sched_param param;
  if (init_malloc () != 0) return -1;
  
  pthread_attr_init(&attr);
  pthread_attr_setfp_np(&attr, 1);
  //param.sched_priority = sched_get_priority_min(SCHED_FIFO);  
  //pthread_attr_setschedparam(&attr, &param);
  
  __do_global_ctors_aux();
  
  return pthread_create (&main_thread, &attr, wrap_main, (void *) NULL);
}

static int EXITED = 0;

void cleanup_module (void) {
  if (!EXITED)
    pthread_delete_np (main_thread);
  __do_global_dtors_aux();
  cleanup_malloc ();
}

void exit (int status) {
  EXITED = 1;
  pthread_delete_np (main_thread);
}
