static void *Func_Thread(ALLEGRO_THREAD *thr, void *arg){ DATA *data = (DATA*) arg; float num = 0.1; al_lock_mutex(data->mutex); bool modi_X = data->modi_X; data->ready = true; al_broadcast_cond(data->cond); al_unlock_mutex(data->mutex); while(!al_get_thread_should_stop(thr)){ al_lock_mutex(data->mutex); if(modi_X) data->posiX += num; else data->posiY += num; al_unlock_mutex(data->mutex); al_rest(0.01); } return NULL; }
/** releases a resource. If the resource is found, then its reference count is decremented. If the resource's reference count drops to 0, the resource is deleted and uninstalled. @param res resource; can be null. @return non-zero if the operation suceeded, zero if the resource does not exist. */ int algui_release_resource(void *res) { _RESOURCE *node; //no resource if (!res) return 0; al_lock_mutex(_mutex); //find the resource node = _find_resource_by_data(res); //if not found, return error if (!node) { al_unlock_mutex(_mutex); return 0; } //decrement the resource's ref count assert(node->ref_count > 0); --node->ref_count; //if the ref count reaches 0, uninstall the resource if (!node->ref_count) _uninstall_resource(node, 1); al_lock_mutex(_mutex); //success return 1; }
static int queue_picture(VideoState * is, AVFrame * pFrame, double pts) { VideoPicture *vp; if (!is->first) { is->first = true; is->external_clock_start = av_gettime(); } /* wait until we have space for a new pic */ al_lock_mutex(is->pictq_mutex); while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && !is->quit) { al_wait_cond(is->pictq_cond, is->pictq_mutex); } al_unlock_mutex(is->pictq_mutex); if (is->quit) return -1; // windex is set to 0 initially vp = &is->pictq[is->pictq_windex]; vp->dropped = false; { ALLEGRO_EVENT event; vp->frame = pFrame; vp->pts = pts; vp->allocated = 0; /* we have to do it in the main thread */ //printf("allocate %d (%4.1f ms)\n", is->pictq_windex, // get_master_clock(is) * 1000); event.type = ALLEGRO_EVENT_VIDEO_FRAME_ALLOC; event.user.data1 = (intptr_t)is->video; al_emit_user_event(&is->video->es, &event, NULL); /* wait until we have a picture allocated */ al_lock_mutex(is->pictq_mutex); is->got_picture++; while (!vp->allocated && !is->quit) { al_wait_cond(is->pictq_cond, is->pictq_mutex); } al_unlock_mutex(is->pictq_mutex); if (is->quit) { return -1; } } return 0; }
InputDescriptor Input::getDescriptor() { al_lock_mutex(mutex); update(); if (tguiCurrentTimeMillis() < timeOfNextNotification || orRelease != -1) { descriptor.left = descriptor.right = descriptor.up = descriptor.down = descriptor.button1 = descriptor.button2 = descriptor.button3 = false; } else { orRelease = -1; } InputDescriptor i = descriptor; al_unlock_mutex(mutex); return i; }
/* Function: al_detach_voice */ void al_detach_voice(ALLEGRO_VOICE *voice) { ASSERT(voice); if (!voice->attached_stream) { return; } al_lock_mutex(voice->mutex); if (!voice->is_streaming) { ALLEGRO_SAMPLE_INSTANCE *spl = voice->attached_stream; spl->pos = voice->driver->get_voice_position(voice); spl->is_playing = voice->driver->voice_is_playing(voice); voice->driver->stop_voice(voice); voice->driver->unload_voice(voice); } else { voice->driver->stop_voice(voice); } _al_kcm_stream_set_mutex(voice->attached_stream, NULL); voice->attached_stream->parent.u.voice = NULL; voice->attached_stream->spl_read = NULL; voice->attached_stream = NULL; al_unlock_mutex(voice->mutex); }
static ALLEGRO_HAPTIC *hapxi_get_from_joystick(ALLEGRO_JOYSTICK *joy) { ALLEGRO_JOYSTICK_XINPUT *joyxi = (ALLEGRO_JOYSTICK_XINPUT *)joy; ALLEGRO_HAPTIC_XINPUT *hapxi; if (!al_is_joystick_haptic(joy)) return NULL; al_lock_mutex(hapxi_mutex); hapxi = haptics + joyxi->index; hapxi->parent.driver = &_al_hapdrv_xinput; hapxi->parent.device = joyxi; hapxi->parent.from = _AL_HAPTIC_FROM_JOYSTICK; hapxi->active = true; hapxi->effect.state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE; /* not in use */ hapxi->parent.gain = 1.0; hapxi->parent.autocenter = 0.0; hapxi->flags = ALLEGRO_HAPTIC_RUMBLE; hapxi->xjoy = joyxi; al_unlock_mutex(hapxi_mutex); return &hapxi->parent; }
static void sdl_destroy_display(ALLEGRO_DISPLAY *d) { ALLEGRO_SYSTEM_SDL *s = (void *)al_get_system_driver(); al_lock_mutex(s->mutex); sdl_destroy_display_locked(d); al_unlock_mutex(s->mutex); }
/* Initializes the XInput haptic system. */ static bool hapxi_init_haptic(void) { int i; ASSERT(hapxi_mutex == NULL); ASSERT(hapxi_thread == NULL); ASSERT(hapxi_cond == NULL); /* Create the mutex and a condition vaiable. */ hapxi_mutex = al_create_mutex_recursive(); if (!hapxi_mutex) return false; hapxi_cond = al_create_cond(); if (!hapxi_cond) return false; al_lock_mutex(hapxi_mutex); for (i = 0; i < HAPTICS_MAX; i++) { haptics[i].active = false; } /* Now start a polling background thread, since XInput is a polled API, and also to make it possible for effects to stop running when their duration has passed. */ hapxi_thread = al_create_thread(hapxi_poll_thread, NULL); al_unlock_mutex(hapxi_mutex); if (hapxi_thread) al_start_thread(hapxi_thread); return(hapxi_thread != NULL); }
/* Function: al_append_native_text_log */ void al_append_native_text_log(ALLEGRO_TEXTLOG *textlog, char const *format, ...) { ALLEGRO_NATIVE_DIALOG *dialog = (ALLEGRO_NATIVE_DIALOG *)textlog; va_list args; /* Fall back to printf if no window. */ if (!dialog) { va_start(args, format); vprintf(format, args); va_end(args); return; } al_lock_mutex(dialog->tl_text_mutex); /* We could optimise the case where format="%s". */ va_start(args, format); al_ustr_vappendf(dialog->tl_pending_text, format, args); va_end(args); _al_append_native_text_log(dialog); al_unlock_mutex(dialog->tl_text_mutex); }
static void *android_app_trampoline(ALLEGRO_THREAD *thr, void *arg) { const int argc = 1; const char *argv[2] = {system_data.user_lib, NULL}; int ret; (void)thr; (void)arg; ALLEGRO_DEBUG("signaling running"); al_lock_mutex(system_data.mutex); system_data.trampoline_running = true; al_broadcast_cond(system_data.cond); al_unlock_mutex(system_data.mutex); ALLEGRO_DEBUG("entering main function %p", system_data.user_main); ret = (system_data.user_main)(argc, (char **)argv); /* Can we do anything with this exit code? */ ALLEGRO_DEBUG("returned from main function, exit code = %d", ret); /* NOTE: don't put any ALLEGRO_DEBUG in here after running main! */ android_cleanup(true); return NULL; }
/** retrieves a resource from the resource manager by name. If the resource is found, then its reference count is incremented. @param name the resource's name (UTF-8 string). @return pointer to the resource or NULL if the resource is not found. */ void *algui_acquire_resource(const char *name) { _RESOURCE *node; assert(name); al_lock_mutex(_mutex); //find the resource node = _find_resource_by_name(name); //if not found, return error if (!node) { al_unlock_mutex(_mutex); return NULL; } //increment the resource's ref count ++node->ref_count; assert(node->ref_count != 0); al_unlock_mutex(_mutex); //return the resource data return node->data; }
void Input::set(bool l, bool r, bool u, bool d, bool b1, bool b2, bool b3, int set_sets) { al_lock_mutex(mutex); set(l, r, u, d, set_sets); set(b1, b2, b3, set_sets); al_unlock_mutex(mutex); }
/** installs a new resource to the resource manager. The new resource's reference count is 1. @param res resource. @param name the resource's name (UTF-8 string); the string is copied internally. @param dtor destructor; invoked when the resource is removed. @return non-zero if the operation suceeded, zero if the resource already exists. */ int algui_install_resource(void *res, const char *name, void (*dtor)(void *)) { _RESOURCE *node; assert(res); assert(name); assert(dtor); al_lock_mutex(_mutex); //find the resource node = _find_resource_by_name(name); //if the resource is already installed, return error if (node) { al_unlock_mutex(_mutex); return 0; } //create a new resource node node = _create_resource(res, name, dtor, 1); //add it to the resource list algui_append_list_node(&_resources, &node->node); al_unlock_mutex(_mutex); //success return 1; }
/** uninstalls a resource from the resource manager. The destructor of the resource is not invoked. @param res pointer to the resource to uninstall; can be null. @return non-zero if the operation suceeded, zero if the resource does not exist. */ int algui_uninstall_resource(void *res) { _RESOURCE *node; //no resource if (!res) return 0; al_lock_mutex(_mutex); //find the resource node = _find_resource_by_data(res); //if the resource is not found, return error if (!node) { al_unlock_mutex(_mutex); return 0; } //uninstall the resource without invoking the destructor _uninstall_resource(node, 0); al_unlock_mutex(_mutex); //success return 1; }
/* * Must be called before the D3D device is reset (e.g., when * resizing a window). All non-synced display bitmaps must be * synced to memory. */ void _al_d3d_prepare_bitmaps_for_reset(ALLEGRO_DISPLAY_D3D *disp) { unsigned int i; if (disp->device_lost) return; if (!_al_d3d_render_to_texture_supported()) return; al_lock_mutex(_al_d3d_lost_device_mutex); for (i = 0; i < created_bitmaps._size; i++) { ALLEGRO_BITMAP_D3D **bptr = (ALLEGRO_BITMAP_D3D **)_al_vector_ref(&created_bitmaps, i); ALLEGRO_BITMAP_D3D *bmp = *bptr; ALLEGRO_BITMAP *al_bmp = (ALLEGRO_BITMAP *)bmp; if (bmp->display == disp) { //d3d_sync_bitmap_memory(al_bmp); if (!al_bmp->preserve_texture) { bmp->modified = false; } else if (!bmp->is_backbuffer && bmp->modified && !(al_bmp->flags & ALLEGRO_MEMORY_BITMAP)) { _al_d3d_sync_bitmap(al_bmp); bmp->modified = false; } } } al_unlock_mutex(_al_d3d_lost_device_mutex); }
static int packet_queue_put(PacketQueue * q, AVPacket * pkt) { AVPacketList *pkt1; if (pkt != &flush_pkt && av_dup_packet(pkt) < 0) { return -1; } pkt1 = av_malloc(sizeof(AVPacketList)); if (!pkt1) return -1; pkt1->pkt = *pkt; pkt1->next = NULL; al_lock_mutex(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; al_signal_cond(q->cond); al_unlock_mutex(q->mutex); return 0; }
/* ljoy_get_joystick: [primary thread] * * Returns the address of a ALLEGRO_JOYSTICK structure for the device * number NUM. */ static ALLEGRO_JOYSTICK *ljoy_get_joystick(int num) { ALLEGRO_JOYSTICK *ret = NULL; unsigned i; ASSERT(num >= 0); al_lock_mutex(config_mutex); for (i = 0; i < _al_vector_size(&joysticks); i++) { ALLEGRO_JOYSTICK_LINUX **slot = _al_vector_ref(&joysticks, i); ALLEGRO_JOYSTICK_LINUX *joy = *slot; if (ACTIVE_STATE(joy->config_state)) { if (num == 0) { ret = (ALLEGRO_JOYSTICK *)joy; break; } num--; } } al_unlock_mutex(config_mutex); return ret; }
/* * To avoid deadlocks, unlock the mutex returned by this function, rather than * whatever you passed as the argument. */ static ALLEGRO_MUTEX *maybe_lock_mutex(ALLEGRO_MUTEX *mutex) { if (mutex) { al_lock_mutex(mutex); } return mutex; }
static bool hapxi_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loops) { ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_device_for_id(id); ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi = hapxi_effect_for_id(id); if ((!hapxi) || (id->_id < 0) || (!effxi) || (loops < 1)) return false; al_lock_mutex(hapxi_mutex); /* Simply set some flags. The polling thread will see this and start playing. after the effect's delay has passed. */ effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STARTING; effxi->start_time = al_get_time(); effxi->stop_time = effxi->start_time + al_get_haptic_effect_duration(&effxi->effect) * loops; effxi->repeats = loops; effxi->play_repeated = 0; effxi->loop_start = effxi->start_time; id->_playing = true; id->_start_time = al_get_time(); id->_start_time = effxi->start_time; id->_end_time = effxi->stop_time; al_unlock_mutex(hapxi_mutex); al_signal_cond(hapxi_cond); return true; }
void Input::set(bool b1, bool b2, bool b3, int set_sets) { al_lock_mutex(mutex); descriptor.button1 = b1; descriptor.button2 = b2; descriptor.button3 = b3; int button_states[7] = { 1, 1, 1, 1, b1, b2, b3 }; if (orRelease >= 0) { if (!button_states[orRelease]) { timeOfNextNotification = tguiCurrentTimeMillis(); orRelease = -1; } } if (set_sets) { sets.button1 = b1; sets.button2 = b2; sets.button3 = b3; } al_unlock_mutex(mutex); }
/* emulate clear_keybuf() */ void clear_keybuf() { al_lock_mutex(keybuf_mutex); keybuf_len = 0; al_unlock_mutex(keybuf_mutex); }
static ALLEGRO_DISPLAY *sdl_create_display(int w, int h) { ALLEGRO_SYSTEM_SDL *s = (void *)al_get_system_driver(); al_lock_mutex(s->mutex); ALLEGRO_DISPLAY *d = sdl_create_display_locked(w, h); al_unlock_mutex(s->mutex); return d; }
void use_input_event() { al_lock_mutex(input_event_mutex); ie = EMPTY_INPUT_EVENT; al_unlock_mutex(input_event_mutex); }
void TripleInput::update() { al_lock_mutex(mutex); #if !defined ALLEGRO_IPHONE kb->update(); js->update(); InputDescriptor id1 = kb->getDescriptor(); InputDescriptor id2; if (js) id2 = js->getDescriptor(); else id2.left = id2.right = id2.up = id2.down = id2.button1 = id2.button2 = id2.button3 = false; InputDescriptor id3; id3.left = id3.right = id3.up = id3.down = id3.button1 = id3.button2 = id3.button3 = false; set( sets.left || id1.left || id2.left || id3.left, sets.right || id1.right || id2.right || id3.right, sets.up || id1.up || id2.up || id3.up, sets.down || id1.down || id2.down || id3.down, sets.button1 || id1.button1 || id2.button1 || id3.button1, sets.button2 || id1.button2 || id2.button2 || id3.button2, sets.button3 || id1.button3 || id2.button3 || id3.button3, false ); #elif defined ALLEGRO_IPHONE kb->update(); InputDescriptor id1 = kb->getDescriptor(); InputDescriptor id3; id3.left = id3.right = id3.up = id3.down = id3.button1 = id3.button2 = id3.button3 = false; InputDescriptor id4; id4.left = id4.right = id4.up = id4.down = id4.button1 = id4.button2 = id4.button3 = false; set( sets.left || id1.left || id3.left || id4.left, sets.right || id1.right || id3.right || id4.right, sets.up || id1.up || id3.up || id4.up, sets.down || id1.down || id3.down || id4.down, sets.button1 || id1.button1 || id3.button1 || id4.button1, sets.button2 || id1.button2 || id3.button2 || id4.button2, sets.button3 || id1.button3 || id3.button3 || id4.button3, false ); #endif al_unlock_mutex(mutex); }
static void toggle_pausedness(int n) { ThreadInfo *info = &thread_info[n]; al_lock_mutex(info->mutex); info->is_paused = !info->is_paused; al_broadcast_cond(info->cond); al_unlock_mutex(info->mutex); }
void add_input_event(INPUT_EVENT ie) { al_lock_mutex(input_event_mutex); ie.timestamp = al_get_time(); input_events.push_back(ie); al_unlock_mutex(input_event_mutex); }
/* This is called from the termination message - it has to return soon as the * user expects the app to close when it is closed. */ void _al_iphone_await_termination(void) { ALLEGRO_INFO("Application awaiting termination.\n"); al_lock_mutex(iphone->mutex); while (!iphone->has_shutdown) { al_wait_cond(iphone->cond, iphone->mutex); } al_unlock_mutex(iphone->mutex); }
static void *thread_func(ALLEGRO_THREAD *thr, void *arg) { ThreadInfo *info = (ThreadInfo *) arg; Viewport viewport; unsigned char palette[256][3]; int y, w, h; y = 0; w = al_get_bitmap_width(info->bitmap); h = al_get_bitmap_height(info->bitmap); viewport.centre_x = info->target_x; viewport.centre_y = info->target_y; viewport.x_extent = 3.0; viewport.y_extent = 3.0; viewport.zoom = 1.0; info->target_x = 0; info->target_y = 0; while (!al_get_thread_should_stop(thr)) { al_lock_mutex(info->mutex); while (info->is_paused) { al_wait_cond(info->cond, info->mutex); /* We might be awoken because the program is terminating. */ if (al_get_thread_should_stop(thr)) { break; } } if (!info->is_paused) { if (y == 0) { random_palette(palette, &info->random_seed); } draw_mandel_line(info->bitmap, &viewport, palette, y); y++; if (y >= h) { double z = viewport.zoom; y = 0; viewport.centre_x += z * viewport.x_extent * info->target_x; viewport.centre_y += z * viewport.y_extent * info->target_y; info->target_x = 0; info->target_y = 0; viewport.zoom *= 0.99; } } al_unlock_mutex(info->mutex); al_rest(0); } return NULL; }
void t3f_pause_music(void) { if(t3f_stream && t3f_music_mutex) { al_lock_mutex(t3f_music_mutex); al_set_audio_stream_playing(t3f_stream, false); al_unlock_mutex(t3f_music_mutex); t3f_set_music_state(T3F_MUSIC_STATE_PAUSED); } }
void t3f_resume_music(void) { if(t3f_stream && t3f_music_mutex) { al_lock_mutex(t3f_music_mutex); al_set_audio_stream_playing(t3f_stream, true); al_unlock_mutex(t3f_music_mutex); t3f_set_music_state(T3F_MUSIC_STATE_PLAYING); } }