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; } }
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; } }
static ErlDrvData start(ErlDrvPort port, char *command) { Otp9302Data *data; ErlDrvSysInfo sys_info; data = driver_alloc(sizeof(Otp9302Data)); if (!data) return ERL_DRV_ERROR_GENERAL; data->port = port; driver_system_info(&sys_info, sizeof(ErlDrvSysInfo)); data->smp = sys_info.smp_support; if (!data->smp) { data->msgq.start = NULL; data->msgq.end = NULL; data->msgq.mtx = erl_drv_mutex_create(""); if (!data->msgq.mtx) { driver_free(data); return ERL_DRV_ERROR_GENERAL; } } return (ErlDrvData) data; }
void mutex_init(mutex_t * mutex){ #ifdef USE_PTHREAD if( pthread_mutex_init(mutex,NULL) != 0 ){ return ; }; #elif defined USE_ETHREAD *mutex = erl_drv_mutex_create(NULL); #endif // return ret; }
static ErlDrvData syslogdrv_start(ErlDrvPort port, char *buf) { syslogdrv_t* d = (syslogdrv_t*)driver_alloc(sizeof(syslogdrv_t)); d->port = port; d->open = 0; d->ident = NULL; set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY); syslog_mtx = erl_drv_mutex_create("syslog_mtx"); return (ErlDrvData)d; }
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; } }
static int innostore_drv_init() { char log_filename[_POSIX_PATH_MAX]; size_t log_filename_size = sizeof(log_filename); ErlDrvSysInfo sys_info; G_ENGINE_STATE_LOCK = erl_drv_mutex_create("innostore_state_lock"); G_LOGGER_LOCK = erl_drv_mutex_create("innostore_logger_lock"); // Check if this is beam.smp - cannot run under beam // due to restrictions with driver_send_term driver_system_info(&sys_info, sizeof(sys_info)); if (sys_info.smp_support == 0) { log("Innostore only supports the SMP runtime, add -smp enable"); return -1; } // Initialize Inno's memory subsystem if (ib_init() != DB_SUCCESS) { return -1; } // Set up the logger if (erl_drv_getenv("INNOSTORE_LOG", log_filename, &log_filename_size) == 0) { set_log_file(log_filename); } else { set_log_file(NULL); } return 0; }
// Initialize thread structure int dthread_init(dthread_t* thr, ErlDrvPort port) { ErlDrvSysInfo sys_info; memset(thr, 0, sizeof(dthread_t)); dthread_signal_init(thr); driver_system_info(&sys_info, sizeof(ErlDrvSysInfo)); // smp_support is used for message passing from thread to // calling process. if SMP is supported the message will go // directly to sender, otherwise it must be sent to port thr->smp_support = sys_info.smp_support; thr->port = port; thr->dport = driver_mk_port(port); thr->owner = driver_connected(port); if (!(thr->iq_mtx = erl_drv_mutex_create("iq_mtx"))) return -1; #ifdef __WIN32__ // create a manual reset event if (!(thr->iq_signal[0] = (ErlDrvEvent) CreateEvent(NULL, TRUE, FALSE, NULL))) { dthread_finish(thr); return -1; } DEBUGF("dthread_init: handle=%d", DTHREAD_EVENT(thr->iq_signal[0])); #else { int pfd[2]; if (pipe(pfd) < 0) { dthread_finish(thr); return -1; } DEBUGF("dthread_init: pipe[0]=%d,pidp[1]=%d", pfd[0], pfd[1]); thr->iq_signal[0] = (ErlDrvEvent) ((long)pfd[0]); thr->iq_signal[1] = (ErlDrvEvent) ((long)pfd[1]); INFOF("pipe: %d,%d", pfd[0], pfd[1]); } #endif return 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; }
// Called by the emulator when the driver is starting. static ErlDrvData start_driver(ErlDrvPort port, char *buff) { EdbcOciPortData *pd = zalloc(port, sizeof(EdbcOciPortData)); if (pd == NULL) { // TODO: use ERL_DRV_ERROR_ERRNO and provide out-of-memory info return ERL_DRV_ERROR_GENERAL; } ErlDrvSysInfo sys_info; driver_system_info(&sys_info, sizeof(ErlDrvSysInfo)); pd->send_term_requires_lock = (sys_info->smp_support == 0); pd->port = (void*)port; pd->owner = driver_connected(port); pd->mutex = erl_drv_mutex_create("edbc_oci_port_instance_mutex"); // some fields can't be initialized until we've got our hands on // the relevant configuration data at runtime... pd->worker_pool = NULL; pd->log = NULL; return (ErlDrvData)pd; };
ErlNifMutex* enif_mutex_create(char *name) { return erl_drv_mutex_create(name); }
erl_mutex_t erts_mutex_create(void) { return (erl_mutex_t) erl_drv_mutex_create(NULL); }