fixed spacing

This commit is contained in:
elvis
2022-06-08 12:33:03 +02:00
parent f228ee5462
commit 11879cf252

View File

@ -17,7 +17,7 @@
* @function void *threadpool_thread(void *threadpool) * @function void *threadpool_thread(void *threadpool)
* @brief funzione eseguita dal thread worker che appartiene al pool * @brief funzione eseguita dal thread worker che appartiene al pool
*/ */
static void *workerpool_thread(void *threadpool) { static void *workerpool_thread(void *threadpool) {
threadpool_t *pool = (threadpool_t *)threadpool; // cast threadpool_t *pool = (threadpool_t *)threadpool; // cast
taskfun_t task; // generic task taskfun_t task; // generic task
pthread_t self = pthread_self(); pthread_t self = pthread_self();
@ -34,28 +34,28 @@ static void *workerpool_thread(void *threadpool) {
LOCK_RETURN(&(pool->lock), NULL); LOCK_RETURN(&(pool->lock), NULL);
for (;;) { for (;;) {
// in attesa di un messaggio, controllo spurious wakeups. // in attesa di un messaggio, controllo spurious wakeups.
while((pool->count == 0) && (!pool->exiting)) { while((pool->count == 0) && (!pool->exiting)) {
pthread_cond_wait(&(pool->cond), &(pool->lock)); pthread_cond_wait(&(pool->cond), &(pool->lock));
} }
if (pool->exiting > 1) break; // exit forzato, esco immediatamente if (pool->exiting > 1) break; // exit forzato, esco immediatamente
// devo uscire ma ci sono messaggi pendenti // devo uscire ma ci sono messaggi pendenti
if (pool->exiting == 1 && !pool->count) break; if (pool->exiting == 1 && !pool->count) break;
// nuovo task // nuovo task
task.fun = pool->pending_queue[pool->head].fun; task.fun = pool->pending_queue[pool->head].fun;
task.arg = pool->pending_queue[pool->head].arg; task.arg = pool->pending_queue[pool->head].arg;
pool->head++; pool->count--; pool->head++; pool->count--;
pool->head = (pool->head == abs(pool->queue_size)) ? 0 : pool->head; pool->head = (pool->head == abs(pool->queue_size)) ? 0 : pool->head;
pool->taskonthefly++; pool->taskonthefly++;
UNLOCK_RETURN(&(pool->lock), NULL); UNLOCK_RETURN(&(pool->lock), NULL);
// eseguo la funzione
(*(task.fun))(task.arg);
// eseguo la funzione
(*(task.fun))(task.arg);
LOCK_RETURN(&(pool->lock), NULL); LOCK_RETURN(&(pool->lock), NULL);
pool->taskonthefly--; pool->taskonthefly--;
} }
@ -69,22 +69,22 @@ static void *workerpool_thread(void *threadpool) {
static int freePoolResources(threadpool_t *pool) { static int freePoolResources(threadpool_t *pool) {
if(pool->threads) { if(pool->threads) {
free(pool->threads); free(pool->threads);
free(pool->pending_queue); free(pool->pending_queue);
pthread_mutex_destroy(&(pool->lock)); pthread_mutex_destroy(&(pool->lock));
pthread_cond_destroy(&(pool->cond)); pthread_cond_destroy(&(pool->cond));
} }
free(pool); free(pool);
return 0; return 0;
} }
threadpool_t *createThreadPool(int numthreads, int pending_size) { threadpool_t *createThreadPool(int numthreads, int pending_size) {
if(numthreads <= 0 || pending_size < 0) { if(numthreads <= 0 || pending_size < 0) {
errno = EINVAL; errno = EINVAL;
return NULL; return NULL;
} }
threadpool_t *pool = (threadpool_t *)malloc(sizeof(threadpool_t)); threadpool_t *pool = (threadpool_t *)malloc(sizeof(threadpool_t));
if (pool == NULL) return NULL; if (pool == NULL) return NULL;
@ -115,20 +115,20 @@ threadpool_t *createThreadPool(int numthreads, int pending_size) {
return NULL; return NULL;
} }
for(int i = 0; i < numthreads; i++) { for(int i = 0; i < numthreads; i++) {
if(pthread_create(&(pool->threads[i]), NULL, if(pthread_create(&(pool->threads[i]), NULL,
workerpool_thread, (void*)pool) != 0) { workerpool_thread, (void*)pool) != 0) {
/* errore fatale, libero tutto forzando l'uscita dei threads */ /* errore fatale, libero tutto forzando l'uscita dei threads */
destroyThreadPool(pool, 1); destroyThreadPool(pool, 1);
errno = EFAULT; errno = EFAULT;
return NULL; return NULL;
} }
pool->numthreads++; pool->numthreads++;
} }
return pool; return pool;
} }
int destroyThreadPool(threadpool_t *pool, int force) { int destroyThreadPool(threadpool_t *pool, int force) {
if(pool == NULL || force < 0) { if(pool == NULL || force < 0) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
@ -171,30 +171,30 @@ int addToThreadPool(threadpool_t *pool, void (*f)(void *), void *arg) {
UNLOCK_RETURN(&(pool->lock),-1); UNLOCK_RETURN(&(pool->lock),-1);
return 1; // esco con valore "coda piena" return 1; // esco con valore "coda piena"
} }
if (pool->taskonthefly >= pool->numthreads) { if (pool->taskonthefly >= pool->numthreads) {
if (nopending) { if (nopending) {
// tutti i thread sono occupati e non si gestiscono task pendenti // tutti i thread sono occupati e non si gestiscono task pendenti
assert(pool->count == 0); assert(pool->count == 0);
UNLOCK_RETURN(&(pool->lock),-1); UNLOCK_RETURN(&(pool->lock),-1);
return 1; // esco con valore "coda piena" return 1; // esco con valore "coda piena"
} }
} }
pool->pending_queue[pool->tail].fun = f; pool->pending_queue[pool->tail].fun = f;
pool->pending_queue[pool->tail].arg = arg; pool->pending_queue[pool->tail].arg = arg;
pool->count++; pool->count++;
pool->tail++; pool->tail++;
if (pool->tail >= queue_size) pool->tail = 0; if (pool->tail >= queue_size) pool->tail = 0;
int r; int r;
if((r=pthread_cond_signal(&(pool->cond))) != 0) { if((r=pthread_cond_signal(&(pool->cond))) != 0) {
UNLOCK_RETURN(&(pool->lock),-1); UNLOCK_RETURN(&(pool->lock),-1);
errno = r; errno = r;
return -1; return -1;
} }
UNLOCK_RETURN(&(pool->lock),-1); UNLOCK_RETURN(&(pool->lock),-1);
return 0; return 0;
} }
@ -204,11 +204,11 @@ int addToThreadPool(threadpool_t *pool, void (*f)(void *), void *arg) {
* @function void *thread_proxy(void *argl) * @function void *thread_proxy(void *argl)
* @brief funzione eseguita dal thread worker che non appartiene al pool * @brief funzione eseguita dal thread worker che non appartiene al pool
*/ */
static void *proxy_thread(void *arg) { static void *proxy_thread(void *arg) {
taskfun_t *task = (taskfun_t*)arg; taskfun_t *task = (taskfun_t*)arg;
// eseguo la funzione // eseguo la funzione
(*(task->fun))(task->arg); (*(task->fun))(task->arg);
free(task); free(task);
return NULL; return NULL;
} }
@ -220,7 +220,7 @@ int spawnThread(void (*f)(void*), void* arg) {
return -1; return -1;
} }
taskfun_t *task = malloc(sizeof(taskfun_t)); // la memoria verra' liberata dal proxy taskfun_t *task = malloc(sizeof(taskfun_t)); // la memoria verra' liberata dal proxy
if (!task) return -1; if (!task) return -1;
task->fun = f; task->fun = f;
task->arg = arg; task->arg = arg;