int myThreadCancel(thread_t pThread) { sigprocmask(SIG_BLOCK, &sigProcMask, NULL); TCB currentThread = threadsQueue->currentThread; if ((currentThread != NULL) && (currentThread->threadID != pThread)) { TCB targetThread = searchThread(pThread, threadsQueue); if (targetThread != NULL) { clearBlockedThreads(targetThread); //printf("MyThread: Thread %ld cancelado\n", targetThread->threadID); targetThread->threadCompleted = 1; sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return SUCESS; } else { sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return INVALID_OPERATION; } } else { sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return INVALID_OPERATION; } }
long myThreadGetTimeExecution(thread_t pThread) { sigprocmask(SIG_BLOCK, &sigProcMask, NULL); TCB targetThread = searchThread(pThread, threadsQueue); long actualQuantums = threadsQueue->quantums; sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return (actualQuantums - targetThread->startQuantum) * threadsQueue->quantum; }
int myThreadDetach(thread_t pThread) { TCB targetThread = searchThread(pThread, threadsQueue); if(targetThread != NULL) { targetThread->detach = 1; return SUCESS; } else { return INVALID_OPERATION; } }
void BFS_3D::run(int x, int y, int z) { if (running) { //error "Search already running" return; } for (int i = 0; i < dim_xyz; i++) if (distance_grid[i] != WALL) distance_grid[i] = UNDISCOVERED; origin = getNode(x, y, z); queue_head = 0; queue_tail = 1; queue[0] = origin; distance_grid[origin] = 0; boost::thread searchThread(&BFS_3D::search, this, dim_x, dim_xy, distance_grid, queue, queue_head, queue_tail); running = true; }
int myThreadJoin(thread_t pThread, void **pStatus) { sigprocmask(SIG_BLOCK, &sigProcMask, NULL); TCB currentThread = threadsQueue->currentThread; TCB targetThread = searchThread(pThread, threadsQueue); if (currentThread == targetThread || currentThread == NULL || (targetThread != NULL && targetThread->detach)) { sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return INVALID_OPERATION; } else { if (targetThread == NULL || targetThread->threadCompleted) { DeadThreadNode deadThreadNode = searchDeadThread(deadThreadsQueue, pThread); sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); if (deadThreadNode != NULL) { if (pStatus != NULL) { *pStatus = *(deadThreadNode->returnValue); } deleteDeadThread(deadThreadsQueue, pThread); return SUCESS; } else { return INVALID_OPERATION; } } else { insertWaitingThread(targetThread, currentThread); int isBlocked = currentThread->threadBlocked; sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); while (isBlocked) { isBlocked = currentThread->threadBlocked; } sigprocmask(SIG_BLOCK, &sigProcMask, NULL); DeadThreadNode deadThreadNode = searchDeadThread(deadThreadsQueue, pThread); if(deadThreadNode != NULL) { if (pStatus != NULL) { *pStatus = *(deadThreadNode->returnValue); } if(((deadThreadNode->threadsWaiting) - 1) == 0) { deleteDeadThread(deadThreadsQueue, pThread); } else { deadThreadNode->threadsWaiting--; } sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return SUCESS; } else { //printf("MyThread: Un thread anterior a este ha realizado el join primero, intente realizando el join para ambos threads antes que el thread al cual desea hacer el join haya finalizado\n"); sigprocmask(SIG_UNBLOCK, &sigProcMask, NULL); return SUCESS; } } } }