void cell_gunlocktree(struct cell *c) { struct cell *finger; TIMER_TIC /* First of all, try to unlock this cell. */ if (lock_unlock(&c->glock) != 0) error("Failed to unlock cell."); /* Climb up the tree and unhold the parents. */ for (finger = c->parent; finger != NULL; finger = finger->parent) __sync_fetch_and_sub(&finger->ghold, 1); TIMER_TOC(timer_locktree); }
int knet_vrouter_route(kvrouter_t* router, kchannel_ref_t* c, const void* buffer, int size) { uint32_t id = 0; wire_t* w = 0; kstream_t* s = 0; int error = error_ok; verify(router); verify(c); verify(buffer); verify(size); lock_lock(router->lock); id = uuid_get_high32(knet_channel_ref_get_uuid(c)); w = (wire_t*)hash_get(router->table, id); if (!w) { lock_unlock(router->lock); return error_router_wire_not_found; } verify(w->c2); s = knet_channel_ref_get_stream(w->c2); verify(s); /* 发送 */ error = knet_stream_push(s, buffer, size); lock_unlock(router->lock); return error; }
int fseeko(FILE *stream, off_t offset, int whence) { int res; lock_lock(&(stream->lock)); do { if(unsafely_set_stream_offset(stream, offset, whence) == -1) { res = -1; break; } memset(&(stream->mb_state), 0, sizeof(mbstate_t)); res = 0; } while(0); lock_unlock(&(stream->lock)); return res; }
int fsetpos(FILE *stream, const fpos_t *pos) { int res; lock_lock(&(stream->lock)); do { if(unsafely_set_stream_offset(stream, pos->offset, SEEK_SET) == -1) { res = -1; break; } stream->mb_state = pos->mb_state; res = 0; } while(0); lock_unlock(&(stream->lock)); return res; }
struct mq_t *gq_pop() { struct mq_t *mq = NULL; lock_lock(mGq->mLock); mq = mGq->mHead; if (mq) { mGq->mHead = mq->mNext; if (!mGq->mHead) { assert(mGq->mTail == mq); mGq->mTail = NULL; } mq->mNext = NULL; } lock_unlock(mGq->mLock); return mq; }
void CGeoIP::ThreadGeoIP(void *params) { InfoGeoIPThread *pInfoThread = static_cast<InfoGeoIPThread*>(params); std::string host = pInfoThread->m_aIpAddress; IGeoIP::GeoInfo info = GetInfo(host.substr(0, host.find_first_of(":")).c_str()); lock_wait(m_GeoIPLock); if (pInfoThread->m_pServerInfoReg) { str_copy(pInfoThread->m_pServerInfoReg->m_aCountryCode, info.m_aCountryCode, sizeof(pInfoThread->m_pServerInfoReg->m_aCountryCode)); str_copy(pInfoThread->m_pServerInfoReg->m_aCountryName, info.m_aCountryName, sizeof(pInfoThread->m_pServerInfoReg->m_aCountryName)); } *pInfoThread->m_pGeoInfo = info; lock_unlock(m_GeoIPLock); }
bool CSound::IsPlaying(CSampleHandle SampleID) { bool Ret = false; lock_wait(m_SoundLock); CSample *pSample = &m_aSamples[SampleID.Id()]; for(int i = 0; i < NUM_VOICES; i++) { if(m_aVoices[i].m_pSample == pSample) { Ret = true; } } lock_unlock(m_SoundLock); return Ret; }
size_t fread(void *ptr, size_t elem_size, size_t elem_count, FILE *stream) { char *buf = (char *) ptr; size_t count, read_byte_count; lock_lock(&(stream->lock)); if(__uportlibc_unsafely_prepare_stream_to_read(stream, -1) == EOF) return 0; count = elem_size * elem_count; read_byte_count = 0; while(stream->pushed_c_count > 0 && count > 0) { stream->pushed_c_count--; *buf = stream->pushed_cs[stream->pushed_c_count]; buf++; count--; read_byte_count++; } if(stream->buf_type != _IONBF) { while(stream->buf_data_cur != stream->buf_data_end && count > 0) { *buf = *(stream->buf_data_cur); buf++; stream->buf_data_cur++; count--; read_byte_count++; } if(stream->buf_data_cur == stream->buf_data_end) { stream->buf_data_cur = stream->buf; stream->buf_data_end = stream->buf; } } while(count > 0) { size_t tmp_count = (count < SSIZE_MAX ? count : SSIZE_MAX); ssize_t res = read(stream->fd, buf, tmp_count); if(res == 0) { stream->flags |= FILE_FLAG_EOF; break; } if(res == -1) { stream->flags |= FILE_FLAG_ERROR; break; } buf += res; count -= res; read_byte_count += res; } lock_unlock(&(stream->lock)); return read_byte_count / elem_size; }
int fgetpos(FILE *stream, fpos_t *pos) { int res; lock_lock(&(stream->lock)); do { off_t offset = unsafely_get_stream_offset(stream); if(offset == -1) { res = -1; break; } pos->offset = offset; pos->mb_state = stream->mb_state; res = 0; } while(0); lock_unlock(&(stream->lock)); return res; }
int putw(int w, FILE *stream) { union word word_union; size_t i; int res; word_union.word = w; res = 0; lock_lock(&(stream->lock)); for(i = 0; i < WORD_BIT / 8; i++) { if(putc_unlocked(word_union.bytes[i], stream) == EOF) { res = EOF; break; } } lock_unlock(&(stream->lock)); return res; }
void FifoConsole::ListenFifoThread(void *pUser) { FifoConsole *pData = (FifoConsole *)pUser; if(!gs_FifoLock) { dbg_msg("fifo", "FIFO not properly initialized"); exit(2); } lock_wait(gs_FifoLock); if(gs_stopFifoThread) return; mkfifo(pData->m_pFifoFile, 0600); struct stat attribute; stat(pData->m_pFifoFile, &attribute); if(!S_ISFIFO(attribute.st_mode)) { dbg_msg("fifo", "'%s' is not a FIFO, removing", pData->m_pFifoFile); fs_remove(pData->m_pFifoFile); mkfifo(pData->m_pFifoFile, 0600); stat(pData->m_pFifoFile, &attribute); if(!S_ISFIFO(attribute.st_mode)) { dbg_msg("fifo", "Can't remove file, quitting"); exit(2); } } lock_unlock(gs_FifoLock); std::ifstream f; char aBuf[8192]; while (true) { f.open(pData->m_pFifoFile); while (f.getline(aBuf, sizeof(aBuf))) pData->m_pConsole->ExecuteLineFlag(aBuf, pData->m_flag, -1); f.close(); } }
int safe_warn(server *srv, const char *format, ...) { /* displays a warning */ lock_lock (&srv->master->lock_log); va_list args; printf ("WARNING "); va_start (args, format); vprintf (format, args); va_end (args); printf ("\n"); lock_unlock (&srv->master->lock_log); return 0; }
int CSound::Update() { // update volume int WantedVolume = g_Config.m_SndVolume; if(!m_pGraphics->WindowActive() && g_Config.m_SndNonactiveMute) WantedVolume = 0; if(WantedVolume != m_SoundVolume) { lock_wait(m_SoundLock); m_SoundVolume = WantedVolume; lock_unlock(m_SoundLock); } return 0; }
void CSound::StopVoice(CVoiceHandle Voice) { if(!Voice.IsValid()) return; int VoiceID = Voice.Id(); if(m_aVoices[VoiceID].m_Age != Voice.Age()) return; lock_wait(m_SoundLock); { m_aVoices[VoiceID].m_pSample = 0; m_aVoices[VoiceID].m_Age++; } lock_unlock(m_SoundLock); }
enum nss_status internal_function __nss_cdb_byid(struct nss_cdb *dbp, unsigned long id, void *result, char *buf, size_t bufl, int *errnop) { enum nss_status r; if (bufl < 30) return *errnop = ERANGE, NSS_STATUS_TRYAGAIN; lock_lock(lock); if (!isopen(dbp) && !__nss_cdb_dosetent(dbp)) *errnop = errno, r = NSS_STATUS_UNAVAIL; else { r = __nss_cdb_dobyid(dbp, id, result, buf, bufl, errnop); if (!dbp->keepopen) __nss_cdb_doendent(dbp); } lock_unlock(lock); return r; }
enum nss_status internal_function __nss_cdb_byname(struct nss_cdb *dbp, const char *name, void *result, char *buf, size_t bufl, int *errnop) { enum nss_status r; if (*name == ':') return *errnop = ENOENT, NSS_STATUS_NOTFOUND; lock_lock(lock); if (!isopen(dbp) && !__nss_cdb_dosetent(dbp)) *errnop = errno, r = NSS_STATUS_UNAVAIL; else { r = __nss_cdb_dobyname(dbp, name, strlen(name), result, buf, bufl, errnop); if (!dbp->keepopen) __nss_cdb_doendent(dbp); } lock_unlock(lock); return r; }
int main(void) { const int cpu = procnum(); int i; printf("Cpu %x booted\n", cpu); for (i=0; i<100; ++i) { lock_lock(&lock); printf("Hello from cpu %x\n", cpu); lock_unlock(&lock); } if (atomic_inc(&count) == 4) exit(0); while(1); }
void CSound::StopAll() { // TODO: a nice fade out lock_wait(m_SoundLock); for(int i = 0; i < NUM_VOICES; i++) { if(m_aVoices[i].m_pSample) { if(m_aVoices[i].m_Flags & FLAG_LOOP) m_aVoices[i].m_pSample->m_PausedAt = m_aVoices[i].m_Tick; else m_aVoices[i].m_pSample->m_PausedAt = 0; } m_aVoices[i].m_pSample = 0; } lock_unlock(m_SoundLock); }
/* Handle a DHCPREQ message */ static uint32_t request(unsigned int client, uint32_t ip, uint32_t siaddr) { if (siaddr != my_ip) { /* This message was intended for a different DHCP server. In a real * system we wouldn't send a DHCPNAK here, but would just ignore this * message. In this simulated setup we are the only server and we * *must* send a reply, so we just NAK it. */ dprintf("%s: Sending DHCPNAK due to server IP mismatch\n", get_instance_name()); return 0; } assert(client < routing_table_sz && "DHCPREQ from non-existent client"); lock_lock(); /* IP address the client gets. This will remain 0 after the logic below if * we're denying the client's request. */ uint32_t assigned = 0; if (routing_table[client] == ip) { /* They requested their existing IP. OK, whatever. */ dprintf("%s: Sending DHCPACK to client %u for its existing IP\n", get_instance_name(), client); assigned = ip; } else if (ip != my_ip && ip != 0 && ip != routing_table[0] && ip != routing_table[1] && ip != routing_table[2] && ip != routing_table[3]) { /* They requested an IP that was not ours, 0 or in the routing table * already. XXX: we should probably block the broadcast IP here too. */ char pretty_ip[STRLEN_IP]; ip_to_string(ip, pretty_ip); dprintf("%s: Sending DHCPACK to client %u of IP %s\n", get_instance_name(), client, pretty_ip); routing_table[client] = ip; assigned = ip; } lock_unlock(); return assigned; }
void CSound::Stop(int SampleID) { // TODO: a nice fade out lock_wait(m_SoundLock); CSample *pSample = &m_aSamples[SampleID]; for(int i = 0; i < NUM_VOICES; i++) { if(m_aVoices[i].m_pSample == pSample) { if(m_aVoices[i].m_Flags & FLAG_LOOP) m_aVoices[i].m_pSample->m_PausedAt = m_aVoices[i].m_Tick; else m_aVoices[i].m_pSample->m_PausedAt = 0; m_aVoices[i].m_pSample = 0; } } lock_unlock(m_SoundLock); }
void thread_barrior() { uint32_t map = 1 << __thread_id; uint32_t full = 0; for(int i = 0; i < __thread_count; i++) full |= 1 << i; lock_lock(__barrior_lock); if(*__barrior == full) { // The first one *__barrior = map; } else { *__barrior |= map; } lock_unlock(__barrior_lock); while(*__barrior != full && *__barrior & map) __asm__ __volatile__ ("nop"); }
int getw(FILE *stream) { union word word_union; size_t i; int w; w = 0; lock_lock(&(stream->lock)); for(i = 0; i < WORD_BIT / 8; i++) { int c = getc_unlocked(stream); if(c == EOF) { w = EOF; break; } word_union.bytes[i] = ((char) ((unsigned char) c)); } lock_unlock(&(stream->lock)); if(w != EOF) w = word_union.word; return w; }
int CJobPool::Add(CJob *pJob, JOBFUNC pfnFunc, void *pData) { mem_zero(pJob, sizeof(CJob)); pJob->m_pfnFunc = pfnFunc; pJob->m_pFuncData = pData; lock_wait(m_Lock); // add job to queue pJob->m_pPrev = m_pLastJob; if(m_pLastJob) m_pLastJob->m_pNext = pJob; m_pLastJob = pJob; if(!m_pFirstJob) m_pFirstJob = pJob; lock_unlock(m_Lock); return 0; }
int run(void) { const char *name = get_instance_name(); printf("%s: Started\n", name); printf("%s: Trying to acquire the lock...\n", name); lock_lock(); printf("%s: Got it!\n", name); printf("%s: Let's do some long running calculation (or more accurately, waste time)...\n", name); for (int i = 0; i < 10000; i++) asm volatile (""); printf("%s: Releasing the lock...\n", name); lock_unlock(); printf("%s: Done; let's spin.\n", name); while (1); return 0; }
int puts(const char *str) { int res; lock_lock(&(stdout->lock)); do { res = fputs_unlocked(str, stdout); if(res == EOF) { res = EOF; break; } if(putchar_unlocked('\n') == EOF) { res = EOF; break; } res++; } while(0); lock_unlock(&(stdout->lock)); return res; }
ISound::CVoiceHandle CSound::Play(int ChannelID, int SampleID, int Flags, float x, float y) { int VoiceID = -1; int Age = -1; int i; lock_wait(m_SoundLock); // search for voice for(i = 0; i < NUM_VOICES; i++) { int id = (m_NextVoice + i) % NUM_VOICES; if(!m_aVoices[id].m_pSample) { VoiceID = id; m_NextVoice = id+1; break; } } // voice found, use it if(VoiceID != -1) { m_aVoices[VoiceID].m_pSample = &m_aSamples[SampleID]; m_aVoices[VoiceID].m_pChannel = &m_aChannels[ChannelID]; if(Flags & FLAG_LOOP) m_aVoices[VoiceID].m_Tick = m_aSamples[SampleID].m_PausedAt; else m_aVoices[VoiceID].m_Tick = 0; m_aVoices[VoiceID].m_Vol = 255; m_aVoices[VoiceID].m_Flags = Flags; m_aVoices[VoiceID].m_X = (int)x; m_aVoices[VoiceID].m_Y = (int)y; m_aVoices[VoiceID].m_Falloff = 0.0f; m_aVoices[VoiceID].m_Shape = ISound::SHAPE_CIRCLE; m_aVoices[VoiceID].m_Circle.m_Radius = DefaultDistance; Age = m_aVoices[VoiceID].m_Age; } lock_unlock(m_SoundLock); return CreateVoiceHandle(VoiceID, Age); }
EXPORTED int mappedfile_unlock(struct mappedfile *mf) { int r; /* make this safe to call multiple times */ if (!mf) return 0; if (mf->lock_status == MF_UNLOCKED) return 0; assert(mf->fd != -1); assert(!mf->dirty); r = lock_unlock(mf->fd, mf->fname); if (r < 0) { syslog(LOG_ERR, "IOERROR: lock_unlock %s: %m", mf->fname); return r; } mf->lock_status = MF_UNLOCKED; return 0; }
void irq_handler(int irq) { uint32_t ti; int left = atomic_add(&max_interrupts, -1); ti = soclib_io_get( base(TIMER), procnum()*TIMER_SPAN+TIMER_VALUE); lock_lock(&lock); printf("IRQ %d received at cycle %d on cpu %d %d interrupts to go\n\n", irq, ti, procnum(), left); lock_unlock(&lock); soclib_io_set( base(TIMER), procnum()*TIMER_SPAN+TIMER_RESETIRQ, 0); if ( ! left ) exit(0); }
int CSound::Play(int ChannelID, CSampleHandle SampleID, int Flags, float x, float y) { if(!SampleID.IsValid()) return -1; int VoiceID = -1; int i; lock_wait(m_SoundLock); // search for voice for(i = 0; i < NUM_VOICES; i++) { int id = (m_NextVoice + i) % NUM_VOICES; if(!m_aVoices[id].m_pSample) { VoiceID = id; m_NextVoice = id+1; break; } } // voice found, use it if(VoiceID != -1) { m_aVoices[VoiceID].m_pSample = &m_aSamples[SampleID.Id()]; m_aVoices[VoiceID].m_pChannel = &m_aChannels[ChannelID]; if(Flags & FLAG_LOOP) m_aVoices[VoiceID].m_Tick = m_aSamples[SampleID.Id()].m_PausedAt; else m_aVoices[VoiceID].m_Tick = 0; m_aVoices[VoiceID].m_Vol = 255; m_aVoices[VoiceID].m_Flags = Flags; m_aVoices[VoiceID].m_X = (int)x; m_aVoices[VoiceID].m_Y = (int)y; } lock_unlock(m_SoundLock); return VoiceID; }
/* other routines call this one when they fail */ static int abort_txn(struct dbengine *db, struct txn *tid) { int r = CYRUSDB_OK; int rw = 0; struct stat sbuf; assert(db && tid); /* cleanup done while lock is held */ if (tid->fnamenew) { unlink(tid->fnamenew); free(tid->fnamenew); rw = 1; } /* release lock */ r = lock_unlock(db->fd, db->fname); if (r == -1) { syslog(LOG_ERR, "IOERROR: unlocking db %s: %m", db->fname); r = CYRUSDB_IOERROR; } if (rw) { /* return to our normally scheduled fd */ if (!r && fstat(db->fd, &sbuf) == -1) { syslog(LOG_ERR, "IOERROR: fstat on %s: %m", db->fname); r = CYRUSDB_IOERROR; } if (!r) { map_free(&db->base, &db->len); map_refresh(db->fd, 0, &db->base, &db->len, sbuf.st_size, db->fname, 0); db->size = sbuf.st_size; } } free(tid); return 0; }