Exemplo n.º 1
0
void* thread_stub2( void* v_td )
#endif
{
  ThreadData* td = reinterpret_cast<ThreadData*>( v_td );

  void ( *entry )( void* ) = td->entry;
  void ( *entry_noparam )( void ) = td->entry_noparam;
  void* arg = td->arg;

  threadmap.Register( thread_pid(), td->name );

  delete td;
  td = nullptr;

  if ( entry != nullptr )
    run_thread( entry, arg );
  else
    run_thread( entry_noparam );

#ifdef _WIN32
  _endthreadex( 0 );
  return 0;
#else
  pthread_exit( nullptr );
  return nullptr;
#endif
}
Exemplo n.º 2
0
static void
simulate(void)
{
  // create thread data
  thread_data = (ThreadData*)malloc(sizeof(ThreadData) * NTHREADS);
  
  int i;
  pthread_t tids[NTHREADS];
  
  // schedule lines per task
  if(NTHREADS == 1) {
    LINES_PER_THREAD_FIRST_PASS = map->lin;
    LINES_PER_THREAD_SECOND_PASS = map->lin;
  } else {
    int f = 2.0;
    int blocks = max(1, (int)((double)map->lin / (f * (double)NTHREADS)));
    
    LINES_PER_THREAD_FIRST_PASS = blocks;
    LINES_PER_THREAD_SECOND_PASS = blocks;
    
    printf("%d\n", blocks);
  }
  
  for(i = 1; i < NTHREADS; ++i) {
    pthread_create(&tids[i], NULL, run_thread, (void*)i);  
  }
  
  run_thread((THREAD_PARAM)MASTER_THREAD);
  
  for(i = 1; i < NTHREADS; ++i) {
    pthread_join(tids[i], NULL);
  }
  
  free(thread_data);
}
Exemplo n.º 3
0
PUBLIC void
schedule(void)
    /* find another runnable thread */
{
    struct thread *t;
    int i;

    need_reschedule=0;
    again:
    check_alarms();
    for (i=0; i < MAX_THREADS; i++) {
	last_run = (last_run+1) % MAX_THREADS;     /* wrap around */
	t = thread_table + last_run;
	if (t->status == THREAD_READY) break;
    }
    if (i == MAX_THREADS) {
	if (to_alarm) {
	    sleep(1);  /*!!!*/
	    goto again;
	}
	if (alive) panic("No thread runnable, %d alive\n", alive);
	else exit(0);   /* All threads dead */
    }
    if (DEBUG_LEVEL >=2)
	printf("Now scheduling %d\n", t - thread_table);
    else run_thread(t - thread_table);

    /* return to calling thread */
}
Exemplo n.º 4
0
void tkit::proc_timer::run(bool should_wait) {
 	std::vector<char*> cargs;
	for (int i = 0; i < _args.size(); i++) {
		const auto arg = _args[i];
		cargs.push_back(const_cast<char*>(arg.c_str()));
	}

	cargs.push_back(nullptr);
	_id = fork();
	if (_id == -1)
		throw fork_error();

	itimerval time_limit, old;
	time_limit.it_value = {_lim / 1000, (_lim % 1000) * 1000};
	time_limit.it_interval = {0, 0};

	// Limits on the child process:
	const rlimit process_lim = {1, 1};
	if (_id == 0) {
		setitimer(ITIMER_PROF, &time_limit, &old);
		setrlimit(RLIMIT_NPROC, &process_lim);
		execvp(_args[0].c_str(), cargs.data());
		_exit(EXIT_FAILURE);
	}

	std::thread run_thread(&tkit::proc_timer::_run_helper, this);
	if (should_wait)
		run_thread.join();
	else
		run_thread.detach();
}
int first_in_first_out(Job * jobs, int n, int* CPUs, int numCPU, long int global_start,FILE* output){
  int i = 0, next = 0, busy_CPUs = 0, nextToArive = 0;
  pthread_t* threads = malloc(sizeof(pthread_t)*n); 
  
  while(next < n){
    for(i = 0; i < numCPU; i++){
      if(nextToArive < n && time_diff(global_start) >= jobs[nextToArive].arrival*1000){
        if(get_debug()) fprintf(stderr,"O processo %s(linha: %d) chegou\n",*(&jobs[nextToArive].name),*(&jobs[nextToArive].line));
        nextToArive++;
      }
      if(nextToArive > next && !CPUs[i]){
        if(get_debug()) fprintf(stderr,"A cpu %d foi ocupada pelo processo %s\n", i, *(&jobs[next].name));
        CPUs[i] = 1;
        run_thread(threads[next], &jobs[next], i, output);
        next++;
      }
    }
  }

  do{
    busy_CPUs = 0;
    for(i = 0; i < numCPU; i++)
      busy_CPUs += CPUs[i];
  } while (busy_CPUs > 0);

  return 1;
}
Exemplo n.º 6
0
void
usb_enumerate(struct usbhub_s *hub)
{
    u32 portcount = hub->portcount;
    hub->threads = portcount;
    hub->detectend = timer_calc(USB_TIME_SIGATT);

    // Launch a thread for every port.
    int i;
    for (i=0; i<portcount; i++) {
        struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
        if (!usbdev) {
            warn_noalloc();
            continue;
        }
        memset(usbdev, 0, sizeof(*usbdev));
        usbdev->hub = hub;
        usbdev->port = i;
        run_thread(usb_hub_port_setup, usbdev);
    }

    // Wait for threads to complete.
    while (hub->threads)
        yield();
}
Exemplo n.º 7
0
 void run_threads() {
     std::for_each(
         threads.begin(), threads.end(),
         [this] (std::pair<unsigned, std::shared_ptr<thread>> pair) {
             run_thread(static_cast<thread_type>(pair.first));
         }
     );
 }
Exemplo n.º 8
0
void
usb_setup(void)
{
    ASSERT32FLAT();
    if (! CONFIG_USB)
        return;
    run_thread(__usb_setup, NULL);
}
Exemplo n.º 9
0
int main(int argc, char** argv)
{
  ros::init(argc, argv, "scan_producer");
  ros::NodeHandle nh;
  boost::thread run_thread(&runLoop);
  ros::spin();
  run_thread.join();
  return 0;
}
Exemplo n.º 10
0
static void
signal_condvar(ConditionVariable *condvar)
{
    VALUE waking = thread_exclusive(wake_one, (VALUE)&condvar->waiting);

    if (RTEST(waking)) {
        run_thread(waking);
    }
}
Exemplo n.º 11
0
void run_vm() {
  graph = create_graph();
  ip = (WORD*) code;
  run_thread();
  while (1) {
    read_inputs();
    update_signals();
    write_outputs();
  }
}
Exemplo n.º 12
0
asmlinkage void thread_create_handler(struct kernel_msg_handler_arg *arg)
{
    struct process *p = arg->sender_thread->proc;
    struct thread *t;
    msg_t *m;
    
    ulong wrapper = arg->msg->params[0].value;
    ulong stack_size = arg->msg->params[1].value;
    ulong tls_size = arg->msg->params[2].value;
    
    ulong start_routine = arg->msg->params[3].value;
    ulong start_arg = arg->msg->params[4].value;
    ulong kthread = arg->msg->params[5].value;
    
    //kprintf("To create user thread, process: %s, wrapper: %p, stack: %p, tls: %p\n", p->name, wrapper, stack_size, tls_size);
    
    // Create the thread
    t = create_thread(p, wrapper, 0, -1, stack_size, tls_size);
    assert(t);
    
    // Set the msg for the newly created thread
    m = create_response_msg(t);
    set_msg_param_value(m, start_routine);
    set_msg_param_value(m, start_arg);
    set_msg_param_value(m, kthread);
    
    set_thread_arg(t, t->memory.block_base + t->memory.msg_recv_offset);
    
    // Set the msg for the sender thread
    m = create_response_msg(arg->sender_thread);
    set_msg_param_value(m, t->thread_id);
    
    // Run both threads
    run_thread(t);
    run_thread(arg->sender_thread);
    
    // Clean up
    terminate_thread_self(arg->handler_thread);
    sfree(arg);

    // Wait for this thread to be terminated
    kernel_unreachable();
}
Exemplo n.º 13
0
static void
signal_condvar(ConditionVariable *condvar)
{
    VALUE waking;
    //rb_thread_critical = 1;
    waking = rb_ensure(wake_one, (VALUE)&condvar->waiting, set_critical, 0);
    if (RTEST(waking)) {
        run_thread(waking);
    }
}
Exemplo n.º 14
0
void DllInjector::inject_into_pid(int pid)
{
	if (!file_exists(dll_path))
		throw std::runtime_error("File '" + dll_path + "' does not exist!");

	HANDLE process = open_process(pid);
	void* memory_ptr = allocate_process_memory(pid, process);
	write_process_memory(pid, process, memory_ptr);
	HANDLE thread = create_remote_thread(pid, process, memory_ptr);
	run_thread(thread);
}
Exemplo n.º 15
0
void
ps2port_setup()
{
    if (! CONFIG_PS2PORT)
        return;
    dprintf(3, "init ps2port\n");

    enable_hwirq(1, entry_09);
    enable_hwirq(12, entry_74);

    run_thread(keyboard_init, NULL);
}
Exemplo n.º 16
0
Arquivo: forkmain.c Projeto: 8l/cmm
int main(int argc, char *argv[]) {
  void *stack;
  Cmm_Cont *thread;
  int rc;

  stack = malloc(STACKSIZE);
  assert(stack);
  thread = Cmm_CreateThread(cmm_threadfun, "fib(%d) = %d\n", stack, STACKSIZE);
  rc = run_thread(thread);
  printf("run_thread returns %d\n", rc);
  return 0;
}
Exemplo n.º 17
0
void
ps2port_setup(void)
{
    ASSERT32FLAT();
    if (! CONFIG_PS2PORT)
        return;
    dprintf(3, "init ps2port\n");

    enable_hwirq(1, FUNC16(entry_09));
    enable_hwirq(12, FUNC16(entry_74));

    run_thread(keyboard_init, NULL);
}
Exemplo n.º 18
0
static VALUE
unlock_mutex(Mutex *mutex)
{
    VALUE waking = thread_exclusive(unlock_mutex_inner, (VALUE)mutex);

    if (!RTEST(waking)) {
        return Qfalse;
    }

    run_thread(waking);

    return Qtrue;
}
Exemplo n.º 19
0
int
ehci_init(struct pci_device *pci, int busid, struct pci_device *comppci)
{
    if (! CONFIG_USB_EHCI)
        return -1;

    u16 bdf = pci->bdf;
    u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
    struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
    u32 hcc_params = readl(&caps->hccparams);
    if (hcc_params & HCC_64BIT_ADDR) {
        dprintf(1, "No support for 64bit EHCI\n");
        return -1;
    }

    struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
    if (!cntl) {
        warn_noalloc();
        return -1;
    }
    memset(cntl, 0, sizeof(*cntl));
    cntl->usb.busid = busid;
    cntl->usb.pci = pci;
    cntl->usb.type = USB_TYPE_EHCI;
    cntl->caps = caps;
    cntl->regs = (void*)caps + readb(&caps->caplength);

    dprintf(1, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
            , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
            , pci_bdf_to_fn(bdf), cntl->regs);

    pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);

    // XXX - check for and disable SMM control?

    // Find companion controllers.
    int count = 0;
    for (;;) {
        if (!comppci || comppci == pci)
            break;
        if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_UHCI)
            cntl->companion[count++] = comppci;
        else if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_OHCI)
            cntl->companion[count++] = comppci;
        comppci = comppci->next;
    }

    run_thread(configure_ehci, cntl);
    return 0;
}
Exemplo n.º 20
0
static int test_thread_local(void) {
  void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST);
  if (ptr != NULL) {
    fprintf(stderr, "Thread-local data was non-NULL at start.\n");
  }

  thread_t thread;
  if (!run_thread(&thread, thread_local_test_thread) ||
      !wait_for_thread(thread)) {
    fprintf(stderr, "thread failed.\n");
    return 0;
  }

  if (!g_test_thread_ok) {
    fprintf(stderr, "Thread-local data didn't work in thread.\n");
    return 0;
  }

  if (g_destructor_called_count != 1) {
    fprintf(stderr,
            "Destructor should have been called once, but actually called %u "
            "times.\n",
            g_destructor_called_count);
    return 0;
  }

  /* thread_local_test2_thread doesn't do anything, but it tests that the
   * thread destructor function works even if thread-local storage wasn't used
   * for a thread. */
  if (!run_thread(&thread, thread_local_test2_thread) ||
      !wait_for_thread(thread)) {
    fprintf(stderr, "thread failed.\n");
    return 0;
  }

  return 1;
}
Exemplo n.º 21
0
static VALUE
rb_mutex_exclusive_unlock(VALUE self)
{
    Mutex *mutex;
    VALUE waking;
    Data_Get_Struct(self, Mutex, mutex);

    waking = thread_exclusive(rb_mutex_exclusive_unlock_inner, (VALUE)mutex);

    if (!RTEST(waking)) {
        return Qnil;
    }

    run_thread(waking);

    return self;
}
Exemplo n.º 22
0
void
pvscsi_setup(void)
{
    ASSERT32FLAT();
    if (! CONFIG_PVSCSI)
        return;

    dprintf(3, "init pvscsi\n");

    struct pci_device *pci;
    foreachpci(pci) {
        if (pci->vendor != PCI_VENDOR_ID_VMWARE
            || pci->device != PCI_DEVICE_ID_VMWARE_PVSCSI)
            continue;
        run_thread(init_pvscsi, pci);
    }
}
Exemplo n.º 23
0
static VALUE
unlock_mutex(Mutex *mutex)
{
    VALUE waking;

    //rb_thread_critical = 1;
    waking = rb_ensure(unlock_mutex_inner, (VALUE)mutex, set_critical, 0);

    if (waking == Qundef) {
        return Qfalse;
    }

    if (RTEST(waking)) {
        run_thread(waking);
    }

    return Qtrue;
}
Exemplo n.º 24
0
// A single threaded main loop
void work_loop(void)
{
	for (;;) {
		thread_t *thread;
		if ((thread = Q_GET_HEAD(&state.sched_queue))) {
			Q_REMOVE(&state.sched_queue, thread, q_link);
			run_thread(thread);
			state.events_until_epoll--;
		}
		bool can_sleep = !Q_GET_HEAD(&state.sched_queue);
		if (can_sleep || state.events_until_epoll == 0) {
			struct timespec next_wakeup = get_next_wakeup();
			do_poll(can_sleep, next_wakeup);
			state.events_until_epoll = Q_GET_SIZE(&state.sched_queue);
			wakeup_sleepers(); // maybe not every time through?
		}
	}
}
Exemplo n.º 25
0
// A multithreaded main loop
void *work_loop_mt(void *p)
{
	for (;;) {
		thread_t *thread;
		sched_lock();
		wakeup_sleepers(); // maybe not every time through?
		if ((thread = Q_GET_HEAD(&state.sched_queue))) {
			Q_REMOVE(&state.sched_queue, thread, q_link);
			sched_unlock();
			run_thread(thread);
			sched_lock();
		}
		bool can_sleep = !Q_GET_HEAD(&state.sched_queue);
		struct timespec next_wakeup = get_next_wakeup();
		sched_unlock();
		do_poll(can_sleep, next_wakeup);
	}
	return NULL;
}
Exemplo n.º 26
0
static VALUE
rb_mutex_exclusive_unlock(VALUE self)
{
    Mutex *mutex;
    VALUE waking;
    Data_Get_Struct(self, Mutex, mutex);

    //rb_thread_critical = 1;
    waking = rb_ensure(rb_mutex_exclusive_unlock_inner, (VALUE)mutex, set_critical, 0);

    if (waking == Qundef) {
        return Qnil;
    }

    if (RTEST(waking)) {
        run_thread(waking);
    }

    return self;
}
Exemplo n.º 27
0
int main(int argc,char **argv) {
    static int *thread_pid = NULL;
    int pid;
    int i,nr = 0;
    if(argc > 1 ) {
        nr = atoi(argv[1]);
    }
    if(nr <= 0) {
        nr = 10;
    }
    thread_pid = calloc(nr, sizeof(*thread_pid));
    for(i=0;i<nr;++i) { //create each thread
        if( (thread_pid[i] = create_thread(mythread, NULL) ) < 0)
            goto out_error;
        if(run_thread(thread_pid[i]) < 0 )  //add the threads to the run_queue
            goto out_error;
    }
#if 1
    if(execute_threads() < 0) 
        goto out_error;
    return 0;
#endif
    if(! (pid = fork()) ) {
        if(execute_threads() < 0) 
            goto out_error;
    }else if(pid > 0) {
        restart:
        while(wait((int*)0) != pid);
        if(errno == EINTR) goto restart;
        message(0,stderr,"Back after thread execution:\n");
    } else {
        message(0,stderr,"Error forking:\n");
        goto out_error;
    }
    return 0;
    out_error:
    message(0,stderr,"Failed in thread creation:\n");
    return 1;
}
Exemplo n.º 28
0
// constructor:
starsMainView::starsMainView(QWidget *parent) : QWidget(parent)
{
   setWindowTitle("Qt example 02");
   runButton = new QPushButton("Generate image...");
   connect(runButton, SIGNAL(clicked()), this, SLOT(run_thread()));

   QHBoxLayout *layout = new QHBoxLayout;
   layout->addWidget(runButton);
   setLayout(layout);

// window to display the image

   imageWindow = new ImageWindow();

// the QImage generation thread
   //qRegisterMetaType<QImage>("QImage");
   otherThread = new timeConsumingThread();

// slot to respond to the QImage generation thread
// - display the image in a window

   connect(otherThread, SIGNAL(theImage(const QImage &)), this, SLOT(displayImage(const QImage &)));
}
Exemplo n.º 29
0
static int test_once(void) {
  if (g_once_init_called != 0) {
    fprintf(stderr, "g_once_init_called was non-zero at start.\n");
    return 0;
  }

  thread_t thread;
  if (!run_thread(&thread, call_once_thread) ||
      !wait_for_thread(thread)) {
    fprintf(stderr, "thread failed.\n");
    return 0;
  }

  CRYPTO_once(&g_test_once, once_init);

  if (g_once_init_called != 1) {
    fprintf(stderr, "Expected init function to be called once, but found %u.\n",
            g_once_init_called);
    return 0;
  }

  return 1;
}
Exemplo n.º 30
0
static void
pow_init(void)
{
    task_t self;

    /*
     * Set default power actions
     */
    pmact.pwrbtn = PWR_OFF;
    pmact.slpbtn = PWR_SUSPEND;
    pmact.lcdclose = PWR_SUSPEND;
    pmact.lowbatt = PWR_OFF;

    /*
     * Connect to the pm driver to get all power events.
     */
    if (device_open("pm", 0, &pmdev) != 0) {
        /*
         * Bad config...
         */
        sys_panic("pow: no pm driver");
    }
    self = task_self();
    device_ioctl(pmdev, PMIOC_CONNECT, &self);

    /*
     * Setup exception to receive signals from pm driver.
     */
    exception_setup(exception_handler);

    /*
     * Start power thread.
     */
    if (run_thread(power_thread))
        sys_panic("pow_init");
}