Ejemplo n.º 1
0
int start_native_gui(wxe_data *sd)
{
  int res;
  wxe_status_m = erl_drv_mutex_create((char *) "wxe_status_m");
  wxe_status_c = erl_drv_cond_create((char *)"wxe_status_c");

  wxe_batch_locker_m = erl_drv_mutex_create((char *)"wxe_batch_locker_m");
  wxe_batch_locker_c = erl_drv_cond_create((char *)"wxe_batch_locker_c");
  init_caller = driver_connected(sd->port_handle);

#ifdef __DARWIN__
  res = erl_drv_steal_main_thread((char *)"wxwidgets",
				  &wxe_thread,wxe_main_loop,(void *) sd->pdl,NULL);
#else
  ErlDrvThreadOpts *opts = erl_drv_thread_opts_create((char *)"wx thread");
  opts->suggested_stack_size = 8192;
  res = erl_drv_thread_create((char *)"wxwidgets",
			      &wxe_thread,wxe_main_loop,(void *) sd->pdl,opts);
  erl_drv_thread_opts_destroy(opts);
#endif
  if(res == 0) {
    erl_drv_mutex_lock(wxe_status_m);
    for(;wxe_status == WXE_NOT_INITIATED;) {
      erl_drv_cond_wait(wxe_status_c, wxe_status_m);
    }
    erl_drv_mutex_unlock(wxe_status_m);
    return wxe_status;
  } else {
    wxString msg;
    msg.Printf(wxT("Erlang failed to create wxe-thread %d\r\n"), res);
    send_msg("error", &msg);
    return -1;
  }
}
Ejemplo n.º 2
0
int start_native_gui(wxe_data *sd)
{
  int res;
  wxe_status_m = erl_drv_mutex_create((char *) "wxe_status_m");
  wxe_status_c = erl_drv_cond_create((char *)"wxe_status_c");

  wxe_batch_locker_m = erl_drv_mutex_create((char *)"wxe_batch_locker_m");
  wxe_batch_locker_c = erl_drv_cond_create((char *)"wxe_batch_locker_c");
  init_caller = driver_connected(sd->port); 

  if((res = erl_drv_thread_create((char *)"wxwidgets",
				  &wxe_thread,wxe_main_loop,(void *) sd->pdl,NULL)) == 0) {
    erl_drv_mutex_lock(wxe_status_m);
    for(;wxe_status == WXE_NOT_INITIATED;) {
      erl_drv_cond_wait(wxe_status_c, wxe_status_m);
    }
    erl_drv_mutex_unlock(wxe_status_m);
    return wxe_status;
  } else {
    wxString msg;
    msg.Printf(wxT("Erlang failed to create wxe-thread %d\r\n"), res);
    send_msg("error", &msg);
    return -1;
  }
}
Ejemplo n.º 3
0
dthread_t* dthread_start(ErlDrvPort port,
			 void* (*func)(void* arg),
			 void* arg, int stack_size)
{
    ErlDrvThreadOpts* opts = NULL;
    dthread_t* thr = NULL;

    if (!(thr = DALLOC(sizeof(dthread_t))))
	return 0;

    if (dthread_init(thr, port) < 0)
	goto error;

    if (!(opts = erl_drv_thread_opts_create("dthread_opts")))
	goto error;

    opts->suggested_stack_size = stack_size;
    thr->arg = arg;

    if (erl_drv_thread_create("dthread", &thr->tid, func, thr, opts) < 0)
	goto error;
    erl_drv_thread_opts_destroy(opts);
    return thr;

error:
    dthread_finish(thr);
    if (opts)
        erl_drv_thread_opts_destroy(opts);
    dthread_finish(thr);
    DFREE(thr);
    return 0;
}
Ejemplo n.º 4
0
int
erts_thread_create(erl_thread_t *tid,
		   void* (*func)(void*),
		   void* arg,
		   int detached)
{
    if (detached)
	return ENOTSUP;
    return erl_drv_thread_create(NULL, (ErlDrvTid *) tid, func, arg, NULL);
}
Ejemplo n.º 5
0
static ErlDrvData innostore_drv_start(ErlDrvPort port, char* buffer)
{
    PortState* state = (PortState*)driver_alloc(sizeof(PortState));
    int worker_rc;

    memset(state, '\0', sizeof(PortState));

    // Save handle to the port
    state->port = port;

    // Save the owner PID
    state->port_owner = driver_connected(port);

    // Initialize in the READY state
    state->port_state = STATE_READY;

    // Make sure port is running in binary mode
    set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);

    // Allocate a mutex and condition variable for the worker
    state->worker_lock = erl_drv_mutex_create("innostore_worker_lock");
    state->worker_cv   = erl_drv_cond_create("innostore_worker_cv");

    // Spin up the worker
    worker_rc = erl_drv_thread_create("innostore_worker", &(state->worker),
                                      &innostore_worker, state, 0);
    if (state->worker_lock != NULL &&
        state->worker_cv != NULL &&
         worker_rc == 0)
    {
        return (ErlDrvData)state;
    }
    else
    {
        log("Innostore: Could not create port [lock=%p, cv=%p]\n",
            state->worker_lock, state->worker_cv);

        if (state->worker_cv != NULL)
            erl_drv_cond_destroy(state->worker_cv);

        if (state->worker_lock != NULL)
            erl_drv_mutex_destroy(state->worker_lock);

        driver_free(state);

        errno = worker_rc;
        return (ErlDrvData) ERL_DRV_ERROR_ERRNO;
    }
}
Ejemplo n.º 6
0
static ErlDrvSSizeT control(ErlDrvData drv_data,
                            unsigned int command,
                            char *buf, ErlDrvSizeT len,
                            char **rbuf, ErlDrvSizeT rlen)
{
    char *res_str;
    PRINTF(("control(%p, %d, ...) called\r\n", drv_data, command));

    switch (command) {
    case ERTS_FP_THREAD_TEST: {
        ErlDrvTid tid;
        ErlDrvSysInfo info;
        driver_system_info(&info, sizeof(ErlDrvSysInfo));
        if (!info.thread_support)
            res_str = "skip: no thread support";
        else if (0 != erl_drv_thread_create("test", &tid, do_test, NULL, NULL))
            res_str = "failed to create thread";
        else if (0 != erl_drv_thread_join(tid, &res_str))
            res_str = "failed to join thread";
        break;
    }
    case ERTS_FP_CONTROL_TEST:
        res_str = do_test(NULL);
        break;
    default:
        res_str = "unknown command";
        break;
    }

done: {
        int res_len = strlen(res_str);
        if (res_len > rlen) {
            char *abuf = driver_alloc(sizeof(char)*res_len);
            if (!abuf)
                return 0;
            *rbuf = abuf;
        }

        memcpy((void *) *rbuf, (void *) res_str, res_len);

        return res_len;
    }
}
Ejemplo n.º 7
0
TPool* bdberl_tpool_start(unsigned int thread_count)
{
    TPool* tpool = driver_alloc(sizeof(TPool));
    memset(tpool, '\0', sizeof(TPool));

    // Initialize lock, cv, etc.
    tpool->lock         = erl_drv_mutex_create("bdberl_tpool_lock");
    tpool->work_cv      = erl_drv_cond_create("bdberl_tpool_work_cv");
    tpool->cancel_cv    = erl_drv_cond_create("bdberl_tpool_cancel_cv");
    tpool->threads      = driver_alloc(sizeof(ErlDrvTid) * thread_count);
    tpool->thread_count = thread_count;

    // Startup all the threads
    int i;
    for (i = 0; i < thread_count; i++)
    {
        // TODO: Figure out good way to deal with errors in this situation (should be rare, but still...)
        erl_drv_thread_create("bdberl_tpool_thread", &(tpool->threads[i]), &bdberl_tpool_main, (void*)tpool, 0);
    }

    return tpool;
}
Ejemplo n.º 8
0
Archivo: erl_nif.c Proyecto: a5an0/otp
int enif_thread_create(char *name, ErlNifTid *tid, void* (*func)(void *),
		       void *args, ErlNifThreadOpts *opts) {
    return erl_drv_thread_create(name,tid,func,args,(ErlDrvThreadOpts*)opts);
}
Ejemplo n.º 9
0
Archivo: echo_drv.c Proyecto: HansN/otp
static void echo_drv_output(ErlDrvData drv_data, char *buf, ErlDrvSizeT len) {
    EchoDrvData* data_p = (EchoDrvData *) drv_data;
    ErlDrvPort port = data_p->erlang_port;

    switch (buf[0]) {
    case ECHO_DRV_OUTPUT:
    {
        driver_output(port, buf+1, len-1);
        break;
    }
    case ECHO_DRV_OUTPUT2:
    {
         driver_output2(port, "a", 1, buf+1, len-1);
         break;
    }
    case ECHO_DRV_OUTPUT_BINARY:
    {
        ErlDrvBinary *bin = driver_alloc_binary(len-1);
        memcpy(&bin->orig_bytes, buf+1, len-1);
        driver_output_binary(port, "a", 1, bin, 1, len - 2);
        driver_free_binary(bin);
        break;
    }
    case ECHO_DRV_OUTPUTV:
    {
        ErlIOVec iov;
        ErlDrvSizeT sz;
        driver_enq(port, buf + 1, len - 1);
        sz = driver_peekqv(port, &iov);
        driver_outputv(port, "a", 1, &iov, 0);
        driver_deq(port, sz);
        break;
    }
    case ECHO_DRV_SET_TIMER:
    {
        driver_set_timer(port, 10);
        break;
    }
    case ECHO_DRV_FAILURE_EOF:
    {
        driver_failure_eof(port);
        break;
    }
    case ECHO_DRV_FAILURE_ATOM:
    {
        driver_failure_atom(port, buf+1);
        break;
    }
    case ECHO_DRV_FAILURE_POSIX:
    {
        driver_failure_posix(port, EAGAIN);
        break;
    }
    case ECHO_DRV_FAILURE:
    {
        driver_failure(port, buf[1]);
        break;
    }
    case ECHO_DRV_OUTPUT_TERM:
    case ECHO_DRV_DRIVER_OUTPUT_TERM:
    case ECHO_DRV_SEND_TERM:
    case ECHO_DRV_DRIVER_SEND_TERM:
    {
        ErlDrvTermData term[] = {
            ERL_DRV_ATOM, driver_mk_atom("echo"),
            ERL_DRV_PORT, driver_mk_port(port),
            ERL_DRV_BUF2BINARY, (ErlDrvTermData)(buf+1),
                                (ErlDrvTermData)(len - 1),
            ERL_DRV_TUPLE, 3};
        switch (buf[0]) {
        case ECHO_DRV_OUTPUT_TERM:
            erl_drv_output_term(driver_mk_port(port), term, sizeof(term) / sizeof(ErlDrvTermData));
            break;
        case ECHO_DRV_DRIVER_OUTPUT_TERM:
            driver_output_term(port, term, sizeof(term) / sizeof(ErlDrvTermData));
            break;
        case ECHO_DRV_SEND_TERM:
            driver_send_term(port, data_p->caller,
                             term, sizeof(term) / sizeof(ErlDrvTermData));
            break;
        case ECHO_DRV_DRIVER_SEND_TERM:
            erl_drv_send_term(driver_mk_port(port), data_p->caller,
                              term, sizeof(term) / sizeof(ErlDrvTermData));
            break;
        }
        break;
    }
    case ECHO_DRV_REMOTE_SEND_TERM:
    {
        struct remote_send_term *t = driver_alloc(sizeof(struct remote_send_term) + len);
        t->len = len-1;
        t->port = driver_mk_port(port);
        t->caller = data_p->caller;
        memcpy(t->buf, buf+1, t->len);
        erl_drv_thread_create("tmp_thread", &t->thread.tid, send_term_thread, t, NULL);
        t->thread.next = data_p->threads;
        data_p->threads = &t->thread;
        break;
    }
    case ECHO_DRV_SAVE_CALLER:
        data_p->caller = driver_caller(port);
        break;
    default:
        break;
    }
}