void *thread_function(void *args) { while(1) { /* Get the next available job. */ struct job *next_job; /* Wait on the job queue semaphore. If its value is positive, indicating that the queue is not empty, decrement the count by 1. If the queue is empty, block until a new job is enqueued. */ sem_wait(&job_queue_count); printf("%s : Pass sem_wait job_queue_count[%d]\n", __FUNCTION__, job_queue_count); /* Lock the mutex on the job queue. */ pthread_mutex_lock(&job_queue_mutex); /* Because of the semaphore, we know the queue is not empty. Get the next available job. */ next_job = job_queue; /* Remove this job from the list. */ job_queue = job_queue->next; /* Unlock the mutex on the job queue because we¡¯re done with the queue for now. */ pthread_mutex_unlock(&job_queue_mutex); /* Carry out the work. */ process_job(next_job); free(next_job); } return NULL; }
/* Process queued jobs until the queue is empty. * Its * */ void* thread_function (void* arg) { printf("Thread %d start...\n", (int) pthread_self()); while (1) { struct job* next_job; /* Wait on the job queue semaphore. If its value is positive, * indicating that the queue is not empty, decrement the count by * 1. If the queue is empty, block until a new job is enqueued. */ sem_wait (&job_queue_count); /* Lock the mutex on the job queue. */ pthread_mutex_lock (&job_queue_mutex); /* Because of the semaphore, we know the queue is not empty. Get * the next available job. */ next_job = job_queue; /* Remove this job from the list. */ job_queue = job_queue->next; /* Unlock the mutex on the job queue because we’re done with the * queue for now. */ pthread_mutex_unlock (&job_queue_mutex); /* Carry out the work. */ process_job (next_job); /* Clean up. */ free (next_job); } return NULL; }
static int __consume(wait_queue_t *wait, int cid) { struct queue *q; int ret; mutex_lock(&qmutex); if (qlen == 0) { INFO("Consumer/%d: waiting", cid); prepare_to_wait_exclusive(&cwq, wait, TASK_INTERRUPTIBLE); mutex_unlock(&qmutex); return 0; } q = remove_first_job(); qlen--; if (qlen < qmax && waitqueue_active(&pwq)) wake_up(&pwq); /* wake up one producer */ mutex_unlock(&qmutex); INFO("Consumer/%d: processing job[%u]", cid, q->job->id); ret = process_job(q->job, cid); INFO("Consumer/%d: done", cid); kfree(q); return ret; }
static int thread_fn(void *thread_id) { int id; allow_signal(SIGKILL); while (!kthread_should_stop()) { struct job* next_job; if (down_interruptible(&my_sem)) { printk(KERN_INFO "Unable to hold the semaphore\n"); break; } mutex_lock(&my_mutex); if (job_queue == NULL) next_job = NULL; else { next_job = job_queue; job_queue = job_queue->next; } mutex_unlock (&my_mutex); if (next_job == NULL) break; process_job (thread_id, next_job); kfree (next_job); if (signal_pending(current)) break; } id = *(int *)thread_id; thread_cons[id] = NULL; printk(KERN_INFO "Consumer Thread Stopping\n"); do_exit(0); }
void* thread_function(void* arg) { while(1) { struct job *next_job; pthread_mutex_lock(&job_queue_mutex); if(job_queue == NULL) { next_job = NULL; } else { next_job = job_queue; job_queue = job_queue->next; } pthread_mutex_unlock(&job_queue_mutex); if(next_job == NULL) { break; } process_job(next_job); free(next_job); } return NULL; }
void thread_fun(int thread_id) { for (;;) { mutex::scoped_lock l(m_mutex); while (m_queue.empty() && thread_id < m_num_threads) m_cond.wait(l); // if the number of wanted thread is decreased, // we may stop this thread // when we're terminating the last hasher thread (id=0), make sure // we finish up all queud jobs first if ((thread_id != 0 || m_queue.empty()) && thread_id >= m_num_threads) break; TORRENT_ASSERT(!m_queue.empty()); T e = m_queue.front(); m_queue.pop_front(); l.unlock(); process_job(e, true); } #ifdef TORRENT_DEBUG if (thread_id == 0) { // when we're terminating the last hasher thread, make sure // there are no more scheduled jobs mutex::scoped_lock l(m_mutex); TORRENT_ASSERT(m_queue.empty()); } #endif }
void* thread_function (void* arg){ int* n = (int *) arg; printf("*********************Soy el hilo %d**************************\n", *n); while (1) { struct job* next_job; /* Wait on the job queue semaphore. If its value is positive, indicating that the queue is not empty, decrement the count by 1. If the queue is empty, block until a new job is enqueued. */ sem_wait (&job_queue_count); /* Lock the mutex on the job queue. */ pthread_mutex_lock (&job_queue_mutex); /* Now it’s safe to check if the queue is empty. */ if (job_queue == NULL) next_job = NULL; else { /* Get the next available job. */ next_job = job_queue; /* Remove this job from the list. */ job_queue = job_queue->next; } /* Unlock the mutex on the job queue because we’re done with the queue for now. */ pthread_mutex_unlock (&job_queue_mutex); /* Was the queue empty? */ if (next_job == NULL) break; /*If so, end the thread.*/ /* Carry out the work. */ process_job (next_job, *n); /* Clean up. */ free (next_job); } return (void*) 1; }
static void finish_job(void *arg) { struct build_peer *peer = arg; LIST_REMOVE(peer, peer_link); if (peer->tmp_buf[0] == 'D') process_job(peer->job, JOB_DONE, 1); else if (peer->tmp_buf[0] == 'F') process_job(peer->job, JOB_FAILED, 1); else kill_peer(peer); peer->job = NULL; recv_command(peer); peer = LIST_FIRST(&unassigned_peers); if (peer != NULL) assign_job(peer); }
static void standalone_mode(void) { struct scan_job *job; while ((job = get_job()) != NULL) { job->scan_output = scan_pkglocation(job->pkg_location); process_job(job, JOB_DONE); } }
static void finish_job(void *arg) { struct scan_peer *peer = arg; if (strlen(peer->job->scan_output) != peer->output_len) { warnx("Invalid output len received from peer"); kill_peer(peer); return; } LIST_REMOVE(peer, peer_link); process_job(peer->job, JOB_DONE); assign_job(peer); }
static void kill_peer(void *arg) { struct build_peer *peer = arg; (void)close(peer->fd); LIST_REMOVE(peer, peer_link); if (peer->job != NULL) process_job(peer->job, JOB_OPEN, 1); free(peer->buf); free(peer); peer = LIST_FIRST(&unassigned_peers); if (peer != NULL) assign_job(peer); }
static void kill_peer(void *arg) { struct scan_peer *peer = arg; (void)close(peer->fd); LIST_REMOVE(peer, peer_link); free(peer->job->scan_output); peer->job->scan_output = NULL; process_job(peer->job, JOB_OPEN); free(peer); peer = LIST_FIRST(&inactive_peers); if (peer == NULL) return; LIST_REMOVE(peer, peer_link); assign_job(peer); }
int consumer(void *data){ struct job *job = NULL; int err = 0; top: /* waiting for jobs */ wait_event_interruptible(consumers, prod_cons_q_len > 0); /* module exit, killing the thread */ if(thread_exit == 1) goto exit; mutex_lock(&big_mutex); if(prod_cons_q_len > 0){ job = remove_job(prod_cons_q); if(IS_ERR(job)){ err = PTR_ERR(job); goto out; } prod_cons_q_len--; } mutex_unlock(&big_mutex); wake_up_all(&producers); err = process_job(job); schedule(); goto top; out: mutex_unlock(&big_mutex); exit: return err; }
int has_job(void) { size_t i; struct scan_entry *e; struct scan_job * job; for (i = first_undone_job; i < len_jobs; ++i) { job = &jobs[i]; if (job->state != JOB_OPEN) continue; e = find_old_scan(job->pkg_location); if (e == NULL) return 1; job->scan_output = xstrdup(e->data); process_job(job, JOB_DONE); i = first_undone_job - 1; } return 0; }
void async_reply_socket::thread_function(thread_data* data) { boost::unique_lock<boost::mutex> lock(queuelock); while(1) { while(jobqueue.empty() && !queue_terminate) { queuecond.wait(lock); } // at this point, we have the lock, either // jobqueue has something, or queue_terminate is set if (queue_terminate) break; assert(!jobqueue.empty()); zmq_msg_vector* msg = jobqueue.front(); jobqueue.pop(); // we process the job outside of the lock lock.unlock(); // this function also frees msg process_job(data, msg); lock.lock(); } }
// returns true if the job was posted, and false if it was // processed immediately bool post_job(T& e) { if (m_num_threads == 0) { // if we don't have any worker threads // just do the work immediately process_job(e, false); return false; } else { retain_job(e); mutex::scoped_lock l(m_mutex); m_queue.push_back(e); // we only need to signal if the threads // may have been put to sleep. If the size // previous to adding the new job was > 0 // they don't need waking up. if (m_queue.size() == 1) m_cond.signal_all(l); return true; } }
void *thread_func (void *data) { while (1) { sem_wait (&job_count); struct job *next_job; /* IMPORTANT: in order to modify a pointer passed into function, * make the parameter as pointer to this pointer.*/ struct job **job_queue = (struct job **)data; pthread_mutex_lock (&job_queue_mutex); /* Remove a job from the head of the queue.*/ next_job = *job_queue; *job_queue = (*job_queue)->next; pthread_mutex_unlock (&job_queue_mutex); process_job (next_job); free (next_job); } return NULL; }
static void recv_output(void *arg) { struct scan_peer *peer = arg; uint32_t output_len; (void)memcpy(&output_len, peer->tmp_buf, 4); output_len = ntohl(output_len); if (output_len == 0) { LIST_REMOVE(peer, peer_link); process_job(peer->job, JOB_DONE); assign_job(peer); return; } if (output_len == 0xffffff) { warnx("Invalid output len received from peer"); kill_peer(peer); return; } peer->job->scan_output = xmalloc(output_len + 1); peer->job->scan_output[output_len] = '\0'; peer->output_len = output_len; deferred_read(peer->fd, peer->job->scan_output, output_len, peer, finish_job, kill_peer); }