static int utmp_lock() { signal(SIGALRM, longlock); alarm(10); lock_sem(UTMP_SEMLOCK); signal(SIGALRM, SIG_IGN); return 0; }
static int player_loop(t_lemipc *lemipc) { while (g_check) { printf("still Alive !\n"); lock_sem(lemipc->game->sem_id); if (check_death(lemipc) == 1) return (0); move_random(lemipc); usleep(600000); unlock_sem(lemipc->game->sem_id); } return (1); }
int launch_game(t_lemipc *lemipc) { signal(SIGINT, catch_signal); srand(time(NULL)); lock_sem(lemipc->game->sem_id); init_player(lemipc); unlock_sem(lemipc->game->sem_id); player_loop(lemipc); lemipc->game->map[lemipc->player.pos.y][lemipc->player.pos.x] = 0; if (count_players(lemipc) == 0) { printf("Winner is team %d\n", lemipc->player.team); free_ipcs(lemipc); } return (0); }
static int lock(unsigned long max_wait_milliseconds) { int locked = 0; if(bandwidth_semaphore == -1) { get_sem(&bandwidth_semaphore, (key_t)(BANDWIDTH_SEMAPHORE_KEY) ); } if(bandwidth_semaphore != -1) { do { locked = lock_sem(bandwidth_semaphore); if(locked == 0 && max_wait_milliseconds > 25) { usleep(1000*25); } max_wait_milliseconds = max_wait_milliseconds > 25 ? max_wait_milliseconds - 25 : 0; } while(locked == 0 && max_wait_milliseconds > 0); } return locked; }
/* * Send a board update message to all connected clients * * Excludes the user with write acces if the parameter * excl_w is set to 1. If the blackbpard should be broadcasted to all * connected clients, excl_w shoul be 0. */ void broadcast_blackboard(char *blackboard, int bsem_id, int excl_w) { struct cl_entry *current; struct net_board *board; int ret; int length; /* * Prepare the blackboard message */ lock_sem(bsem_id); length = strlen(blackboard); board = build_board(blackboard, length); unlock_sem(bsem_id); /* * Iterate over the complete client list * This locks the mutex of the client list */ current = start_iteration(); while (current != NULL) { /* Don't broadcast to the user with write access */ if ((current == get_write_user()) && excl_w) { current = iteration_next(); continue; } ret = send(current->cdata->sfd, board, sizeof(struct net_header) + length, 0); if (ret < 0) { perror("send"); } /* Select next client in list */ current = iteration_next(); } /* Unlock mutex of client list */ end_iteration(); free(board); log_debug("broadcasting agent: blackboard sent to all connected clients"); }
static int bcache_lock() { lock_sem(BCACHE_SEMLOCK); bcache_setreadonly(0); return 0; }