void ui_playlist_widget_update (GtkWidget * widget, gint type, gint at, gint count) { PlaylistWidgetData * data = audgui_list_get_user (widget); g_return_if_fail (data); if (type >= PLAYLIST_UPDATE_STRUCTURE) { gint diff = aud_playlist_entry_count (data->list) - audgui_list_row_count (widget); if (diff > 0) audgui_list_insert_rows (widget, at, diff); else if (diff < 0) audgui_list_delete_rows (widget, at, -diff); ui_playlist_widget_scroll (widget); } if (type >= PLAYLIST_UPDATE_METADATA) audgui_list_update_rows (widget, at, count); audgui_list_update_selection (widget, at, count); audgui_list_set_focus (widget, aud_playlist_get_focus (data->list)); update_queue (widget, data); }
void ui_playlist_widget_update (GtkWidget * widget, int type, int at, int count) { PlaylistWidgetData * data = audgui_list_get_user (widget); g_return_if_fail (data); if (type == PLAYLIST_UPDATE_STRUCTURE) { int old_entries = audgui_list_row_count (widget); int entries = aud_playlist_entry_count (data->list); audgui_list_delete_rows (widget, at, old_entries - (entries - count)); audgui_list_insert_rows (widget, at, count); /* scroll to end of playlist if entries were added there (but not if a newly added entry is playing) */ if (entries > old_entries && at + count == entries && aud_playlist_get_focus (data->list) < old_entries) aud_playlist_set_focus (data->list, entries - 1); ui_playlist_widget_scroll (widget); } else if (type == PLAYLIST_UPDATE_METADATA) audgui_list_update_rows (widget, at, count); audgui_list_update_selection (widget, at, count); audgui_list_set_focus (widget, aud_playlist_get_focus (data->list)); update_queue (widget, data); }
// looks for the shortest path from one node to other vector<int> ShortestPathAlgo<ValuesType>::path(Graph<ValuesType> graphic, int beginning, int ending, bool &status){ // First, we clear all the tools related to the quest nodes.clear(); queue.clear(); nodes_passed.clear(); vector<int> zeros(1); // Second, we create the priorityqueue that will manage the quest create_priorityqueue(graphic,beginning); // There are two options, we can find out out path, or we can run out paths to look for, this information will be inside status var while(!(queue.top_node()==ending) ){ if (queue.size() == 0) { status = false; return zeros; } update_queue(graphic); }; status = true; return nodes[queue.top_position()]; };
void MapManager::Update(uint32 diff) { i_timer.Update(diff); if( !i_timer.Passed() ) return; int i = 0; MapMapType::iterator iter; std::vector<Map*> update_queue(i_maps.size()); omp_set_num_threads(sWorld.getConfig(CONFIG_NUMTHREADS)); for(iter = i_maps.begin(), i = 0; iter != i_maps.end(); ++iter, ++i) update_queue[i] = iter->second; /* gomp in gcc <4.4 version cannot parallelise loops using random access iterators so until gcc 4.4 isnt standard, we need the update_queue workaround */ // Parallelize map updates. #pragma omp parallel for schedule(dynamic) private(i) shared(update_queue) for(int i = 0; i < i_maps.size(); ++i) { checkAndCorrectGridStatesArray(); // debugging code, should be deleted some day update_queue[i]->Update(i_timer.GetCurrent()); } ObjectAccessor::Instance().Update(i_timer.GetCurrent()); for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) (*iter)->Update(i_timer.GetCurrent()); i_timer.SetCurrent(0); }
/** * Run all pending updates. * * Returns true on success, false on failure. */ bool Control::run_pending_updates() { /* MPD has new current song */ if (idle_events & MPD_IDLE_PLAYER) { if (!get_current_song()) { return false; } /* MPD_IDLE_PLAYER will be subtracted below */ } /* MPD has new status information */ if (idle_events & (MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS | MPD_IDLE_QUEUE)) { if (!get_status()) { return false; } set_update_done(MPD_IDLE_PLAYER); set_update_done(MPD_IDLE_MIXER); set_update_done(MPD_IDLE_OPTIONS); /* MPD_IDLE_QUEUE will be subtracted below */ } /* MPD has updates to queue */ if (idle_events & MPD_IDLE_QUEUE) { if (!update_queue()) { return false; } set_update_done(MPD_IDLE_QUEUE); } /* MPD has updates to a stored playlist */ if (idle_events & MPD_IDLE_STORED_PLAYLIST) { if (!update_playlist_index()) { return false; } if (!update_playlists()) { return false; } set_update_done(MPD_IDLE_STORED_PLAYLIST); } /* MPD has new song database */ if (idle_events & MPD_IDLE_DATABASE) { if (!update_library()) { return false; } set_update_done(MPD_IDLE_DATABASE); } /* Hack to make has_pending_updates() work smoothly without too much * effort. We don't care about the rest of the events, so we just * pretend they never happened. */ idle_events = 0; return true; }
/* * add semadj values to semaphores, free undo structures. * undo structures are not freed when semaphore arrays are destroyed * so some of them may be out of date. * IMPLEMENTATION NOTE: There is some confusion over whether the * set of adjustments that needs to be done should be done in an atomic * manner or not. That is, if we are attempting to decrement the semval * should we queue up and wait until we can do so legally? * The original implementation attempted to do this (queue and wait). * The current implementation does not do so. The POSIX standard * and SVID should be consulted to determine what behavior is mandated. */ void sem_exit (void) { struct sem_queue *q; struct sem_undo *u, *un = NULL, **up, **unp; struct semid_ds *sma; int nsems, i; /* If the current process was sleeping for a semaphore, * remove it from the queue. */ if ((q = current->semsleeping)) { if (q->prev) remove_from_queue(q->sma,q); current->semsleeping = NULL; } for (up = ¤t->semundo; (u = *up); *up = u->proc_next, kfree(u)) { if (u->semid == -1) continue; sma = semary[(unsigned int) u->semid % SEMMNI]; if (sma == IPC_UNUSED || sma == IPC_NOID) continue; if (sma->sem_perm.seq != (unsigned int) u->semid / SEMMNI) continue; /* remove u from the sma->undo list */ for (unp = &sma->undo; (un = *unp); unp = &un->id_next) { if (u == un) goto found; } printk ("sem_exit undo list error id=%d\n", u->semid); break; found: *unp = un->id_next; /* perform adjustments registered in u */ nsems = sma->sem_nsems; for (i = 0; i < nsems; i++) { struct sem * sem = &sma->sem_base[i]; sem->semval += u->semadj[i]; if (sem->semval < 0) sem->semval = 0; /* shouldn't happen */ sem->sempid = current->pid; } sma->sem_otime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); } current->semundo = NULL; }
void update_handler( void ) { static int pulse_area; static int pulse_mobile; static int pulse_violence; static int pulse_point; static int pulse_room; struct timeval start; gettimeofday( &start, NULL ); event_update( ); delete_list( extracted ); if( --pulse_area <= 0 ) { pulse_area = number_range( PULSE_AREA/2, 3*PULSE_AREA/2 ); area_update( ); } if( --pulse_mobile <= 0 ) { pulse_mobile = PULSE_MOBILE; action_update( ); auction_update( ); regen_update( ); time_update( ); } if( --pulse_point <= 0 ) { pulse_point = number_range( PULSE_TICK/2, 3*PULSE_TICK/2 ); char_update( ); obj_update( ); } if( --pulse_room <= 0 ) { pulse_room = number_range( PULSE_ROOM/2, 3*PULSE_ROOM/2 ); room_update( ); w3_who( ); } if( --pulse_violence <= 0 ) { pulse_violence = PULSE_VIOLENCE; update_queue( ); } pulse_time[ TIME_UPDATE ] = stop_clock( start ); }
void MapManager::Update(uint32 diff) { i_timer.Update(diff); if( !i_timer.Passed() ) return; #ifdef MULTI_THREAD_MAP uint32 i=0; MapMapType::iterator iter; std::vector<Map*> update_queue(i_maps.size()); int omp_set_num_threads(sWorld.getConfig(CONFIG_NUMTHREADS)); for(iter = i_maps.begin(), i=0;iter != i_maps.end(); ++iter, i++) update_queue[i]=iter->second; /* gomp in gcc <4.4 version cannot parallelise loops using random access iterators so until gcc 4.4 isnt standard, we need the update_queue workaround */ #pragma omp parallel for schedule(dynamic) private(i) shared(update_queue) for(int32 i = 0; i < i_maps.size(); ++i) { checkAndCorrectGridStatesArray(); // debugging code, should be deleted some day update_queue[i]->Update(i_timer.GetCurrent()); sWorld.RecordTimeDiff("UpdateMap %u", update_queue[i]->GetId()); // sLog.outError("This is thread %d out of %d threads,updating map %u",omp_get_thread_num(),omp_get_num_threads(),iter->second->GetId()); } #else for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) { iter->second->Update(i_timer.GetCurrent()); sWorld.RecordTimeDiff("UpdateMap %u", iter->second->GetId()); } #endif for(MapMapType::iterator iter = i_maps.begin(); iter != i_maps.end(); ++iter) iter->second->DelayedUpdate(i_timer.GetCurrent()); ObjectAccessor::Instance().Update(i_timer.GetCurrent()); sWorld.RecordTimeDiff("UpdateObjectAccessor"); for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) (*iter)->Update(i_timer.GetCurrent()); sWorld.RecordTimeDiff("UpdateTransports"); i_timer.SetCurrent(0); }
void MapManager::DoDelayedMovesAndRemoves() { int i = 0; std::vector<Map*> update_queue(i_maps.size()); MapMapType::iterator iter; for(iter = i_maps.begin(); iter != i_maps.end(); ++iter, ++i) update_queue[i] = iter->second; int omp_set_num_threads(sWorld.getConfig(CONFIG_NUMTHREADS)); // Parallelize map updates. #pragma omp parallel for schedule(dynamic) private(i) shared(update_queue) for(i = 0 ; i < i_maps.size() ; ++i) { update_queue[i]->DoDelayedMovesAndRemoves(); } }
void MapManager::Update(uint32 diff) { i_timer.Update(diff); if ( !i_timer.Passed() ) return; MapMapType::iterator iter = i_maps.begin(); std::vector<Map*> update_queue(i_maps.size()); int omp_set_num_threads(sWorld.getConfig(CONFIG_UINT32_NUMTHREADS)); for (uint32 i = 0; iter != i_maps.end(); ++iter, ++i) update_queue[i] = iter->second; uint32 i = 0; #pragma omp parallel for schedule(dynamic) private(i) shared(update_queue) for (i = 0; i < i_maps.size(); ++i) update_queue[i]->Update(i_timer.GetCurrent()); for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) (*iter)->Update(i_timer.GetCurrent()); //remove all maps which can be unloaded iter = i_maps.begin(); while(iter != i_maps.end()) { Map * pMap = iter->second; //check if map can be unloaded if(pMap->CanUnload((uint32)i_timer.GetCurrent())) { pMap->UnloadAll(true); delete pMap; i_maps.erase(iter++); } else ++iter; } i_timer.SetCurrent(0); }
void MapManager::Update(uint32 diff) { i_timer.Update(diff); if( !i_timer.Passed() ) return; MapMapType::iterator iter = i_maps.begin(); std::vector<Map*> update_queue(i_maps.size()); int omp_set_num_threads(sWorld.getConfig(CONFIG_UINT32_NUMTHREADS)); for (uint32 i = 0; iter != i_maps.end(); ++iter, ++i) update_queue[i] = iter->second; #pragma omp parallel for schedule(dynamic) private(i) shared(update_queue) for (uint32 i = 0; i < i_maps.size(); ++i) update_queue[i]->Update(i_timer.GetCurrent()); for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) (*iter)->Update(i_timer.GetCurrent()); i_timer.SetCurrent(0); }
/* * simulated_cache - simulate the cache based on the given parameters from command line */ cache_output_param simulated_cache(cache* sim_cache, cache_built_param params, cache_output_param output, unsigned long long int address) { int line_index; int replace_line_index = -1; // flag - represent the line in the set to be replaced int last_hits = output.hits; // number of hits in last trace used to determine whether it's hit or miss in this trace // get the params of the cache int lines_num = params.E; long long int tag_size = 64 - (params.b) - (params.s); // get the tags and set index in input address unsigned long long int input_tag = address >> (params.b + params.s); unsigned long long int input_setIndex = address << (tag_size) >> (tag_size + params.b); // find the set in the cache to be accessed cache_set target_set = sim_cache -> sets[input_setIndex]; cache_line line; /* * implement the cache policies */ for (line_index = 0; line_index < lines_num; line_index++) { line = target_set.lines[line_index]; /* * if the line is valid, go on check the line tag * otherwise, the line is empty, just fetch a new line in this line */ if (line.valid) { /* * if the line's flag is equal to the input flag, it's a hit. Add the line index into the set's accessed queue * otherwise, it's a miss. Fetch a new line to replace a possible empty line */ if (line.tag == input_tag) { output.hits++; printf("Got a hit. Hit %d.\n", output.hits); update_queue(sim_cache, input_setIndex, line_index); } } } /* * if this trace is a miss and there is a empty line, fetch a new line and put into the empty line * if this trace is a miss and there is no empty line, fetch a new line and put into the LRU line, it's a miss + evict */ if (last_hits == output.hits) { output.misses++; printf("Got a miss. Misses %d.\n", output.misses); replace_line_index = find_empty_line(sim_cache, input_setIndex, lines_num); if(replace_line_index != -1) { sim_cache -> sets[input_setIndex].lines[replace_line_index].valid = 1; sim_cache -> sets[input_setIndex].lines[replace_line_index].tag = input_tag; sim_cache -> sets[input_setIndex].used_line_queue.rear++; sim_cache -> sets[input_setIndex].used_line_queue.indexes[sim_cache -> sets[input_setIndex].used_line_queue.rear] = replace_line_index; } else { replace_line_index = find_evict_line(sim_cache, input_setIndex, lines_num); sim_cache -> sets[input_setIndex].lines[replace_line_index].tag = input_tag; output.evicts++; sim_cache -> sets[input_setIndex].used_line_queue.rear++; sim_cache -> sets[input_setIndex].used_line_queue.indexes[sim_cache -> sets[input_setIndex].used_line_queue.rear] = replace_line_index; printf("Got a evict. Evicts %d.\n", output.evicts); } } return output; }
int sys_semop (int semid, struct sembuf *tsops, unsigned nsops) { int i, id, size, error; struct semid_ds *sma; struct sembuf sops[SEMOPM], *sop; struct sem_undo *un; int undos = 0, alter = 0; if (nsops < 1 || semid < 0) return -EINVAL; if (nsops > SEMOPM) return -E2BIG; if (!tsops) return -EFAULT; if ((i = verify_area (VERIFY_READ, tsops, nsops * sizeof(*tsops)))) return i; memcpy_fromfs (sops, tsops, nsops * sizeof(*tsops)); id = (unsigned int) semid % SEMMNI; if ((sma = semary[id]) == IPC_UNUSED || sma == IPC_NOID) return -EINVAL; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) return -EIDRM; for (i = 0; i < nsops; i++) { sop = &sops[i]; if (sop->sem_num >= sma->sem_nsems) return -EFBIG; if (sop->sem_flg & SEM_UNDO) undos++; if (sop->sem_op) alter++; } if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) return -EACCES; error = try_semop(sma, sops, nsops); if (error < 0) return error; if (undos) { /* Make sure we have an undo structure * for this process and this semaphore set. */ for (un = current->semundo; un; un = un->proc_next) if (un->semid == semid) break; if (!un) { size = sizeof(struct sem_undo) + sizeof(short)*sma->sem_nsems; un = (struct sem_undo *) kmalloc(size, GFP_ATOMIC); if (!un) return -ENOMEM; memset(un, 0, size); un->semadj = (short *) &un[1]; un->semid = semid; un->proc_next = current->semundo; current->semundo = un; un->id_next = sma->undo; sma->undo = un; } } else un = NULL; if (error == 0) { /* the operations go through immediately */ error = do_semop(sma, sops, nsops, un, current->pid); /* maybe some queued-up processes were waiting for this */ update_queue(sma); return error; } else { /* We need to sleep on this operation, so we put the current * task into the pending queue and go to sleep. */ struct sem_queue queue; queue.sma = sma; queue.sops = sops; queue.nsops = nsops; queue.undo = un; queue.pid = current->pid; queue.status = 0; insert_into_queue(sma,&queue); queue.sleeper = NULL; current->semsleeping = &queue; interruptible_sleep_on(&queue.sleeper); current->semsleeping = NULL; /* When we wake up, either the operation is finished, * or some kind of error happened. */ if (!queue.prev) { /* operation is finished, update_queue() removed us */ return queue.status; } else { remove_from_queue(sma,&queue); return -EINTR; } } }
int sys_semctl (int semid, int semnum, int cmd, union semun arg) { struct semid_ds *buf = NULL; struct semid_ds tbuf; int i, id, val = 0; struct semid_ds *sma; struct ipc_perm *ipcp; struct sem *curr = NULL; struct sem_undo *un; unsigned int nsems; ushort *array = NULL; ushort sem_io[SEMMSL]; if (semid < 0 || semnum < 0 || cmd < 0) return -EINVAL; switch (cmd) { case IPC_INFO: case SEM_INFO: { struct seminfo seminfo, *tmp = arg.__buf; seminfo.semmni = SEMMNI; seminfo.semmns = SEMMNS; seminfo.semmsl = SEMMSL; seminfo.semopm = SEMOPM; seminfo.semvmx = SEMVMX; seminfo.semmnu = SEMMNU; seminfo.semmap = SEMMAP; seminfo.semume = SEMUME; seminfo.semusz = SEMUSZ; seminfo.semaem = SEMAEM; if (cmd == SEM_INFO) { seminfo.semusz = used_semids; seminfo.semaem = used_sems; } i = verify_area(VERIFY_WRITE, tmp, sizeof(struct seminfo)); if (i) return i; memcpy_tofs (tmp, &seminfo, sizeof(struct seminfo)); return max_semid; } case SEM_STAT: buf = arg.buf; i = verify_area (VERIFY_WRITE, buf, sizeof (*buf)); if (i) return i; if (semid > max_semid) return -EINVAL; sma = semary[semid]; if (sma == IPC_UNUSED || sma == IPC_NOID) return -EINVAL; if (ipcperms (&sma->sem_perm, S_IRUGO)) return -EACCES; id = (unsigned int) sma->sem_perm.seq * SEMMNI + semid; tbuf.sem_perm = sma->sem_perm; tbuf.sem_otime = sma->sem_otime; tbuf.sem_ctime = sma->sem_ctime; tbuf.sem_nsems = sma->sem_nsems; memcpy_tofs (buf, &tbuf, sizeof(*buf)); return id; } id = (unsigned int) semid % SEMMNI; sma = semary [id]; if (sma == IPC_UNUSED || sma == IPC_NOID) return -EINVAL; ipcp = &sma->sem_perm; nsems = sma->sem_nsems; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) return -EIDRM; switch (cmd) { case GETVAL: case GETPID: case GETNCNT: case GETZCNT: case SETVAL: if (semnum >= nsems) return -EINVAL; curr = &sma->sem_base[semnum]; break; } switch (cmd) { case GETVAL: case GETPID: case GETNCNT: case GETZCNT: case GETALL: if (ipcperms (ipcp, S_IRUGO)) return -EACCES; switch (cmd) { case GETVAL : return curr->semval; case GETPID : return curr->sempid; case GETNCNT: return count_semncnt(sma,semnum); case GETZCNT: return count_semzcnt(sma,semnum); case GETALL: array = arg.array; i = verify_area (VERIFY_WRITE, array, nsems*sizeof(ushort)); if (i) return i; } break; case SETVAL: val = arg.val; if (val > SEMVMX || val < 0) return -ERANGE; break; case IPC_RMID: if (suser() || current->euid == ipcp->cuid || current->euid == ipcp->uid) { freeary (id); return 0; } return -EPERM; case SETALL: /* arg is a pointer to an array of ushort */ array = arg.array; if ((i = verify_area (VERIFY_READ, array, nsems*sizeof(ushort)))) return i; memcpy_fromfs (sem_io, array, nsems*sizeof(ushort)); for (i = 0; i < nsems; i++) if (sem_io[i] > SEMVMX) return -ERANGE; break; case IPC_STAT: buf = arg.buf; if ((i = verify_area (VERIFY_WRITE, buf, sizeof(*buf)))) return i; break; case IPC_SET: buf = arg.buf; if ((i = verify_area (VERIFY_READ, buf, sizeof (*buf)))) return i; memcpy_fromfs (&tbuf, buf, sizeof (*buf)); break; } if (semary[id] == IPC_UNUSED || semary[id] == IPC_NOID) return -EIDRM; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) return -EIDRM; switch (cmd) { case GETALL: if (ipcperms (ipcp, S_IRUGO)) return -EACCES; for (i = 0; i < sma->sem_nsems; i++) sem_io[i] = sma->sem_base[i].semval; memcpy_tofs (array, sem_io, nsems*sizeof(ushort)); break; case SETVAL: if (ipcperms (ipcp, S_IWUGO)) return -EACCES; for (un = sma->undo; un; un = un->id_next) un->semadj[semnum] = 0; curr->semval = val; sma->sem_ctime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); break; case IPC_SET: if (suser() || current->euid == ipcp->cuid || current->euid == ipcp->uid) { ipcp->uid = tbuf.sem_perm.uid; ipcp->gid = tbuf.sem_perm.gid; ipcp->mode = (ipcp->mode & ~S_IRWXUGO) | (tbuf.sem_perm.mode & S_IRWXUGO); sma->sem_ctime = CURRENT_TIME; return 0; } return -EPERM; case IPC_STAT: if (ipcperms (ipcp, S_IRUGO)) return -EACCES; tbuf.sem_perm = sma->sem_perm; tbuf.sem_otime = sma->sem_otime; tbuf.sem_ctime = sma->sem_ctime; tbuf.sem_nsems = sma->sem_nsems; memcpy_tofs (buf, &tbuf, sizeof(*buf)); break; case SETALL: if (ipcperms (ipcp, S_IWUGO)) return -EACCES; for (i = 0; i < nsems; i++) sma->sem_base[i].semval = sem_io[i]; for (un = sma->undo; un; un = un->id_next) for (i = 0; i < nsems; i++) un->semadj[i] = 0; sma->sem_ctime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); break; default: return -EINVAL; } return 0; }
asmlinkage int sys_semop (int semid, struct sembuf *tsops, unsigned nsops) { int id, size, error = -EINVAL; struct semid_ds *sma; struct sembuf sops[SEMOPM], *sop; struct sem_undo *un; int undos = 0, decrease = 0, alter = 0; struct sem_queue queue; lock_kernel(); if (nsops < 1 || semid < 0) goto out; error = -E2BIG; if (nsops > SEMOPM) goto out; error = -EFAULT; if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) goto out; id = (unsigned int) semid % SEMMNI; error = -EINVAL; if ((sma = semary[id]) == IPC_UNUSED || sma == IPC_NOID) goto out; error = -EIDRM; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) goto out; error = -EFBIG; for (sop = sops; sop < sops + nsops; sop++) { if (sop->sem_num >= sma->sem_nsems) goto out; if (sop->sem_flg & SEM_UNDO) undos++; if (sop->sem_op < 0) decrease = 1; if (sop->sem_op > 0) alter = 1; } alter |= decrease; error = -EACCES; if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) goto out; if (undos) { /* Make sure we have an undo structure * for this process and this semaphore set. */ for (un = current->semundo; un; un = un->proc_next) if (un->semid == semid) break; if (!un) { size = sizeof(struct sem_undo) + sizeof(short)*sma->sem_nsems; un = (struct sem_undo *) kmalloc(size, GFP_ATOMIC); if (!un) { error = -ENOMEM; goto out; } memset(un, 0, size); un->semadj = (short *) &un[1]; un->semid = semid; un->proc_next = current->semundo; current->semundo = un; un->id_next = sma->undo; sma->undo = un; } } else un = NULL; error = try_atomic_semop (sma, sops, nsops, un, current->pid, 0); if (error <= 0) goto update; /* We need to sleep on this operation, so we put the current * task into the pending queue and go to sleep. */ queue.sma = sma; queue.sops = sops; queue.nsops = nsops; queue.undo = un; queue.pid = current->pid; queue.alter = decrease; current->semsleeping = &queue; if (alter) append_to_queue(sma ,&queue); else prepend_to_queue(sma ,&queue); for (;;) { queue.status = -EINTR; queue.sleeper = NULL; interruptible_sleep_on(&queue.sleeper); /* * If queue.status == 1 we where woken up and * have to retry else we simply return. * If an interrupt occurred we have to clean up the * queue * */ if (queue.status == 1) { error = try_atomic_semop (sma, sops, nsops, un, current->pid,0); if (error <= 0) break; } else { error = queue.status;; if (queue.prev) /* got Interrupt */ break; /* Everything done by update_queue */ current->semsleeping = NULL; goto out; } } current->semsleeping = NULL; remove_from_queue(sma,&queue); update: if (alter) update_queue (sma); out: unlock_kernel(); return error; }
asmlinkage int sys_semctl (int semid, int semnum, int cmd, union semun arg) { struct semid_ds *buf = NULL; struct semid_ds tbuf; int i, id, val = 0; struct semid_ds *sma; struct ipc_perm *ipcp; struct sem *curr = NULL; struct sem_undo *un; unsigned int nsems; ushort *array = NULL; ushort sem_io[SEMMSL]; int err = -EINVAL; lock_kernel(); if (semid < 0 || semnum < 0 || cmd < 0) goto out; switch (cmd) { case IPC_INFO: case SEM_INFO: { struct seminfo seminfo, *tmp = arg.__buf; seminfo.semmni = SEMMNI; seminfo.semmns = SEMMNS; seminfo.semmsl = SEMMSL; seminfo.semopm = SEMOPM; seminfo.semvmx = SEMVMX; seminfo.semmnu = SEMMNU; seminfo.semmap = SEMMAP; seminfo.semume = SEMUME; seminfo.semusz = SEMUSZ; seminfo.semaem = SEMAEM; if (cmd == SEM_INFO) { seminfo.semusz = used_semids; seminfo.semaem = used_sems; } err = -EFAULT; if (copy_to_user (tmp, &seminfo, sizeof(struct seminfo))) goto out; err = max_semid; goto out; } case SEM_STAT: buf = arg.buf; err = -EINVAL; if (semid > max_semid) goto out; sma = semary[semid]; if (sma == IPC_UNUSED || sma == IPC_NOID) goto out; err = -EACCES; if (ipcperms (&sma->sem_perm, S_IRUGO)) goto out; id = (unsigned int) sma->sem_perm.seq * SEMMNI + semid; tbuf.sem_perm = sma->sem_perm; tbuf.sem_otime = sma->sem_otime; tbuf.sem_ctime = sma->sem_ctime; tbuf.sem_nsems = sma->sem_nsems; err = -EFAULT; if (copy_to_user (buf, &tbuf, sizeof(*buf)) == 0) err = id; goto out; } id = (unsigned int) semid % SEMMNI; sma = semary [id]; err = -EINVAL; if (sma == IPC_UNUSED || sma == IPC_NOID) goto out; ipcp = &sma->sem_perm; nsems = sma->sem_nsems; err = -EIDRM; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) goto out; switch (cmd) { case GETVAL: case GETPID: case GETNCNT: case GETZCNT: case SETVAL: err = -EINVAL; if (semnum >= nsems) goto out; curr = &sma->sem_base[semnum]; break; } switch (cmd) { case GETVAL: case GETPID: case GETNCNT: case GETZCNT: case GETALL: err = -EACCES; if (ipcperms (ipcp, S_IRUGO)) goto out; switch (cmd) { case GETVAL : err = curr->semval; goto out; case GETPID : err = curr->sempid & 0xffff; goto out; case GETNCNT: err = count_semncnt(sma,semnum); goto out; case GETZCNT: err = count_semzcnt(sma,semnum); goto out; case GETALL: array = arg.array; break; } break; case SETVAL: val = arg.val; err = -ERANGE; if (val > SEMVMX || val < 0) goto out; break; case IPC_RMID: if (current->euid == ipcp->cuid || current->euid == ipcp->uid || capable(CAP_SYS_ADMIN)) { freeary (id); err = 0; goto out; } err = -EPERM; goto out; case SETALL: /* arg is a pointer to an array of ushort */ array = arg.array; err = -EFAULT; if (copy_from_user (sem_io, array, nsems*sizeof(ushort))) goto out; err = 0; for (i = 0; i < nsems; i++) if (sem_io[i] > SEMVMX) { err = -ERANGE; goto out; } break; case IPC_STAT: buf = arg.buf; break; case IPC_SET: buf = arg.buf; err = copy_from_user (&tbuf, buf, sizeof (*buf)); if (err) err = -EFAULT; break; } err = -EIDRM; if (semary[id] == IPC_UNUSED || semary[id] == IPC_NOID) goto out; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) goto out; switch (cmd) { case GETALL: err = -EACCES; if (ipcperms (ipcp, S_IRUGO)) goto out; for (i = 0; i < sma->sem_nsems; i++) sem_io[i] = sma->sem_base[i].semval; if (copy_to_user (array, sem_io, nsems*sizeof(ushort))) err = -EFAULT; break; case SETVAL: err = -EACCES; if (ipcperms (ipcp, S_IWUGO)) goto out; for (un = sma->undo; un; un = un->id_next) un->semadj[semnum] = 0; curr->semval = val; sma->sem_ctime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); break; case IPC_SET: if (current->euid == ipcp->cuid || current->euid == ipcp->uid || capable(CAP_SYS_ADMIN)) { ipcp->uid = tbuf.sem_perm.uid; ipcp->gid = tbuf.sem_perm.gid; ipcp->mode = (ipcp->mode & ~S_IRWXUGO) | (tbuf.sem_perm.mode & S_IRWXUGO); sma->sem_ctime = CURRENT_TIME; err = 0; goto out; } err = -EPERM; goto out; case IPC_STAT: err = -EACCES; if (ipcperms (ipcp, S_IRUGO)) goto out; tbuf.sem_perm = sma->sem_perm; tbuf.sem_otime = sma->sem_otime; tbuf.sem_ctime = sma->sem_ctime; tbuf.sem_nsems = sma->sem_nsems; if (copy_to_user (buf, &tbuf, sizeof(*buf))) err = -EFAULT; break; case SETALL: err = -EACCES; if (ipcperms (ipcp, S_IWUGO)) goto out; for (i = 0; i < nsems; i++) sma->sem_base[i].semval = sem_io[i]; for (un = sma->undo; un; un = un->id_next) for (i = 0; i < nsems; i++) un->semadj[i] = 0; sma->sem_ctime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); break; default: err = -EINVAL; goto out; } err = 0; out: unlock_kernel(); return err; }
asmlinkage long sys_semtimedop (int semid, struct sembuf *tsops, unsigned nsops, const struct timespec *timeout) { int error = -EINVAL; struct sem_array *sma; struct sembuf fast_sops[SEMOPM_FAST]; struct sembuf* sops = fast_sops, *sop; struct sem_undo *un; int undos = 0, decrease = 0, alter = 0; struct sem_queue queue; unsigned long jiffies_left = 0; if (nsops < 1 || semid < 0) return -EINVAL; if (nsops > sc_semopm) return -E2BIG; if(nsops > SEMOPM_FAST) { sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL); if(sops==NULL) return -ENOMEM; } if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) { error=-EFAULT; goto out_free; } if (timeout) { struct timespec _timeout; if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) { error = -EFAULT; goto out_free; } if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 || _timeout.tv_nsec >= 1000000000L) { error = -EINVAL; goto out_free; } jiffies_left = timespec_to_jiffies(&_timeout); } sma = sem_lock(semid); error=-EINVAL; if(sma==NULL) goto out_free; error = -EIDRM; if (sem_checkid(sma,semid)) goto out_unlock_free; error = -EFBIG; for (sop = sops; sop < sops + nsops; sop++) { if (sop->sem_num >= sma->sem_nsems) goto out_unlock_free; if (sop->sem_flg & SEM_UNDO) undos++; if (sop->sem_op < 0) decrease = 1; if (sop->sem_op > 0) alter = 1; } alter |= decrease; error = -EACCES; if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) goto out_unlock_free; if (undos) { /* Make sure we have an undo structure * for this process and this semaphore set. */ un=current->semundo; while(un != NULL) { if(un->semid==semid) break; if(un->semid==-1) un=freeundos(sma,un); else un=un->proc_next; } if (!un) { error = alloc_undo(sma,&un,semid,alter); if(error) goto out_free; } } else un = NULL; error = try_atomic_semop (sma, sops, nsops, un, current->tgid, 0); if (error <= 0) goto update; /* We need to sleep on this operation, so we put the current * task into the pending queue and go to sleep. */ queue.sma = sma; queue.sops = sops; queue.nsops = nsops; queue.undo = un; queue.pid = current->tgid; queue.alter = decrease; queue.id = semid; if (alter) append_to_queue(sma ,&queue); else prepend_to_queue(sma ,&queue); current->semsleeping = &queue; for (;;) { struct sem_array* tmp; queue.status = -EINTR; queue.sleeper = current; current->state = TASK_INTERRUPTIBLE; sem_unlock(semid); if (timeout) jiffies_left = schedule_timeout(jiffies_left); else schedule(); tmp = sem_lock(semid); if(tmp==NULL) { if(queue.prev != NULL) BUG(); current->semsleeping = NULL; error = -EIDRM; goto out_free; } /* * If queue.status == 1 we where woken up and * have to retry else we simply return. * If an interrupt occurred we have to clean up the * queue * */ if (queue.status == 1) { error = try_atomic_semop (sma, sops, nsops, un, current->tgid, 0); if (error <= 0) break; } else { error = queue.status; if (error == -EINTR && timeout && jiffies_left == 0) error = -EAGAIN; if (queue.prev) /* got Interrupt */ break; /* Everything done by update_queue */ current->semsleeping = NULL; goto out_unlock_free; } } current->semsleeping = NULL; remove_from_queue(sma,&queue); update: if (alter) update_queue (sma); out_unlock_free: sem_unlock(semid); out_free: if(sops != fast_sops) kfree(sops); return error; }
static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg) { struct sem_array *sma; struct sem* curr; int err; ushort fast_sem_io[SEMMSL_FAST]; ushort* sem_io = fast_sem_io; int nsems; sma = sem_lock(semid); if(sma==NULL) return -EINVAL; nsems = sma->sem_nsems; err=-EIDRM; if (sem_checkid(sma,semid)) goto out_unlock; err = -EACCES; if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO)) goto out_unlock; switch (cmd) { case GETALL: { ushort *array = arg.array; int i; if(nsems > SEMMSL_FAST) { sem_unlock(semid); sem_io = ipc_alloc(sizeof(ushort)*nsems); if(sem_io == NULL) return -ENOMEM; err = sem_revalidate(semid, sma, nsems, S_IRUGO); if(err) goto out_free; } for (i = 0; i < sma->sem_nsems; i++) sem_io[i] = sma->sem_base[i].semval; sem_unlock(semid); err = 0; if(copy_to_user(array, sem_io, nsems*sizeof(ushort))) err = -EFAULT; goto out_free; } case SETALL: { int i; struct sem_undo *un; sem_unlock(semid); if(nsems > SEMMSL_FAST) { sem_io = ipc_alloc(sizeof(ushort)*nsems); if(sem_io == NULL) return -ENOMEM; } if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) { err = -EFAULT; goto out_free; } for (i = 0; i < nsems; i++) { if (sem_io[i] > SEMVMX) { err = -ERANGE; goto out_free; } } err = sem_revalidate(semid, sma, nsems, S_IWUGO); if(err) goto out_free; for (i = 0; i < nsems; i++) sma->sem_base[i].semval = sem_io[i]; for (un = sma->undo; un; un = un->id_next) for (i = 0; i < nsems; i++) un->semadj[i] = 0; sma->sem_ctime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); err = 0; goto out_unlock; } case IPC_STAT: { struct semid64_ds tbuf; memset(&tbuf,0,sizeof(tbuf)); kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm); tbuf.sem_otime = sma->sem_otime; tbuf.sem_ctime = sma->sem_ctime; tbuf.sem_nsems = sma->sem_nsems; sem_unlock(semid); if (copy_semid_to_user (arg.buf, &tbuf, version)) return -EFAULT; return 0; } /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */ } err = -EINVAL; if(semnum < 0 || semnum >= nsems) goto out_unlock; curr = &sma->sem_base[semnum]; switch (cmd) { case GETVAL: err = curr->semval; goto out_unlock; case GETPID: err = curr->sempid & 0xffff; goto out_unlock; case GETNCNT: err = count_semncnt(sma,semnum); goto out_unlock; case GETZCNT: err = count_semzcnt(sma,semnum); goto out_unlock; case SETVAL: { int val = arg.val; struct sem_undo *un; err = -ERANGE; if (val > SEMVMX || val < 0) goto out_unlock; for (un = sma->undo; un; un = un->id_next) un->semadj[semnum] = 0; curr->semval = val; curr->sempid = current->tgid; sma->sem_ctime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); err = 0; goto out_unlock; } } out_unlock: sem_unlock(semid); out_free: if(sem_io != fast_sem_io) ipc_free(sem_io, sizeof(ushort)*nsems); return err; }
/* * add semadj values to semaphores, free undo structures. * undo structures are not freed when semaphore arrays are destroyed * so some of them may be out of date. * IMPLEMENTATION NOTE: There is some confusion over whether the * set of adjustments that needs to be done should be done in an atomic * manner or not. That is, if we are attempting to decrement the semval * should we queue up and wait until we can do so legally? * The original implementation attempted to do this (queue and wait). * The current implementation does not do so. The POSIX standard * and SVID should be consulted to determine what behavior is mandated. */ void sem_exit (void) { struct sem_queue *q; struct sem_undo *u, *un = NULL, **up, **unp; struct sem_array *sma; int nsems, i; /* If the current process was sleeping for a semaphore, * remove it from the queue. */ if ((q = current->semsleeping)) { int semid = q->id; sma = sem_lock(semid); current->semsleeping = NULL; if (q->prev) { if(sma==NULL) BUG(); remove_from_queue(q->sma,q); } if(sma!=NULL) sem_unlock(semid); } for (up = ¤t->semundo; (u = *up); *up = u->proc_next, kfree(u)) { int semid = u->semid; if(semid == -1) continue; sma = sem_lock(semid); if (sma == NULL) continue; if (u->semid == -1) goto next_entry; if (sem_checkid(sma,u->semid)) goto next_entry; /* remove u from the sma->undo list */ for (unp = &sma->undo; (un = *unp); unp = &un->id_next) { if (u == un) goto found; } printk ("sem_exit undo list error id=%d\n", u->semid); goto next_entry; found: *unp = un->id_next; /* perform adjustments registered in u */ nsems = sma->sem_nsems; for (i = 0; i < nsems; i++) { struct sem * sem = &sma->sem_base[i]; sem->semval += u->semadj[i]; if (sem->semval < 0) sem->semval = 0; /* shouldn't happen */ sem->sempid = current->tgid; } sma->sem_otime = CURRENT_TIME; /* maybe some queued-up processes were waiting for this */ update_queue(sma); next_entry: sem_unlock(semid); } current->semundo = NULL; }