Exemple #1
0
/* Add work to the thread pool */
int
thpool_add_work(thpool_* thpool_p, void *(*function_p)(void*), void* arg_p,
                char *tag)
{
	job *newjob;

	newjob = (struct job*) malloc(sizeof(struct job));
	if (newjob == NULL) {
		fprintf(stderr, "thpool_add_work(): Could not allocate memory for new job\n");
		return -1;
	}

	/* add function and argument */
	newjob->function = function_p;
	newjob->arg = arg_p;
    newjob->tag = (char *) calloc(strlen(tag) + 1, sizeof(char));
    (void) strcpy(newjob->tag, tag);

	/* add job to queue */
	pthread_mutex_lock(&thpool_p->jobqueue_p->rwmutex);
	jobqueue_push(thpool_p, newjob);
	pthread_mutex_unlock(&thpool_p->jobqueue_p->rwmutex);

	return 0;
}
Exemple #2
0
bool thpool_add_job(thpool_t* p, void *(*function)(void*), void* args) {
    bool res = jobqueue_push(p->queue, JOB(function, args));

    if (res) {
        pthread_mutex_lock(&p->update_mutex);
        pthread_cond_broadcast(&p->update_cv);
        pthread_mutex_unlock(&p->update_mutex);
    }

    return res;
}
Exemple #3
0
/* Add work to the thread pool */
int thpool_add_work(thpool_* thpool_p, void *(*function_p)(void*), void* arg_p){
	job* newjob;

	newjob=(struct job*)malloc(sizeof(struct job));
	if (newjob==NULL){
		fprintf(stderr, "thpool_add_work(): Could not allocate memory for new job\n");
		return -1;
	}

	/* add function and argument */
	newjob->function=function_p;
	newjob->arg=arg_p;

	/* add job to queue */
	jobqueue_push(&thpool_p->jobqueue, newjob);

	return 0;
}
Exemple #4
0
/**
 * @brief Add work to the job queue
 *
 * Takes an action and its argument and adds it to the threadpool's job queue.
 * If you want to add to work a function with more than one arguments then
 * a way to implement this is by passing a pointer to a structure.
 *
 * NOTICE: You have to cast both the function and argument to not get warnings.
 *
 * @example
 *
 *    void print_num(int num){
 *       printf("%d\n", num);
 *    }
 *
 *    int main() {
 *       ..
 *       int a = 10;
 *       thpool_add_job(thpool, (void*)print_num, (void*)a);
 *       ..
 *    }
 *
 * @param  threadpool    threadpool to which the work will be added
 * @param  function      pointer to function to add as work
 * @param  arg           pointer to an argument
 * @return 0 on successs, -1 otherwise.
 */
static int thpool_add_job(ThPool* thpool,
                          thpool_function_type function,
                          void* arg,
                          const float* avgElapsed,
                          float* elapsed) {
    Job* newjob;

    newjob = (struct Job*) malloc(sizeof(struct Job));
    if (newjob == NULL) {
        fprintf(stderr, "thpool_add_job(): Could not allocate memory for new job\n");
        return -1;
    }

    /* add function and argument */
    newjob->function = function;
    newjob->arg = arg;
    newjob->avgElapsed = avgElapsed;
    newjob->elapsed = elapsed;

    /* add job to queue */
    jobqueue_push(thpool->jobqueue, newjob);

    return 0;
}
Exemple #5
0
/****************************************************************************
  Message Handler: submit_job_*(func, uniq, arg)
 ****************************************************************************/
int msg_submit_job(Client *cli, unsigned char *arg, int argsize, gboolean background, gboolean high)
{
    unsigned char *args[3];
    int last_arg_len = parse_args(args, 3, arg, argsize);

    int is_uniq = args[1][0] != 0;

    const gchar *func = g_intern_string((char*)args[0]);
    // printf(">> [%s] = %.8x\n", args[0], func);

    /* look for a duplicate job - if one exists, add to listeners */
    if (is_uniq) {
        UniqJobKey uk;
        uk.func = func;
        if (args[1][0] == '-' && args[1][1] == 0) {
            uk.uniq = args[2];
            uk.uniq_len = last_arg_len;
        } else {
            uk.uniq = args[1];
            uk.uniq_len = strlen((char*)args[1]);
        }
        Job *job = g_hash_table_lookup(g_uniq_jobs, &uk);
        if (job) {
            #if DEBUG
            g_debug("[%s] submit_job - merging %s:%s -> %s", cli->id, job->func, job->uniq, job->handle);
            #endif
            if (!job->background) {
                g_ptr_array_add_uniq(job->listeners, cli);
                client_listen_to(cli, job);
            }
            MemBlock *block = new_response(MSG_JOB_CREATED, strlen(job->handle), (unsigned char *)job->handle);
            client_send(cli, block);
            return 0;
        }
    }

    Job *job = job_new();
    generate_job_handle(job->handle);
    job->func = func;
    if (last_arg_len == 0)
        job->arg = (unsigned char *)"";
    else {
        job->arg = malloc(last_arg_len);
        memcpy(job->arg, args[2], last_arg_len);
    }
    job->arg_len = last_arg_len;
    job->background = background;
    if (!background) {
        g_ptr_array_add(job->listeners, cli);
    }
    g_hash_table_insert(g_jobs, job->handle, job);

    #if DEBUG
    g_debug("[%s] submit_job: %s, %s -> %s", cli->id, args[0], args[1], job->handle);
    #endif

    jobqueue_push(g_jobqueue, job, high);
    if (!background) {
        client_listen_to(cli, job);
    }

    job->is_uniq = is_uniq;
    if (is_uniq) {
        job->uniq = strdup((char*)args[1]);
        job->uniq_key.func = func;
        if (job->uniq[0] == '-' && job->uniq[1] == 0) {
            job->uniq_key.uniq = job->arg;
            job->uniq_key.uniq_len = job->arg_len;
        } else {
            job->uniq_key.uniq = (unsigned char *)job->uniq;
            job->uniq_key.uniq_len = strlen(job->uniq);
        }
        g_hash_table_insert(g_uniq_jobs, &job->uniq_key, job);
        #if DEBUG
        g_debug("[%s] \tadded uniq job %s", cli->id, job->handle);
        #endif
    }

    MemBlock *block = new_response(MSG_JOB_CREATED, strlen(job->handle), (unsigned char *)job->handle);
    client_send(cli, block);
    wake_up_sleepers(job->func);

    return 0;
}