pbs_queue *next_queue( all_queues *aq, int *iter) { pbs_queue *pque; pthread_mutex_lock(aq->allques_mutex); pque = (pbs_queue *)next_thing(aq->ra,iter); if (pque != NULL) lock_queue(pque, "next_queue", (char *)NULL, LOGLEVEL); pthread_mutex_unlock(aq->allques_mutex); if (pque != NULL) { if (pque->q_being_recycled != FALSE) { unlock_queue(pque, __func__, (char *)"recycled queue", LOGLEVEL); pque = next_queue(aq, iter); } } return(pque); } /* END next_queue() */
int queue_distroy(queue_t *queue) { int ret = 0; lock_queue(queue); if( !is_empty_queue(queue) ) { debug(DEBUG_UTILS, "Distroy not empty queue:%s",queue->name); ret = -1; goto d_end; } free(queue->name); if( queue->need_free == 1 ) { unlock_queue(queue); free(queue); return 0; } queue->tail =(queue_body_t *) QUEUE_TAIL_NULL; queue->head =(queue_body_t *) QUEUE_HEAD_NULL; queue->maxLength = 0; d_end: unlock_queue(queue); return ret; }
int remove_queue( all_queues *aq, pbs_queue *pque) { int rc = PBSE_NONE; int index; char log_buf[1000]; if (pthread_mutex_trylock(aq->allques_mutex)) { unlock_queue(pque, __func__, NULL, LOGLEVEL); lock_allques_mutex(aq, __func__, "1", LOGLEVEL); lock_queue(pque, __func__, NULL, LOGLEVEL); } if ((index = get_value_hash(aq->ht,pque->qu_qs.qu_name)) < 0) rc = THING_NOT_FOUND; else { remove_thing_from_index(aq->ra,index); remove_hash(aq->ht,pque->qu_qs.qu_name); } snprintf(log_buf, sizeof(log_buf), "index = %d, name = %s", index, pque->qu_qs.qu_name); log_err(-1, __func__, log_buf); unlock_allques_mutex(aq, __func__, "2", LOGLEVEL); return(rc); } /* END remove_queue() */
pbs_queue *lock_queue_with_job_held( pbs_queue *pque, job **pjob_ptr) { char jobid[PBS_MAXSVRJOBID]; job *pjob = *pjob_ptr; if (pque != NULL) { if (pthread_mutex_trylock(pque->qu_mutex)) { /* if fail */ strcpy(jobid, pjob->ji_qs.ji_jobid); unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL); lock_queue(pque, __func__, NULL, LOGLEVEL); if ((pjob = svr_find_job(jobid, TRUE)) == NULL) { unlock_queue(pque, __func__, NULL, 0); pque = NULL; *pjob_ptr = NULL; } } } return(pque); } /* END get_jobs_queue() */
static queue_body_t * __del_queue(queue_head *queue) { struct queue_body * re = NULL; lock_queue(queue); if( is_empty_queue(queue) ) { debug(DEBUG_UTILS, "Queue:%s is empty",queue->name); goto out_pos; } re = queue->head; queue->head = queue->head->next; if(queue->head == QUEUE_TAIL_NULL) { queue->tail =(queue_body_t *) QUEUE_TAIL_NULL; queue->head =(queue_body_t *) QUEUE_HEAD_NULL; } queue->curLength--; out_pos: unlock_queue(queue); return re; }
pbs_queue *lock_queue_with_job_held( pbs_queue *pque, job **pjob_ptr) { char jobid[PBS_MAXSVRJOBID + 1]; job *pjob = *pjob_ptr; if (pque != NULL) { if (LOGLEVEL >= 10 ) log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_QUEUE, __func__, pque->qu_qs.qu_name); if (pthread_mutex_trylock(pque->qu_mutex)) { /* if fail */ strcpy(jobid, pjob->ji_qs.ji_jobid); unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL); lock_queue(pque, __func__, NULL, LOGLEVEL); if ((pjob = svr_find_job(jobid, TRUE)) == NULL) { unlock_queue(pque, __func__, NULL, 0); pque = NULL; *pjob_ptr = NULL; } } } return(pque); } /* END lock_queue_with_job_held() */
static int enqueue_work_item_and_wait_for_result(work_q_item_t *item) { /* put the work item at the end of the work queue */ lock_queue(&g_work_q); sq_addlast(&item->link, &(g_work_q.q)); /* Adjust the queue size and potentially the maximum queue size */ if (++g_work_q.size > g_work_q.max_size) { g_work_q.max_size = g_work_q.size; } unlock_queue(&g_work_q); /* tell the work thread that work is available */ px4_sem_post(&g_work_queued_sema); /* wait for the result */ px4_sem_wait(&item->wait_sem); int result = item->result; destroy_work_item(item); return result; }
pbs_queue *next_queue( all_queues *aq, int *iter) { pbs_queue *pque; lock_allques_mutex(aq, __func__, NULL, LOGLEVEL); pque = (pbs_queue *)next_thing(aq->ra,iter); if (pque != NULL) lock_queue(pque, "next_queue", NULL, LOGLEVEL); unlock_allques_mutex(aq, __func__, NULL, LOGLEVEL); if (pque != NULL) { if (pque->q_being_recycled != FALSE) { unlock_queue(pque, __func__, "recycled queue", LOGLEVEL); pque = next_queue(aq, iter); } } return(pque); } /* END next_queue() */
static int __add_queue(queue_t *queue, queue_body_t *toadd) { int ret = 0; lock_queue(queue); if(is_full_queue(queue)) { debug(DEBUG_UTILS, "Add to full queue"); ret = -1; goto unl; } toadd->next = (queue_body_t *)QUEUE_TAIL_NULL; if( is_tail_null(queue) ) { queue->tail = toadd; queue->head = toadd; queue->curLength = 1; ret = 0; goto unl; } queue->tail->next = toadd; queue->tail = toadd; queue->curLength++; unl: unlock_queue(queue); return ret; }
static work_q_item_t * create_work_item(void) { work_q_item_t *item; /* Try to reuse item from free item queue */ lock_queue(&g_free_q); if ((item = (work_q_item_t *)sq_remfirst(&(g_free_q.q)))) { g_free_q.size--; } unlock_queue(&g_free_q); /* If we there weren't any free items then obtain memory for a new ones */ if (item == NULL) { item = (work_q_item_t *)malloc(k_work_item_allocation_chunk_size * sizeof(work_q_item_t)); if (item) { item->first = 1; lock_queue(&g_free_q); for (size_t i = 1; i < k_work_item_allocation_chunk_size; i++) { (item + i)->first = 0; sq_addfirst(&(item + i)->link, &(g_free_q.q)); } /* Update the queue size and potentially the maximum queue size */ g_free_q.size += k_work_item_allocation_chunk_size - 1; if (g_free_q.size > g_free_q.max_size) { g_free_q.max_size = g_free_q.size; } unlock_queue(&g_free_q); } } /* If we got one then lock the item*/ if (item) { px4_sem_init(&item->wait_sem, 1, 0); /* Caller will wait on this... initially locked */ } /* return the item pointer, or NULL if all failed */ return item; }
int queue_cpy_del(struct queue_head *from, struct queue_head *to) { lock_queue(from); lock_queue(to); to->head = from->head; to->tail = from->tail; from->head = (queue_body_t *) QUEUE_HEAD_NULL; from->tail = (queue_body_t *) QUEUE_TAIL_NULL; to->curLength = from->curLength; from->curLength = 0; unlock_queue(to); unlock_queue(from); return 0; }
pbs_queue *que_alloc( char *name, int sv_qs_mutex_held) { static char *mem_err = "no memory"; int i; pbs_queue *pq; pq = (pbs_queue *)calloc(1, sizeof(pbs_queue)); if (pq == NULL) { log_err(errno, __func__, mem_err); return(NULL); } pq->qu_qs.qu_type = QTYPE_Unset; pq->qu_mutex = calloc(1, sizeof(pthread_mutex_t)); pq->qu_jobs = calloc(1, sizeof(struct all_jobs)); pq->qu_jobs_array_sum = calloc(1, sizeof(struct all_jobs)); if ((pq->qu_mutex == NULL) || (pq->qu_jobs == NULL) || (pq->qu_jobs_array_sum == NULL)) { log_err(ENOMEM, __func__, mem_err); return(NULL); } initialize_all_jobs_array(pq->qu_jobs); initialize_all_jobs_array(pq->qu_jobs_array_sum); pthread_mutex_init(pq->qu_mutex,NULL); lock_queue(pq, "que_alloc", NULL, LOGLEVEL); strncpy(pq->qu_qs.qu_name, name, PBS_MAXQUEUENAME); insert_queue(&svr_queues,pq); if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque++; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); /* set the working attributes to "unspecified" */ for (i = 0; i < QA_ATR_LAST; i++) { clear_attr(&pq->qu_attr[i], &que_attr_def[i]); } return(pq); } /* END que_alloc() */
int32_t queue_size(queue_t *queue) { int32_t count = 0; struct queueitem *tmp; lock_queue(queue); DL_COUNT(queue->list,tmp,count); portable_mutex_unlock(&queue->mutex); return count; }
pbs_queue *find_queuebyname( char *quename) /* I */ { char *pc; pbs_queue *pque = NULL; char qname[PBS_MAXDEST + 1]; char log_buf[LOCAL_LOG_BUF_SIZE+1]; int i; snprintf(qname, sizeof(qname), "%s", quename); if (LOGLEVEL >= 7) { sprintf(log_buf, "%s", quename); LOG_EVENT(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf); } pc = strchr(qname, (int)'@'); /* strip off server (fragment) */ if (pc != NULL) *pc = '\0'; lock_allques_mutex(&svr_queues, __func__, NULL, LOGLEVEL); i = get_value_hash(svr_queues.ht,qname); if (i >= 0) { pque = svr_queues.ra->slots[i].item; } if (pque != NULL) lock_queue(pque, __func__, NULL, LOGLEVEL); unlock_allques_mutex(&svr_queues, __func__, NULL, LOGLEVEL); if (pque != NULL) { if (pque->q_being_recycled != FALSE) { unlock_queue(pque, __func__, "recycled queue", LOGLEVEL); pque = NULL; } } if (pc != NULL) *pc = '@'; /* restore '@' server portion */ return(pque); } /* END find_queuebyname() */
void queue_enqueue(char *name,queue_t *queue,struct queueitem *item) { if ( queue->list == 0 && name != 0 && name[0] != 0 ) safecopy(queue->name,name,sizeof(queue->name)); if ( item == 0 ) { printf("FATAL type error: queueing empty value\n"), getchar(); return; } lock_queue(queue); DL_APPEND(queue->list,item); portable_mutex_unlock(&queue->mutex); //printf("name.(%s) append.%p list.%p\n",name,item,queue->list); }
static inline work_q_item_t * dequeue_work_item(void) { work_q_item_t *work; /* retrieve the 1st item on the work queue */ lock_queue(&g_work_q); if ((work = (work_q_item_t *)sq_remfirst(&g_work_q.q))) g_work_q.size--; unlock_queue(&g_work_q); return work; }
static inline void destroy_work_item(work_q_item_t *item) { sem_destroy(&item->wait_sem); /* Destroy the item lock */ /* Return the item to the free item queue for later reuse */ lock_queue(&g_free_q); sq_addfirst(&item->link, &(g_free_q.q)); /* Update the queue size and potentially the maximum queue size */ if (++g_free_q.size > g_free_q.max_size) g_free_q.max_size = g_free_q.size; unlock_queue(&g_free_q); }
void *queue_dequeue(queue_t *queue,int32_t offsetflag) { struct queueitem *item = 0; lock_queue(queue); if ( queue->list != 0 ) { item = queue->list; DL_DELETE(queue->list,item); //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list); } portable_mutex_unlock(&queue->mutex); if ( item != 0 && offsetflag != 0 ) return((void *)((long)item + sizeof(struct queueitem))); else return(item); }
pbs_queue *next_queue_from_recycler( all_queues *aq, all_queues_iterator *iter) { pbs_queue *pq; aq->lock(); pq = iter->get_next_item(); if (pq != NULL) lock_queue(pq, __func__, NULL, LOGLEVEL); aq->unlock(); return(pq); } /* END next_queue_from_recycler() */
void gpu_worker(void *arg) { hs_worker *worker_arg = (hs_worker *) arg; bind_to_cpu(worker_arg); init_cuda(worker_arg->device_id); // printf("GPU I am id:%d\n", worker_arg->worker_id); pthread_mutex_lock(&worker_arg->mutex); worker_arg->initialized = 1; pthread_cond_signal(&worker_arg->ready); pthread_mutex_unlock(&worker_arg->mutex); _task_t task; while (is_running()) { lock_queue(worker_arg->task_queue); task = pop_task(worker_arg->task_queue); if (task == NULL) { if (is_running()) sleep_worker(worker_arg); unlock_queue(worker_arg->task_queue); continue; } unlock_queue(worker_arg->task_queue); if ((task->task->arch_type & worker_arg->arch) != worker_arg->arch) { push_task(worker_arg->task_queue, task); continue; } execute_task(worker_arg, task); } deinit_cuda(); pthread_exit((void*) 0); }
pbs_queue *find_queuebyname( char *quename) /* I */ { char *pc; pbs_queue *pque = NULL; char qname[PBS_MAXDEST + 1]; int i; snprintf(qname, sizeof(qname), "%s", quename); pc = strchr(qname, (int)'@'); /* strip off server (fragment) */ if (pc != NULL) *pc = '\0'; pthread_mutex_lock(svr_queues.allques_mutex); i = get_value_hash(svr_queues.ht,qname); if (i >= 0) { pque = svr_queues.ra->slots[i].item; } if (pque != NULL) lock_queue(pque, __func__, NULL, LOGLEVEL); pthread_mutex_unlock(svr_queues.allques_mutex); if (pque != NULL) { if (pque->q_being_recycled != FALSE) { unlock_queue(pque, __func__, "recycled queue", LOGLEVEL); pque = NULL; } } if (pc != NULL) *pc = '@'; /* restore '@' server portion */ return(pque); } /* END find_queuebyname() */
pbs_queue *lock_queue_with_job_held( pbs_queue *pque, job **pjob_ptr) { char jobid[PBS_MAXSVRJOBID + 1]; job *pjob = *pjob_ptr; char log_buf[LOCAL_LOG_BUF_SIZE]; if (pque != NULL) { if (pthread_mutex_trylock(pque->qu_mutex)) { /* if fail */ strcpy(jobid, pjob->ji_qs.ji_jobid); unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL); lock_queue(pque, __func__, NULL, LOGLEVEL); if ((pjob = svr_find_job(jobid, TRUE)) == NULL) { unlock_queue(pque, __func__, NULL, LOGLEVEL); pque = NULL; *pjob_ptr = NULL; } } else { if (LOGLEVEL >= 10) { snprintf(log_buf, sizeof(log_buf), "try lock succeeded for queue %s on job %s", pque->qu_qs.qu_name, pjob->ji_qs.ji_jobid); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf); } } } return(pque); } /* END lock_queue_with_job_held() */
pbs_queue *que_alloc( char *name, int sv_qs_mutex_held) { static char *mem_err = (char *)"no memory"; int i; pbs_queue *pq; pq = (pbs_queue *)calloc(1, sizeof(pbs_queue)); if (pq == NULL) { log_err(errno, __func__, mem_err); return(NULL); } pq->qu_qs.qu_type = QTYPE_Unset; pq->qu_mutex = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t)); pq->qu_jobs = (struct all_jobs *)calloc(1, sizeof(struct all_jobs)); pq->qu_jobs_array_sum = (struct all_jobs *)calloc(1, sizeof(struct all_jobs)); if ((pq->qu_mutex == NULL) || (pq->qu_jobs == NULL) || (pq->qu_jobs_array_sum == NULL)) { log_err(ENOMEM, __func__, mem_err); free(pq); return(NULL); } initialize_all_jobs_array(pq->qu_jobs); initialize_all_jobs_array(pq->qu_jobs_array_sum); pthread_mutex_init(pq->qu_mutex,NULL); lock_queue(pq, __func__, NULL, LOGLEVEL); snprintf(pq->qu_qs.qu_name, sizeof(pq->qu_qs.qu_name), "%s", name); insert_queue(&svr_queues,pq); if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque++; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); /* set up the user info struct */ pq->qu_uih = (user_info_holder *)calloc(1, sizeof(user_info_holder)); initialize_user_info_holder(pq->qu_uih); /* set the working attributes to "unspecified" */ for (i = 0; i < QA_ATR_LAST; i++) { clear_attr(&pq->qu_attr[i], &que_attr_def[i]); } /* Set route_retry_thread_id in case this is a routing queue */ pq->route_retry_thread_id = -1; return(pq); } /* END que_alloc() */
int get_parent_dest_queues( char *queue_parent_name, char *queue_dest_name, pbs_queue **parent, pbs_queue **dest, job **pjob_ptr) { pbs_queue *pque_parent; pbs_queue *pque_dest; char jobid[PBS_MAXSVRJOBID + 1]; char log_buf[LOCAL_LOG_BUF_SIZE + 1]; job *pjob = *pjob_ptr; int index_parent; int index_dest; int rc = PBSE_NONE; strcpy(jobid, pjob->ji_qs.ji_jobid); if ((queue_parent_name != NULL) && (queue_dest_name != NULL)) { if (!strcmp(queue_parent_name, queue_dest_name)) { /* parent and destination are the same. Job is already in destnation queue. return */ snprintf(log_buf, sizeof(log_buf), "parent and destination queues are the same: parent %s - dest %s. jobid: %s", queue_parent_name, queue_dest_name, jobid); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf); return(-1); } } else return(-1); unlock_ji_mutex(pjob, __func__, (char *)"1", LOGLEVEL); unlock_queue(*parent, __func__, (char *)NULL, 0); *parent = NULL; *dest = NULL; pthread_mutex_lock(svr_queues.allques_mutex); index_parent = get_value_hash(svr_queues.ht, queue_parent_name); index_dest = get_value_hash(svr_queues.ht, queue_dest_name); if ((index_parent < 0) || (index_dest < 0)) { rc = -1; } else { /* good path */ pque_parent = svr_queues.ra->slots[index_parent].item; pque_dest = svr_queues.ra->slots[index_dest].item; if ((pque_parent == NULL) || (pque_dest == NULL)) { rc = -1; } else { /* SUCCESS! */ lock_queue(pque_parent, __func__, (char *)NULL, 0); lock_queue(pque_dest, __func__, (char *)NULL, 0); *parent = pque_parent; *dest = pque_dest; rc = PBSE_NONE; } } pthread_mutex_unlock(svr_queues.allques_mutex); if ((*pjob_ptr = svr_find_job(jobid, TRUE)) == NULL) rc = -1; return(rc); } /* END get_parent_dest_queues() */
int get_parent_dest_queues( char *queue_parent_name, char *queue_dest_name, pbs_queue **parent, pbs_queue **dest, job **pjob_ptr) { pbs_queue *pque_parent; pbs_queue *pque_dest; char jobid[PBS_MAXSVRJOBID + 1]; job *pjob = *pjob_ptr; int index_parent; int index_dest; int rc = PBSE_NONE; strcpy(jobid, pjob->ji_qs.ji_jobid); unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL); unlock_queue(*parent, __func__, NULL, 0); *parent = NULL; *dest = NULL; pthread_mutex_lock(svr_queues.allques_mutex); index_parent = get_value_hash(svr_queues.ht, queue_parent_name); index_dest = get_value_hash(svr_queues.ht, queue_dest_name); if ((index_parent < 0) || (index_dest < 0)) { rc = -1; } else { /* good path */ pque_parent = svr_queues.ra->slots[index_parent].item; pque_dest = svr_queues.ra->slots[index_dest].item; if ((pque_parent == NULL) || (pque_dest == NULL)) { rc = -1; } else { /* SUCCESS! */ lock_queue(pque_parent, __func__, NULL, 0); lock_queue(pque_dest, __func__, NULL, 0); *parent = pque_parent; *dest = pque_dest; rc = PBSE_NONE; } } pthread_mutex_unlock(svr_queues.allques_mutex); if ((*pjob_ptr = svr_find_job(jobid, TRUE)) == NULL) rc = -1; return(rc); } /* END get_parent_dest_queues() */
int get_parent_dest_queues( char *queue_parent_name, char *queue_dest_name, pbs_queue **parent, pbs_queue **dest, job **pjob_ptr) { pbs_queue *pque_parent; pbs_queue *pque_dest; char jobid[PBS_MAXSVRJOBID + 1]; char log_buf[LOCAL_LOG_BUF_SIZE + 1]; job *pjob = *pjob_ptr; int index_parent; int index_dest; int rc = PBSE_NONE; if (LOGLEVEL >= 7) { sprintf(log_buf, "%s", pjob->ji_qs.ji_jobid); LOG_EVENT(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf); } strcpy(jobid, pjob->ji_qs.ji_jobid); unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL); unlock_queue(*parent, __func__, NULL, 0); *parent = NULL; *dest = NULL; lock_allques_mutex(&svr_queues, __func__, NULL, LOGLEVEL); index_parent = get_value_hash(svr_queues.ht, queue_parent_name); index_dest = get_value_hash(svr_queues.ht, queue_dest_name); if ((index_parent < 0) || (index_dest < 0)) { rc = -1; } else { /* good path */ pque_parent = svr_queues.ra->slots[index_parent].item; pque_dest = svr_queues.ra->slots[index_dest].item; if ((pque_parent == NULL) || (pque_dest == NULL)) { rc = -1; } else { /* SUCCESS! */ lock_queue(pque_parent, __func__, NULL, 0); lock_queue(pque_dest, __func__, NULL, 0); *parent = pque_parent; *dest = pque_dest; rc = PBSE_NONE; } } unlock_allques_mutex(&svr_queues, __func__, NULL, LOGLEVEL); if ((*pjob_ptr = svr_find_job(jobid, TRUE)) == NULL) rc = -1; return(rc); } /* END get_parent_dest_queues() */