TACHE	Conso2(void)
{
    puts("------> Start Conso 2");
    while(1) {
        puts("------> Conso 2 : puis-je ?");
        s_wait(mySem2);
        printf("Conso 2 : j'ai mangé : %d\n",field.file[field.deb]);
        field.deb = (field.deb+1) % MAX_TACHES;
        s_signal(mySem);
        puts("------> Conso 2 : go");
    }
    fin_tache();
}
Example #2
0
void a_switch_to(void) {
    static bool not_first;

    while (not_first);

    printf("%s: Received call from Receiver\n", get_instance_name());

    /* OK, now let's proceed to try to trash our reply cap. You can more or
     * less read execution in a straight line from here as each function calls
     * the next.
     */

    printf("%s: Acquiring mutex...\n", get_instance_name());
    m_lock();

    printf("%s: Acquiring semaphore...\n", get_instance_name());
    s_wait();

    /* Trigger some more calls on the original endpoint, just in case this is
     * not correctly handled.
     */
    p1_emit();
    p2_emit();

    printf("%s: Taking first hop...\n", get_instance_name());
    b1_switch_to();

    printf("%s: Taking second hop...\n", get_instance_name());
    c1_switch_to();

    printf("%s: Taking third hop...\n", get_instance_name());
    d1_switch_to();

    printf("%s: Taking fourth (external) hop...\n", get_instance_name());
    e_switch_to();

    printf("%s: Releasing mutex...\n", get_instance_name());
    m_unlock();

    printf("%s: Releasing semaphore...\n", get_instance_name());
    s_post();

    printf("%s: Returning (moment of truth)...\n", get_instance_name());

    /* Prevent future output that can confuse users. */
    if (!not_first) {
        not_first = true;
    }
}
TACHE	Prod(void)
{
    puts("------> Start Prod");
    while(1) {
        puts("------> Prod : puis-je?");
        s_wait(mySem);
        puts("------> Prod : je prod");
        field.file[field.fin] = field.fin;
        field.fin = (field.fin+1) % MAX_TACHES;
        puts("------> Prod : go");
        s_signal(mySem2);
        s_signal(mySem2);
        s_signal(mySem2);
    }
    fin_tache();
}
Example #4
0
static void
s_atexit_handler(void) {
  if (likely(__sync_fetch_and_add(&s_is_exit_handler_called, 1) == 0)) {
    gallus_result_t r;
    r = s_trylock();

    if (likely(r == GALLUS_RESULT_OK)) {
      bool is_finished_cleanly = false;

      if (s_n_modules > 0) {

     recheck:
        mbar();
	if (s_gstate == MODULE_GLOBAL_STATE_UNKNOWN) {
          is_finished_cleanly = true;
        } else if (s_gstate == MODULE_GLOBAL_STATE_STARTED) {
          (void)global_state_request_shutdown(SHUTDOWN_RIGHT_NOW);
        } else if (s_gstate != MODULE_GLOBAL_STATE_FINALIZED) {
          r = s_wait(100LL * 1000LL * 1000LL);
          if (r == GALLUS_RESULT_OK) {
            goto recheck;
          } else if (r == GALLUS_RESULT_TIMEDOUT) {
            gallus_msg_warning("Module finalization seems not completed.\n");
          } else {
            gallus_perror(r);
            gallus_msg_error("module finalization wait failed.\n");
          }
        } else {
          is_finished_cleanly = true;
        }
      }

      if (is_finished_cleanly == true) {
        s_is_unloading = true;
        mbar();
      }

      s_unlock();

    } else if (r == GALLUS_RESULT_BUSY) {
      /*
       * The lock failure. Snoop s_gstate anyway. Note that it's safe
       * since the modules are always accessesed only by a single
       * thread and the thread is calling exit(3) at this moment.
       */
      if (s_gstate == MODULE_GLOBAL_STATE_UNKNOWN) {
        /*
         * No modules are initialized. Just exit cleanly and let all
         * the static destructors run.
         */
        s_is_unloading = true;
        mbar();
      } else {
        if (pthread_self() == s_initializer_tid) {
          /*
           * Made sure that this very thread is the module
           * initializer. So we can safely unlock the lock.
           */

          switch (s_gstate) {

            case MODULE_GLOBAL_STATE_FINALIZING:
            case MODULE_GLOBAL_STATE_FINALIZED:
            case MODULE_GLOBAL_STATE_UNKNOWN: {
              s_unlock();
              /*
               * Nothing is needed to do.
               */
              break;
            }

            case MODULE_GLOBAL_STATE_INITIALIZING:
            case MODULE_GLOBAL_STATE_INITIALIZED:
            case MODULE_GLOBAL_STATE_STARTING: {
              s_unlock();
              /*
               * With this only modules safely finalizable so far are
               * finalized.
               */
              gallus_module_finalize_all();
              break;
            }

            case MODULE_GLOBAL_STATE_STARTED: {
              s_unlock();
              (void)global_state_request_shutdown(SHUTDOWN_RIGHT_NOW);
              break;
            }

            case MODULE_GLOBAL_STATE_SHUTTINGDOWN:
            case MODULE_GLOBAL_STATE_STOPPING:
            case MODULE_GLOBAL_STATE_WAITING:
            case MODULE_GLOBAL_STATE_SHUTDOWN: {
              s_unlock();
              /*
               * There's nothing we can do at this moment.
               */
              break;
            }

            default: {
              s_unlock();
              break;
            }
          }

        } else { /* (pthread_self() == s_initializer_tid) */
          /*
           * This menas that a thread other than module initialized is
           * locking the lock. There's nothing we can do at this moment.
           */
          return;
        }
      } /* (s_gstate == MODULE_GLOBAL_STATE_UNKNOWN) */
    }
  }
}