void tick_start(unsigned int interval_in_ms) { if (!sim_kernel_init()) { panicf("Could not initialize kernel!"); exit(-1); } if (tick_timer_id != NULL) { SDL_RemoveTimer(tick_timer_id); tick_timer_id = NULL; } else { start_tick = SDL_GetTicks(); } tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL); #ifndef HAVE_SDL_THREADS SDL_LockMutex(wfi_mutex); #endif }
static Uint32 SimulationTimer(Uint32 interval, void *unused) { if (SDL_LockMutex(Mutex)) { SDL_perror("SDL_LockMutex"); } else if (SDL_CondSignal(Cond)) { SDL_perror("SDL_CondSignal"); } else { SDL_UnlockMutex(Mutex); } double elapsed = interval; double approx = elapsed / 1000; double error = Step - approx; double step = Step + error; return Uint32(1000*step); }
int packet_queue_put(PacketQueue *q, AVPacket *pkt) { AVPacketList *pkt1; if(av_dup_packet(pkt) < 0) { return -1; } pkt1 = (AVPacketList*)av_malloc(sizeof(AVPacketList)); if (!pkt1) return -1; pkt1->pkt = *pkt; pkt1->next = NULL; SDL_LockMutex(q->mutex); if (!q->last_pkt) q->first_pkt = pkt1; else q->last_pkt->next = pkt1; q->last_pkt = pkt1; q->nb_packets++; q->size += pkt1->pkt.size; SDL_UnlockMutex(q->mutex); return 0; }
static void packet_queue_flush(PacketQueue *q) { AVPacketList *pkt, *pkt1; SDL_LockMutex(q->mutex); for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) { pkt1 = pkt->next; if(pkt1->pkt.data != (uint8_t *)"FLUSH") { } av_free_packet(&pkt->pkt); av_freep(&pkt); } q->last_pkt = NULL; q->first_pkt = NULL; q->nb_packets = 0; q->size = 0; SDL_UnlockMutex(q->mutex); }
static bool resolvercheck(char **name, ENetAddress *address) { SDL_LockMutex(resolvermutex); if (!ResolverResults.empty()) { ResolverResult &rr = ResolverResults.pop(); *name = rr.query; *address = rr.address; SDL_UnlockMutex(resolvermutex); return true; } loopv(ResolverThreads) { ResolverThread &rt = ResolverThreads[i]; if (rt.query) { if (game::lastmillis() - rt.starttime > resolverlimit) { resolverstop(rt, true); *name = rt.query; SDL_UnlockMutex(resolvermutex); return true; } } } SDL_UnlockMutex(resolvermutex); return false; }
void alloc_picture(void *userdata) { VideoState *is = (VideoState *)userdata; VideoPicture *vp; vp = &is->pictq[is->pictq_windex]; if(vp->bmp) { // we already have one make another, bigger/smaller SDL_FreeYUVOverlay(vp->bmp); } // Allocate a place to put our YUV image on that screen SDL_LockMutex(screen_mutex); vp->bmp = SDL_CreateYUVOverlay(is->video_ctx->width, is->video_ctx->height, SDL_YV12_OVERLAY, screen); SDL_UnlockMutex(screen_mutex); vp->width = is->video_ctx->width; vp->height = is->video_ctx->height; vp->allocated = 1; }
void BXBGProcess::Resume() { if (!m_bPaused) return; Lock(); std::map<int, BXBGJob*>::iterator it = m_inProgressMap.begin(); for (;it != m_inProgressMap.end(); it++) { BXBGJob *pJob = it->second; if (pJob) { pJob->Resume(); } } Unlock(); // Lock the pause mutex SDL_LockMutex(m_pPauseLock); // Update the pause protected variable m_bPaused = false; // Signal the condition variable so that the threads would resume working SDL_CondBroadcast(m_pPauseCond); // Unlock the mutex back SDL_UnlockMutex(m_pPauseLock); }
static void audioCallback(void* userdata, Uint8* stream, int len) { SDL_LockMutex(musicMutex); if (!musicSamples) { // fill with silence memset(stream, 0, len); SDL_UnlockMutex(musicMutex); return; } float* out = reinterpret_cast<float*>(stream); size_t numSamples = (size_t)len / sizeof(float); while (numSamples > 0) { auto count = std::min(numSamples, musicSize - musicCursor); memcpy(out, musicSamples + musicCursor, count * sizeof(float)); numSamples -= count; out += count; musicCursor = (musicCursor + count) % musicSize; } SDL_UnlockMutex(musicMutex); }
int TimerManager::create(double trigger, bool shouldLoop, int handlerForLua, int luaObject) { DGTimer timer; timer.handle = _handles; timer.hasTriggered = false; timer.isEnabled = true; timer.isLoopable = shouldLoop; timer.lastTime = SDL_GetTicks() / 1000; timer.type = DGTimerNormal; timer.trigger = trigger; timer.luaHandler = handlerForLua; timer.luaObject = luaObject; if (SDL_LockMutex(_mutex) == 0) { _arrayOfTimers.push_back(timer); SDL_UnlockMutex(_mutex); } _handles++; return timer.handle; }
int shr::SDLAudio::Open(unsigned freq, unsigned samples, void *buf) { int res; udata = buf; fmt.freq = freq; fmt.format = AUDIO_S16LSB; fmt.channels = 2; fmt.samples = samples; fmt.callback = cb; fmt.userdata = this; res = SDL_OpenAudio(&fmt, NULL); if(res < 0) { std::cerr << "SDLAudio::Open failed, " << SDL_GetError() << std::endl; return -1; } SDL_LockMutex(mut); return 0; }
int packet_queue_put(PacketQueue *q, AVPacket *pkt) { AVPacketList *pkt1; if(av_dup_packet(pkt) < 0) { fprintf(stderr, "dup packet filed\n"); return -1; } pkt1 = av_malloc(sizeof(AVPacketList)); pkt1->pkt = *pkt; pkt1->next = NULL; SDL_LockMutex(q->mutex); if(!q->last_pkt) q->first_pkt = pkt1; else q->last_pkt->next = pkt1; q->last_pkt = pkt1; q->nb_packets++; q->size += pkt1->pkt.size; SDL_CondSignal(q->cond); SDL_UnlockMutex(q->mutex); return 0; }
static ErrMsg *findErrorForCurrentThread(void) { ErrMsg *i; Uint32 tid; if (error_msgs != NULL) { tid = SDL_ThreadID(); SDL_LockMutex(errorlist_mutex); for (i = error_msgs; i != NULL; i = i->next) { if (i->tid == tid) { SDL_UnlockMutex(errorlist_mutex); return(i); } /* if */ } /* for */ SDL_UnlockMutex(errorlist_mutex); } /* if */ return(NULL); /* no error available. */ } /* findErrorForCurrentThread */
void HTTPServerAgent::ProcessResponses() { std::queue<Response> responseQueue; // make a copy of the response queue so we can process // the response at our leisure SDL_LockMutex(m_responseQueueLock); responseQueue = m_responseQueue; while (!m_responseQueue.empty()) m_responseQueue.pop(); SDL_UnlockMutex(m_responseQueueLock); while (!responseQueue.empty()) { Response &resp = responseQueue.front(); if (resp.success) resp.onSuccess(resp.data, resp.userdata); else resp.onFail(resp.buffer, resp.userdata); responseQueue.pop(); } }
int add_to_queue(PacketQueue *queue, AVPacket *pkt) { AVPacketList *pkt1; pkt1 = av_malloc(sizeof(AVPacketList)); if(!pkt1) return -1; if(av_dup_packet(pkt)) return -1; pkt1->pkt = *pkt; pkt1->next = NULL; SDL_LockMutex(queue->mutex); if(!queue->end) queue->header = pkt1; else queue->end->next = pkt1; queue->end = pkt1; queue->pktNum++; queue->size += pkt1->pkt.size; SDL_CondSignal(queue->cond); SDL_UnlockMutex(queue->mutex); return 0; }
/* Restart one of the threads that are waiting on the condition variable */ int SDL_CondSignal(SDL_cond * cond) { if (!cond) { SDL_SetError("Passed a NULL condition variable"); return -1; } /* If there are waiting threads not already signalled, then signal the condition and wait for the thread to respond. */ SDL_LockMutex(cond->lock); if (cond->waiting > cond->signals) { ++cond->signals; SDL_SemPost(cond->wait_sem); SDL_UnlockMutex(cond->lock); SDL_SemWait(cond->wait_done); } else { SDL_UnlockMutex(cond->lock); } return 0; }
static int queue_picture(FFMovie *movie, AVFrame *src_frame) { /*DECODE LOOP*/ AVPicture pict; SDL_LockMutex(movie->dest_mutex); /* if the frame movie not skipped, then display it */ if (movie->dest_overlay) { /* get a pointer on the bitmap */ SDL_LockYUVOverlay(movie->dest_overlay); pict.data[0] = movie->dest_overlay->pixels[0]; pict.data[1] = movie->dest_overlay->pixels[2]; pict.data[2] = movie->dest_overlay->pixels[1]; pict.linesize[0] = movie->dest_overlay->pitches[0]; pict.linesize[1] = movie->dest_overlay->pitches[2]; pict.linesize[2] = movie->dest_overlay->pitches[1]; /* first fields of AVFrame match AVPicture, so it appears safe to cast here (at least of ffmpeg-0.4.8, this is how ffplay does it) AVPicture is just a container for 4 pixel pointers and 4 strides */ img_convert(&pict, PIX_FMT_YUV420P, (AVPicture *)src_frame, movie->video_st->codec.pix_fmt, movie->video_st->codec.width, movie->video_st->codec.height); SDL_UnlockYUVOverlay(movie->dest_overlay); video_refresh_timer(movie); } SDL_UnlockMutex(movie->dest_mutex); return 0; }
static float get_delay(struct ao *ao) { struct priv *priv = ao->priv; SDL_LockMutex(priv->buffer_mutex); int sz = av_fifo_size(priv->buffer); #ifdef ESTIMATE_DELAY int64_t callback_time0 = priv->callback_time0; int64_t callback_time1 = priv->callback_time1; #endif SDL_UnlockMutex(priv->buffer_mutex); // delay component: our FIFO's length float delay = sz / (float) ao->bps; #ifdef ESTIMATE_DELAY // delay component: outstanding audio living in SDL int64_t current_time = mp_time_us(); // interval between callbacks int64_t callback_interval = callback_time0 - callback_time1; int64_t elapsed_interval = current_time - callback_time0; if (elapsed_interval > callback_interval) elapsed_interval = callback_interval; // delay subcomponent: remaining audio from the currently played buffer int64_t buffer_interval = callback_interval - elapsed_interval; // delay subcomponent: remaining audio from the next played buffer, as // provided by the callback buffer_interval += callback_interval; delay += buffer_interval / 1000000.0; #endif return delay; }
/** * Worker for the writer thread. It waits for enqueued outgoing packets * and sends them to the server as fast as it can. * * If any error is detected, the socket is closed and the thread exits. It is * up to them main thread to detect this and join() the worker threads. */ static int writer_thread_loop(void *dummy) { command_buffer *buf = NULL; while (!abort_thread) { SDL_LockMutex(output_buffer_mutex); while (output_queue_start == NULL && !abort_thread) { SDL_CondWait(output_buffer_cond, output_buffer_mutex); } buf = command_buffer_dequeue(&output_queue_start, &output_queue_end); SDL_UnlockMutex(output_buffer_mutex); size_t written = 0; while (buf != NULL && written < buf->len && !abort_thread) { size_t amt; bool success = socket_write(csocket.sc, (const void *) (buf->data + written), buf->len - written, &amt); if (!success) { break; } written += amt; network_graph_update(NETWORK_GRAPH_TYPE_GAME, NETWORK_GRAPH_TRAFFIC_TX, amt); } if (buf != NULL) { command_buffer_free(buf); buf = NULL; } } client_socket_close(&csocket); return 0; }
/********************* return RET_NOK on error *********************/ static ret_code_t __remove_group(const char * table, const char * file, const char * group, va_list ap) { const config_t * config; config_setting_t * setting = NULL; char * path; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } path = get_path(ap); if(path != NULL) { setting = config_lookup(config,path); free(path); } else { setting = config_root_setting(config); } if( setting == NULL ) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } if( config_setting_remove(setting,group) == CONFIG_FALSE) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } write_config(config,table,file); SDL_UnlockMutex(entry_mutex); return RET_OK; }
/********************* return RET_NOK on error *********************/ static ret_code_t __add_to_list(const char * table, const char * file, const char * to_be_added, va_list ap) { const config_t * config = NULL; config_setting_t * setting = NULL; char * path; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } path = get_path(ap); if(path == NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } setting = config_lookup (config, path); if( setting == NULL ) { SDL_UnlockMutex(entry_mutex); free(path); return RET_NOK; } free(path); if(config_setting_set_string_elem(setting,-1,to_be_added)==NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } write_config(config,table,file); SDL_UnlockMutex(entry_mutex); return RET_OK; }
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) { AVPacketList *pkt1; int ret; SDL_LockMutex(q->mutex); for (;;) { if (quit) { ret = -1; break; } pkt1 = q->first_pkt; if (pkt1) { q->first_pkt = pkt1->next; if (!q->first_pkt) q->last_pkt = NULL; q->nb_packets--; q->size -= pkt1->pkt.size; *pkt = pkt1->pkt; av_free(pkt1); ret = 1; break; } else if (!block) { ret = 0; break; } else { SDL_CondWait(q->cond, q->mutex); } } SDL_UnlockMutex(q->mutex); return ret; }
static boolean I_AVPopPacketFromQueue(avPacketQueue_t *packetQueue, AVPacket **packet) { boolean result = true; *packet = NULL; if(packetQueue->mutex) { SDL_LockMutex(packetQueue->mutex); } if(packetQueue->first) { if(!packetQueue->first->endMark) { void *del; *packet = packetQueue->first->packet; del = packetQueue->first; packetQueue->first = packetQueue->first->next; free(del); } else { // needed to indicate that we reached the end result = false; } } if(packetQueue->mutex) { SDL_UnlockMutex(packetQueue->mutex); } return result; }
EXPORT int osal_TurnOffCamera(int cam_id){ JNIEnv* env=(JNIEnv*)SDL_AndroidGetJNIEnv(); TCamera* cam=&g_cameras[cam_id]; jmethodID method_id; jvalue args[1]; int ret; if((unsigned int)cam_id>=(unsigned int)MAX_CAMERAS)return 0; if(!cam->m_is_on)return 1; SDL_LockMutex(cam->m_cam_mutex); method_id=(*env)->GetMethodID(env,cam->m_clazz,"turn_off","()I"); args[0].l=NULL; ret=(*env)->CallIntMethodA(env,cam->m_cam_object,method_id,args); if(cam->m_image_back){ free(cam->m_image_back); cam->m_image_back=NULL; } if(cam->m_image_front){ free(cam->m_image_front); cam->m_image_front=NULL; } cam->m_is_on=0; SDL_UnlockMutex(cam->m_cam_mutex); return ret; }
/** * @sa _Mem_FreePool */ void _Mem_Free (void *ptr, const char *fileName, const int fileLine) { memBlock_t *search; memBlock_t **prev; if (!ptr) return; memBlock_t* const mem = Mem_PtrToBlock(ptr); _Mem_CheckSentinels(mem, fileName, fileLine); SDL_LockMutex(z_lock); /* Decrement counters */ mem->pool->blockCount--; mem->pool->byteCount -= Mem_BlockRawSize(mem); /* De-link it */ prev = &mem->pool->blocks[(uintptr_t)mem % MEM_HASH]; for (;;) { search = *prev; if (!search) break; if (search == mem) { *prev = search->next; break; } prev = &search->next; } SDL_UnlockMutex(z_lock); /* Free it */ free(mem); }
/* =============== GLimp_RendererSleep =============== */ void *GLimp_RendererSleep(void) { void *data = NULL; SDL_LockMutex(smpMutex); { smpData = NULL; // after this, the front end can exit GLimp_FrontEndSleep SDL_CondSignal(renderCompletedEvent); while (!smpData) SDL_CondWait(renderCommandsEvent, smpMutex); data = (void *)smpData; if (data == (void *)0xdead ) { renderThread = (void *)0xdead; data = NULL; } } SDL_UnlockMutex(smpMutex); return data; }
void TCP_NetServer::removeRemoteSocket(TCPsocket sock) { SDL_LockMutex(remoteMutex); int removedIndex = -1; for (int i = 0; i < NETSP_PLAYERS_COUNT; ++i) { if (remote[i].socket == sock) { removedIndex = i; SDLNet_TCP_Close(remote[i].socket); remote[i].socket = NULL; remote[i].playerId = 0; break; } } //TODO NET inform everyone about what happened SDL_UnlockMutex(remoteMutex); if (removedIndex >= 0 && remote[removedIndex].recvThread != NULL) { SDL_WaitThread(remote[removedIndex].recvThread, NULL); remote[removedIndex].recvThread = NULL; } }
/********************* return RET_NOK on error *********************/ static ret_code_t __write_string(const char * table, const char * file, const char * data, va_list ap) { config_setting_t * setting; const config_t * config; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } setting = create_tree(config,NULL,NULL,NULL,CONFIG_TYPE_STRING,ap); /* update string */ if(config_setting_set_string(setting, data)==CONFIG_FALSE) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } write_config(config,table,file); SDL_UnlockMutex(entry_mutex); return RET_OK; }
static void flush_rtp_packets (rtp_plugin_data_t *pifptr) { isma_enc_rtp_data_t *iptr = (isma_enc_rtp_data_t *)pifptr; isma_frame_data_t *p; SDL_LockMutex(iptr->m_rtp_packet_mutex); if (iptr->m_frame_data_on != NULL) { iptr->m_frame_data_on->frame_data_next = iptr->m_frame_data_head; iptr->m_frame_data_head = iptr->m_frame_data_on; iptr->m_frame_data_on = NULL; } if (iptr->m_frame_data_head != NULL) { p = iptr->m_frame_data_head; while (p->frame_data_next != NULL) { #ifdef DEBUG_ISMA_AAC isma_message(LOG_DEBUG, ismaencrtp, "reset removing pak %d", p->pak->rtp_pak_seq); #endif if (p->last_in_pak != 0) { if (p->is_fragment == 1) { // get rid of frag data isma_frag_data_t * q = NULL; while ((q = p->frag_data) != NULL) { p->frag_data = q->frag_data_next; CHECK_AND_FREE(q); } } iptr->m_vft->free_pak(p->pak); } p = p->frame_data_next; } p->frame_data_next = iptr->m_frame_data_free; iptr->m_frame_data_free = iptr->m_frame_data_head; iptr->m_frame_data_head = NULL; } SDL_UnlockMutex(iptr->m_rtp_packet_mutex); }
void CRfc3119RtpByteStream::insert_interleave_adu (adu_data_t *adu) { adu_data_t *p, *q; #ifdef DEBUG_3119_INTERLEAVE mpa_message(LOG_DEBUG, "inserting interleave %d %d %d", adu->aduDataSize, adu->cyc_ct, adu->interleave_idx); #endif SDL_LockMutex(m_rtp_packet_mutex); if (m_deinterleave_list == NULL) { m_got_next_idx = 0; m_deinterleave_list = adu; } else { q = NULL; p = m_deinterleave_list; if (adu->cyc_ct != p->cyc_ct) { m_got_next_idx = 1; do { q = p; p = p->next_adu; } while (p != NULL && p->cyc_ct != adu->cyc_ct); if (p == NULL) { q->next_adu = adu; SDL_UnlockMutex(m_rtp_packet_mutex); return; } } while (p != NULL && p->interleave_idx < adu->interleave_idx) { q = p; p = p->next_adu; } q->next_adu = adu; adu->next_adu = p; } SDL_UnlockMutex(m_rtp_packet_mutex); }
Player* PlayerBucket::next() { Player *p = NULL; if (last_pos) return NULL; SDL_LockMutex(lock); /* move iterator */ if (first_pos) { it = dic.begin(); first_pos = false; } if (it != dic.end()) { p = it->second; it++; } else { last_pos = true; } SDL_UnlockMutex(lock); return p; }