static void check_deadlocks() { int key1, key2; char *sk1, *sk2; if (lock_waiting_list == NULL) return; GList *trans_waiting = g_hash_table_get_keys(wait_table); for (int i = 0; i < g_list_length(trans_waiting); i++) { sk1 = g_list_nth_data(trans_waiting, i); if (sk1 == NULL) continue; for (int j = 0; j < g_list_length(trans_waiting); j++) { sk2 = g_list_nth_data(trans_waiting, j); if (sk2 == NULL) continue; key1 = atoi(sk1); key2 = atoi(sk2); if (key1 == key2) continue; if (is_locking(key1, key2) && is_locking(key2, key1)) { printf("* DEADLOCK DETECTED *\n"); // Selecionando pela operação mais velha sk1 = g_strdup_printf("%d", key1); GSList *op_list = g_hash_table_lookup(wait_table, sk1); g_free(sk1); if (g_slist_length(op_list) == 0) continue; struct operation *op = g_slist_nth_data(op_list, 0); if (op == NULL) continue; abort_transaction(op); return; } } } }
/* Add clients file descriptors to fds. */ static void add_clients_fds (fd_set *read, fd_set *write) { int i; for (i = 0; i < CLIENTS_MAX; i++) if (clients[i].socket != -1) { if (locking_client() == -1 || is_locking(&clients[i])) FD_SET (clients[i].socket, read); LOCK (clients[i].events_mtx); if (!event_queue_empty(&clients[i].events)) FD_SET (clients[i].socket, write); UNLOCK (clients[i].events_mtx); } }
/* Handle clients whose fds are ready to read. */ static void handle_clients (fd_set *fds) { int i; for (i = 0; i < CLIENTS_MAX; i++) if (clients[i].socket != -1 && FD_ISSET(clients[i].socket, fds)) { if (locking_client() == -1 || is_locking(&clients[i])) handle_command (i); else debug ("Not getting a command from client with" " fd %d because of lock", clients[i].socket); } }