Example #1
0
void
pymatecorba_register_stub(CORBA_TypeCode tc, PyObject *stub)
{
    init_hash_tables();

    if (tc->repo_id) {
        CORBA_Object_duplicate((CORBA_Object)tc, NULL);
        g_hash_table_replace(type_codes, tc->repo_id, tc);
    }

    if (stub) {
        PyObject *stub_dict = NULL;
        Py_INCREF(stub);
        g_hash_table_insert(stubs, tc->repo_id, stub);

        if (!strncmp(tc->repo_id, "IDL:omg.org/CORBA", 17)) {
            gchar *other_repo_id = g_strconcat("IDL:", &tc->repo_id[12], NULL);

            g_hash_table_insert(stubs, other_repo_id, stub);
        }

        if (PyType_Check(stub))
            stub_dict = ((PyTypeObject *)stub)->tp_dict;
        else if (PyClass_Check(stub))
            stub_dict = ((PyClassObject *)stub)->cl_dict;

        if (stub_dict && !PyDict_GetItemString(stub_dict, "__typecode__")) {
            PyObject *py_tc = pycorba_typecode_new(tc);

            PyDict_SetItemString(stub_dict, "__typecode__", py_tc);
            Py_DECREF(py_tc);
        }
    }
}
Example #2
0
PyObject *
pymatecorba_get_stub_from_repo_id(const gchar *repo_id)
{
    init_hash_tables();

    if (repo_id == NULL) return NULL;
    return g_hash_table_lookup(stubs, repo_id);
}
Example #3
0
void ThreadsManager::init() {

  int threadID[MAX_THREADS];

  // This flag is needed to properly end the threads when program exits
  allThreadsShouldExit = false;

  // Threads will sent to sleep as soon as created, only main thread is kept alive
  activeThreads = 1;
  threads[0].state = Thread::SEARCHING;

  // Allocate pawn and material hash tables for main thread
  init_hash_tables();

  lock_init(&mpLock);

  // Initialize thread and split point locks
  for (int i = 0; i < MAX_THREADS; i++)
  {
      lock_init(&threads[i].sleepLock);
      cond_init(&threads[i].sleepCond);

      for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
          lock_init(&(threads[i].splitPoints[j].lock));
  }

  // Create and startup all the threads but the main that is already running
  for (int i = 1; i < MAX_THREADS; i++)
  {
      threads[i].state = Thread::INITIALIZING;
      threadID[i] = i;

#if defined(_MSC_VER)
      bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threadID[i], 0, NULL) != NULL);
#else
      pthread_t pthreadID;
      bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threadID[i]) == 0);
      pthread_detach(pthreadID);
#endif
      if (!ok)
      {
          std::cout << "Failed to create thread number " << i << std::endl;
          ::exit(EXIT_FAILURE);
      }

      // Wait until the thread has finished launching and is gone to sleep
      while (threads[i].state == Thread::INITIALIZING) {}
  }
}
Example #4
0
PyObject *
pymatecorba_get_stub(CORBA_TypeCode tc)
{
    PyObject *stub;

    init_hash_tables();

    if (!tc->repo_id) return NULL;

    stub = g_hash_table_lookup(stubs, tc->repo_id);
    /* if we didn't get a typecode, and the repoid is in the type_codes
     * hash, try generating a stub. */
    if (!stub && tc->repo_id && g_hash_table_lookup(type_codes, tc->repo_id) == NULL) {
        pymatecorba_generate_typecode_stubs(tc);
        stub = g_hash_table_lookup(stubs, tc->repo_id);
    }
    return stub;
}
Example #5
0
int main (int argc, char *argv[]) {

  char input[STR_BUFF], *p, output[STR_BUFF];
  move_s move, comp_move;
  int depth = 4, comp_color;
  bool force_mode, show_board;
  double nps, elapsed;
  clock_t cpu_start = 0, cpu_end = 0;

  parse_cmdline (argc, argv);
  start_up ();
  init_hash_values ();
  init_hash_tables ();
  init_game ();
  init_book ();
  xb_mode = FALSE;
  force_mode = FALSE;
  comp_color = 0;
  show_board = TRUE;
  
  setbuf (stdout, NULL);
  setbuf (stdin, NULL);

  /* keep looping for input, and responding to it: */
  while (TRUE) {

    /* case where it's the computer's turn to move: */
    if (comp_color == white_to_move && !force_mode) {
      nodes = 0;
      qnodes = 0;
      ply = 0;

      start_time = rtime ();
      cpu_start = clock ();
      comp_move = think ();
      cpu_end = clock ();

      /* check for a game end: */
      if (((comp_color == 1 && result != white_is_mated) ||
	  (comp_color == 0 && result != black_is_mated)) &&
	  result != stalemate && result != draw_by_fifty &&
	  result != draw_by_rep) {
	
	comp_to_coord (comp_move, output);

	make (&comp_move, 0);

	/* check to see if we draw by rep/fifty after our move: */
	if (is_draw ()) {
	  result = draw_by_rep;
	}
	else if (fifty > 100) {
	  result = draw_by_fifty;
	}

	reset_piece_square ();
	/* check to see if we mate our opponent with our current move: */
	if (!result) {
	  if (xb_mode) {
	    printf ("move %s\n", output);
	  }
	  else {
	    printf ("\n%s\n", output);
	  }
	}
	else {
	  if (xb_mode) {
	    printf ("move %s\n", output);
	  }
	  else {
	    printf ("\n%s\n", output);
	  }
	  if (result == white_is_mated) {
	    printf ("0-1 {Black Mates}\n");
	  }
	  else if (result == black_is_mated) {
	    printf ("1-0 {White Mates}\n");
	  }
	  else if (result == draw_by_fifty) {
	    printf ("1/2-1/2 {Fifty move rule}\n");
	  }
	  else if (result == draw_by_rep) {
	    printf ("1/2-1/2 {3 fold repetition}\n");
	  }
	  else {
	    printf ("1/2-1/2 {Draw}\n");
	  }
	}
      }
      /* we have been mated or there is a draw: */
      else {
	if (result == white_is_mated) {
	  printf ("0-1 {Black Mates}\n");
	}
	else if (result == black_is_mated) {
	  printf ("1-0 {White Mates}\n");
	}
	else if (result == stalemate) {
	  printf ("1/2-1/2 {Stalemate}\n");
	}
	else if (result == draw_by_fifty) {
	  printf ("1/2-1/2 {Fifty move rule}\n");
	}
	else if (result == draw_by_rep) {
	  printf ("1/2-1/2 {3 fold repetition}\n");
	}
	else {
	  printf ("1/2-1/2 {Draw}\n");
	}
      }

    }

    /* get our input: */
    if (!xb_mode) {
      if (show_board && strcmp (input, "help")) {
	printf ("\n");
	display_board (stdout, 1-comp_color);
      }
      printf ("Faile> ");
      rinput (input, STR_BUFF, stdin);
    }
    else {
      rinput (input, STR_BUFF, stdin);
    }

    /* check to see if we have a move.  If it's legal, play it. */
    if (is_valid_comp (pgn_to_comp (input))) {
      /* good SAN input style move */
      move = pgn_to_comp (input);
      make (&move, 0);
      reset_piece_square ();
      if (show_board) {
	printf ("\n");
	display_board (stdout, 1-comp_color);
      }
    }
    else if (is_move (&input[0])) {
      /* good coordinate style input move */
      if (verify_coord (input, &move)) {
	make (&move, 0);
	reset_piece_square ();
	if (show_board) {
	  printf ("\n");
	  display_board (stdout, 1-comp_color);
	}
      }
      else {
	printf ("Illegal move: %s\n", input);
      }
    }
    else {

      /* make everything lower case for convenience: */
      for (p = input; *p; p++) *p = tolower (*p);

      /* command parsing: */
      if (!strcmp (input, "quit") || !strcmp (input, "exit")) {
	shut_down (EXIT_SUCCESS);
      }
      else if (!strcmp (input, "diagram") || !strcmp (input, "d")) {
	toggle_bool (&show_board);
      }
      else if (!strncmp (input, "perft", 5)) {
	sscanf (input+6, "%d", &depth);
	raw_nodes = 0;
	perft (depth);
	printf ("Raw nodes for depth %d: %ld\n", depth, raw_nodes);
      }
      else if (!strcmp (input, "new")) {
	init_game ();
	/* refresh our hash tables: */
	refresh_hash ();
	force_mode = FALSE;
	comp_color = 0;
      }
      else if (!strcmp (input, "xboard")) {
	xb_mode = TRUE;
	toggle_bool (&show_board);
	signal (SIGINT, SIG_IGN);
	printf ("\n");
      }
      else if (!strcmp (input, "nodes")) {
	printf ("Number of nodes: %li (%0.2f%% qnodes)\n", nodes,
		(float) ((float) qnodes / (float) nodes * 100.0));
      }
      else if (!strcmp (input, "nps")) {
	elapsed = (cpu_end-cpu_start)/(double) CLOCKS_PER_SEC;
	nps = (float) nodes/(float) elapsed;
	if (!elapsed)
	  printf ("NPS: N/A\n");
	else
	  printf ("NPS: %ld\n", (long int) nps);
      }
      else if (!strcmp (input, "post")) {
	toggle_bool (&post);
	if (xb_mode)
	  post = TRUE;
      }
      else if (!strcmp (input, "nopost")) {
	post = FALSE;
      }
      else if (!strcmp (input, "random")) {
	continue;
      }
      else if (!strcmp (input, "hard")) {
	continue;
      }
      else if (!strcmp (input, "easy")) {
	continue;
      }
      else if (!strcmp (input, "?")) {
	continue;
      }
      else if (!strcmp (input, "white")) {
	white_to_move = 1;
	comp_color = 0;
      }
      else if (!strcmp (input, "black")) {
	white_to_move = 0;
	comp_color = 1;
      }
      else if (!strcmp (input, "force")) {
	force_mode = TRUE;
      }
      else if (!strcmp (input, "go")) {
	comp_color = white_to_move;
	force_mode = FALSE;
      }
      else if (!strncmp (input, "time", 4)) {
	sscanf (input+5, "%ld", &time_left);
      }
      else if (!strncmp (input, "otim", 4)) {
	sscanf (input+5, "%ld", &opp_time);
      }
      else if (!strncmp (input, "level", 5)) {
	/* extract the time controls: */
	sscanf (input+6, "%ld %ld %ld", &moves_to_tc, &min_per_game, &inc);
	time_left = min_per_game*6000;
	opp_time = time_left;
	inc *= 100;
      }
      else if (!strncmp (input, "result", 6)) {
	ics_game_end ();
	init_game ();
	force_mode = FALSE;
	comp_color = 0;
      }
      else if (!strcmp (input, "help")) {
	printf ("\n%s\n\n", divider);
	printf ("diagram/d: toggle diagram display\n");
	printf ("exit/quit: terminate Faile\n");
	printf ("go:        make Faile play the side to move\n");
	printf ("new:       start a new game\n");
	printf ("level <x>: the xboard style command to set time\n");
	printf ("  <x> should be in the form: <a> <b> <c> where:\n");
	printf ("  a -> moves to TC (0 if using an ICS style TC)\n");
	printf ("  b -> minutes per game\n");
	printf ("  c -> increment in seconds\n");
	printf ("nodes:     outputs the number of nodes searched\n");
	printf ("nps:       outputs Faile's NPS in search\n");
	printf ("perft <x>: compute raw nodes to depth x\n");
	printf ("post:      toggles thinking output\n");
	printf ("xboard:    put Faile into xboard mode\n");
	printf ("\n%s\n\n", divider);
      }
      else if (!xb_mode) {
	printf ("Illegal move: %s\n", input);
      }

    }

  }

  return 0;

}
Example #6
0
void
pymatecorba_generate_iinterface_stubs(MateCORBA_IInterface *iface)
{
    CORBA_TypeCode tc;
    PyObject *stub, *bases, *class_dict, *slots;
    PyObject **base_list;
    gint i, j, n_bases;

    init_hash_tables();

    tc = iface->tc;
    /* has wrapper already been generated? */
    if (g_hash_table_lookup(stubs, tc->repo_id))
        return;

    /* create bases tuple */
    base_list = g_new(PyObject *, iface->base_interfaces._length);
    for (i = 0; i < iface->base_interfaces._length; i++) {
        const gchar *base_repo_id = iface->base_interfaces._buffer[i];
        PyObject *base = pymatecorba_get_stub_from_repo_id(base_repo_id);

        /* if we haven't wrapped the base, try and look it up */
        if (!base) {
            MateCORBA_IInterface *base_iface;
            CORBA_Environment ev;

            CORBA_exception_init(&ev);
            base_iface = MateCORBA_small_get_iinterface(CORBA_OBJECT_NIL,
                         base_repo_id, &ev);
            if (ev._major != CORBA_NO_EXCEPTION) {
                g_warning("repo id for base %s has not been registered",
                          base_repo_id);
                CORBA_exception_free(&ev);
                for (j = 0; j < i; j++) Py_DECREF(base_list[j]);
                g_free(base_list);
                return;
            }
            CORBA_exception_free(&ev);

            /* generate interface, then get it */
            pymatecorba_generate_iinterface_stubs(base_iface);
            base = pymatecorba_get_stub_from_repo_id(base_repo_id);
            if (!base) {
                g_warning("could not generate stub for base %s", base_repo_id);
                for (j = 0; j < i; j++) Py_DECREF(base_list[j]);
                g_free(base_list);
                return;
            }
        }
        Py_INCREF(base);
        base_list[i] = base;
    }

    /* get rid of unneeded bases  */
    n_bases = iface->base_interfaces._length;
    for (i = 0; i < iface->base_interfaces._length; i++) {
        for (j = 0; j < iface->base_interfaces._length; j++) {
            if (i == j) continue;
            /* if base[j] is a subclass of base[i], we can ignore [i] */
            if (base_list[j] &&
                    PyType_IsSubtype((PyTypeObject *)base_list[j],
                                     (PyTypeObject *)base_list[i])) {
                Py_DECREF(base_list[i]);
                base_list[i] = NULL;
                n_bases--;
                break;
            }
        }
    }
    bases = PyTuple_New(n_bases);
    n_bases = 0;
    for (i = 0; i < iface->base_interfaces._length; i++) {
        if (base_list[i])
            PyTuple_SetItem(bases, n_bases++, base_list[i]);
    }
    g_free(base_list);

    class_dict = PyDict_New();
    slots = PyTuple_New(0);
    PyDict_SetItemString(class_dict, "__slots__", slots);
    Py_DECREF(slots);
    stub = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
                                 tc->name, bases, class_dict);
    Py_DECREF(bases);
    Py_DECREF(class_dict);
    if (!stub) {
        g_message("couldn't build stub %s:", tc->name);
        PyErr_Print();
        PyErr_Clear();
        return;
    }

    pymatecorba_add_imethods_to_stub(stub, &iface->methods);

    add_stub_to_container(tc, tc->name, stub);

    pymatecorba_register_stub(tc, stub);
}
Example #7
0
void
pymatecorba_generate_typecode_stubs(CORBA_TypeCode tc)
{
    PyObject *stub = NULL;

    init_hash_tables();

    switch (tc->kind) {
    case CORBA_tk_null:
    case CORBA_tk_void:
    case CORBA_tk_short:
    case CORBA_tk_long:
    case CORBA_tk_ushort:
    case CORBA_tk_ulong:
    case CORBA_tk_float:
    case CORBA_tk_double:
    case CORBA_tk_boolean:
    case CORBA_tk_char:
    case CORBA_tk_octet:
    case CORBA_tk_any:
    case CORBA_tk_TypeCode:
    case CORBA_tk_Principal:
        break;
    case CORBA_tk_objref:
        break;
    case CORBA_tk_struct:
        stub = generate_struct_stub(tc);
        break;
    case CORBA_tk_union:
        stub = generate_union_stub(tc);
        break;
    case CORBA_tk_enum:
        stub = generate_enum_stub(tc);
        break;
    case CORBA_tk_string:
    case CORBA_tk_sequence:
    case CORBA_tk_array:
        break;
    case CORBA_tk_alias:
        stub = pymatecorba_get_stub(tc->subtypes[0]);
        break;
    case CORBA_tk_except:
        stub = generate_exception_stub(tc);
        break;
    case CORBA_tk_longlong:
    case CORBA_tk_ulonglong:
    case CORBA_tk_longdouble:
    case CORBA_tk_wchar:
    case CORBA_tk_wstring:
    case CORBA_tk_fixed:
    case CORBA_tk_value:
    case CORBA_tk_value_box:
    case CORBA_tk_native:
    case CORBA_tk_abstract_interface:
        break;
    }

    if (stub)
        add_stub_to_container(tc, tc->name, stub);

    pymatecorba_register_stub(tc, stub);
}