static void test_unblock_op(void) { rtems_status_code sc; rtems_id task_id; Thread_Control *executing; Scheduler_SMP_Node *executing_node; Thread_Control *other; size_t i; task_id = start_task(3); _Thread_Disable_dispatch(); executing = _Thread_Executing; executing_node = _Scheduler_SMP_Thread_get_node(executing); other = get_thread_by_id(task_id); for (i = 0; i < RTEMS_ARRAY_SIZE(states); ++i) { test_case_unblock_op( executing, executing_node, other, states[i] ); } _Thread_Change_priority(executing, 1, true); rtems_test_assert(executing_node->state == SCHEDULER_SMP_NODE_SCHEDULED); _Thread_Enable_dispatch(); sc = rtems_task_delete(task_id); rtems_test_assert(sc == RTEMS_SUCCESSFUL); }
int class_mutex_unlock(class_mutex_ptr cmutex) { int thread_id = pthread_self(); pthread_mutex_lock(&count_mutex); // Lock pthread_t this_thread_id = pthread_self(); pthread_monitor *this_monitor = get_thread_by_id(this_thread_id); pthread_monitor *other_monitor = get_other_thread_by_id(this_thread_id); if (this_monitor == NULL || other_monitor == NULL) { exit(1); } assert(this_monitor->count == 1 || this_monitor->count == 2); assert(other_monitor->count == 0); this_monitor->count -= 1; if (this_monitor->count == 0) { pthread_cond_signal(&count_cond); } pthread_mutex_unlock(&count_mutex); // Unlock if(pthread_mutex_unlock(&cmutex->mutex)) { fprintf(stdout, "Error: pthread mutex unlock failed!\n"); return -1; } return 0; }
/** * Cleans up current thread and removes it from the list. */ void cleanup_handler(void *arg) { int i, j; response_s response; games_list_s **list = tdata.games_list; thread_s *thread = NULL; threads_list_s **tlist = tdata.threads_list; response.type = MSG_CLEANUP_RSP; response.error = MSG_RSP_ERROR_NONE; printf("Thread %d cleanup handler goes\n", (int) pthread_self()); for (i = 0; i < SPECTATORS_NO; i++) { if (tdata.spectators_fd[i] != -1) { FD_SET(tdata.spectators_fd[i], tdata.rd_fds); if (play == 1) { send_response_message(tdata.spectators_fd[i], &response); } } } for (j = 0; j < 2; j++) { if (tdata.players_fd[j] != -1) { FD_SET(tdata.players_fd[j], tdata.rd_fds); if (play == 1) { send_response_message(tdata.players_fd[j], &response); } } } get_thread_by_id(*tlist, &thread, tdata.game->id); if (thread != NULL) { pthread_mutex_lock(tdata.threads_list_mutex); remove_thread_from_list(tlist, thread); pthread_mutex_unlock(tdata.threads_list_mutex); } destroy_board(tdata.game->board); pthread_mutex_lock(tdata.games_list_mutex); remove_game_from_list(list, tdata.game); pthread_mutex_unlock(tdata.games_list_mutex); kill(tdata.parent_pid, SIGRTMIN + 11); }
int class_mutex_lock(class_mutex_ptr cmutex) { int thread_id = pthread_self(); pthread_mutex_lock(&count_mutex); // Lock pthread_t this_thread_id = pthread_self(); pthread_monitor *this_monitor = get_thread_by_id(this_thread_id); pthread_monitor *other_monitor = get_other_thread_by_id(this_thread_id); if (other_monitor == NULL) { exit(1); } printf("OTHER COUNT = %d\n", other_monitor->count); while (other_monitor->count > 0) { pthread_cond_wait(&count_cond, &count_mutex); other_monitor = get_other_thread_by_id(this_thread_id); if (other_monitor == NULL) { exit(1); } } this_monitor->count += 1; pthread_mutex_unlock(&count_mutex); // Unlock if(pthread_mutex_lock(&cmutex->mutex)) { fprintf(stdout, "Error: pthread mutex lock failed!\n"); return -1; } return 0; }