Exemple #1
0
int AudioQueue_copy (AudioQueue* self, Uint8* buf, int size)
{
	if (size <= 0) return 0;
	if (SDL_SemValue(self->full_node) > 0) {
		if (!self->buf) self->buf = self->cur->buf;
		int cur_size = self->cur->size - (self->buf - self->cur->buf);
		int size_taken = SDL_min(size, cur_size);

		Copy(buf, self->buf, size_taken, Uint8);

		cur_size -= size_taken;
		if (cur_size <= 0) {
			// move to the next node
			SDL_SemPost(self->empty_node);
			if (++self->cur - self->nodes >= self->n) self->cur = self->nodes;
			self->buf = NULL;

			buf  += size_taken;
			size -= size_taken;
			SDL_SemWait(self->full_node); // this never blocks
			return size_taken + AudioQueue_copy(self, buf, size);
		}
		else {
			// didn't use the entire current node
			self->buf += size_taken;
			return size_taken;
		}
	}
	else {
		Zero(buf, size, Uint8);
		return 0;
	}
}
Exemple #2
0
int Sync_Audio::sample_count() const
{
	if ( !free_sem )
		return 0;
	
	int buf_free = SDL_SemValue( free_sem ) * buf_size + (buf_size - write_pos);
	return buf_size * buf_count - buf_free;
}
Exemple #3
0
int SDLCALL
ThreadFunc(void *data)
{
    int threadnum = (int) (uintptr_t) data;
    while (alive) {
        SDL_SemWait(sem);
        SDL_Log("Thread number %d has got the semaphore (value = %d)!\n",
                threadnum, SDL_SemValue(sem));
        SDL_Delay(200);
        SDL_SemPost(sem);
        SDL_Log("Thread number %d has released the semaphore (value = %d)!\n",
                threadnum, SDL_SemValue(sem));
        SDL_Delay(1);           /* For the scheduler */
    }
    SDL_Log("Thread number %d exiting.\n", threadnum);
    return 0;
}
Exemple #4
0
extern "C" void projectM_render_pcm(gint16 pcm_data[2][512])
{
  //SDL_mutexP(mutex); while ( SDL_SemValue(sem)==1 )
  if ( SDL_SemValue(sem)==1 )
        globalPM->pcm()->addPCM16(pcm_data);
	 
       	//SDL_mutexV(mutex);
	
}
Exemple #5
0
// Pause
extern "C" void Java_org_libsdl_app_SDLActivity_nativePause(
                                    JNIEnv* env, jclass cls)
{
    if (Android_Window) {
        /* Signal the pause semaphore so the event loop knows to pause and (optionally) block itself */
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
    }
}
Exemple #6
0
/** 
 * A SDL timer callback function. Signals the supplied SDL semaphore
 * if its value is small.
 * 
 * @param interval The interval since the last call (in ms)
 * @param param The pointer to the semaphore.
 * 
 * @return The interval to the next call (required by SDL)
 */
static Uint32
glade_fps_limiter_fn( Uint32 interval, void *param) {
  SDL_sem *sdl_semaphore = (SDL_sem *)param;

  /* signal the semaphore if it is getting low */
  if ( SDL_SemValue( sdl_semaphore) < 4) {
    SDL_SemPost( sdl_semaphore);
  }

  return interval;
}
Exemple #7
0
/*
 * ===========
 * AllocPortal
 * ===========
 */
static portal_t *AllocPortal(void) {

    if (debug) {
        SDL_SemPost(semaphores.active_portals);
        const uint32_t active_portals = SDL_SemValue(semaphores.active_portals);

        if (active_portals > c_peak_portals)
            c_peak_portals = active_portals;
    }

    return Mem_Malloc(sizeof(portal_t));
}
Exemple #8
0
int Threads_Glue_SemWait(void *Ptr, int Max)
{
	 int	have = 0;
	assert(Ptr);
	do {
		if( SDL_SemWait( Ptr ) == -1 ) {
			return -1;
		}
		have ++;
	} while( SDL_SemValue(Ptr) && have < Max );
	return have;
}
Exemple #9
0
static mrb_value
mrb_sdl2_semaphore_get_value(mrb_state *mrb, mrb_value self)
{
  Uint32 value;
  mrb_sdl2_semaphore_data_t *data =
    (mrb_sdl2_semaphore_data_t*)mrb_data_get_ptr(mrb, self, &mrb_sdl2_semaphore_data_type);
  if (NULL == data->semaphore) {
    return mrb_nil_value();
  }
  value = SDL_SemValue(data->semaphore);
  return mrb_fixnum_value((mrb_int)value);
}
Exemple #10
0
/**
 * @brief
 */
winding_t *AllocWinding(int32_t points) {

	if (debug) {
		SDL_SemPost(semaphores.active_windings);
		uint32_t active_windings = SDL_SemValue(semaphores.active_windings);

		if (active_windings > c_peak_windings) {
			c_peak_windings = active_windings;
		}
	}

	return Mem_TagMalloc(sizeof(int32_t) + sizeof(vec3_t) * points, MEM_TAG_WINDING);
}
Exemple #11
0
// Resume
extern "C" void Java_org_libsdl_app_SDLActivity_nativeResume(
                                    JNIEnv* env, jclass cls)
{
    if (Android_Window) {
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
         * and this function will be called from the Java thread instead.
         */
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
    }
}
void Sound_Queue::fill_buffer( Uint8* out, int count )
{
	if ( SDL_SemValue( free_sem ) < buf_count - 1 )
	{
		currently_playing_ = buf( read_buf );
		memcpy( out, buf( read_buf ), count );
		read_buf = (read_buf + 1) % buf_count;
		SDL_SemPost( free_sem );
	}
	else
	{
		memset( out, 0, count );
	}
}
Exemple #13
0
// Pause
extern "C" void Java_org_libsdl_app_SDLActivity_nativePause(
                                    JNIEnv* env, jclass cls)
{
    if (Android_Window) {
        /* Signal the pause semaphore so the event loop knows to pause and (optionally) block itself */
        if (!SDL_SemValue(Android_PauseSem)) SDL_SemPost(Android_PauseSem);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
    }

    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativePause()");
    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
}
Exemple #14
0
// Wait for D3D to finish processing and return the result
HRESULT CDirect3D::Wait(bool unlock) {
    HRESULT res;

    EnterCriticalSection(&cs);
    while(thread_command != D3D_IDLE) {
	wait = true;
	LeaveCriticalSection(&cs);
#if LOG_D3D
	LOG_MSG("D3D:Waiting for D3D thread to finish processing...(command: %d)", thread_command);
#endif
	SDL_SemWait(thread_ack);
	EnterCriticalSection(&cs);
	wait = false;
    }

#if LOG_D3D
    if(SDL_SemValue(thread_ack)) LOG_MSG("D3D:Semaphore has value: %d!", SDL_SemValue(thread_ack));
#endif

    res = thread_hr;
    if(unlock) LeaveCriticalSection(&cs);
    return res;
}
Exemple #15
0
void UpdateScreen(void)
{
	if (!params.scale2x) {
		// do not use threading here, because realScreen == screen

		if (params.useFlipSurface) SDL_Flip(realScreen);
		else SDL_UpdateRect(realScreen, 0, 0, 0, 0);

		return;
	}

#if defined(__APPLE__)
	if (SDL_SemValue(updateScreenThreadSem)) {
		return;
	}
#endif

	if (SDL_MUSTLOCK(realScreen)) { if (SDL_LockSurface(realScreen) < 0) return; }

	if (SDL_MUSTLOCK(screen)) {
		if (SDL_LockSurface(screen) < 0) {
			if (SDL_MUSTLOCK(realScreen)) SDL_UnlockSurface(realScreen);
			return;
		}
	}

	if (params.scanlines)
	{
		for (int i = HEIGHT-1; i >= 0; i--)
		{
			int* line = (int*)screen->pixels + i * PITCH + WIDTH-1;
			int* lineA = (int*)realScreen->pixels + (i*2) * REAL_PITCH + (WIDTH*2-1);
			int* lineB = (int*)realScreen->pixels + (i*2+1) * REAL_PITCH + (WIDTH*2-1);

			for (int j = WIDTH; j--;)
			{
				int c = *(line--);
				int dc = (c & 0xFEFEFE) >> 1;

				*(lineA--) = c;
				*(lineA--) = c;
				*(lineB--) = dc;
				*(lineB--) = dc;
			}
		}
	}
	else
	{
		for (int i = HEIGHT-1; i >= 0; i--)
Exemple #16
0
/* @brief Run every job in the vpool queue and block until every job in the
 * queue is done.
 * @note It destroys the queue when it's done.
 */
void vpool_wait( ThreadQueue *queue )
{
   int i, cnt;
   SDL_cond *cond;
   SDL_mutex *mutex;
   vpoolThreadData *arg;
   ThreadQueueData *node;

   /* Create temporary threading structures. */
   cond  = SDL_CreateCond();
   mutex = SDL_CreateMutex();
   /* This might be a little ugly (and inefficient?) */
   cnt   = SDL_SemValue( queue->semaphore );

   /* Allocate all vpoolThreadData objects */
   arg = calloc( cnt, sizeof(vpoolThreadData) );

   SDL_mutexP( mutex );
   /* Initialize the vpoolThreadData */
   for (i=0; i<cnt; i++) {
      /* This is needed to keep the invariants of the queue */
      while (SDL_SemWait( queue->semaphore ) == -1) {
          /* Again, a really bad idea */
          WARN("L%d: SDL_SemWait failed! Error: %s", __LINE__, SDL_GetError());
      }
      node = tq_dequeue( queue );

      /* Set up arguments. */
      arg[i].node    = node;
      arg[i].cond    = cond;
      arg[i].mutex   = mutex;
      arg[i].count   = &cnt;

      /* Launch new job. */
      threadpool_newJob( vpool_worker, &arg[i] );
   }

   /* Wait for the threads to finish */
   SDL_CondWait( cond, mutex );
   SDL_mutexV( mutex );

   /* Clean up */
   SDL_DestroyMutex( mutex );
   SDL_DestroyCond( cond );
   tq_destroy( queue );
   free(arg);
}
Exemple #17
0
// Resume
extern "C" void Java_org_libsdl_app_SDLActivity_nativeResume(
                                    JNIEnv* env, jclass cls)
{
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "nativeResume()");
    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);

    if (Android_Window) {
        /* Signal the resume semaphore so the event loop knows to resume and restore the GL Context
         * We can't restore the GL Context here because it needs to be done on the SDL main thread
         * and this function will be called from the Java thread instead.
         */
        if (!SDL_SemValue(Android_ResumeSem)) SDL_SemPost(Android_ResumeSem);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
    }
}
Exemple #18
0
void PathStackFree(void* _Ptr) {
	SDL_SemPost(g_PathStack.Sem);
	SDL_LockMutex(g_PathStack.Lock);
	int _Size = PATHTABLE_SIZE - SDL_SemValue(g_PathStack.Sem);
	int _Bottom = _Size + g_PathStack.Top;
	void* _Temp = NULL;

	if(_Bottom >= PATHTABLE_SIZE)
		_Bottom -= PATHTABLE_SIZE;
	for(int i = 0; i < PATHTABLE_SIZE; ++i)
		if(g_PathStack.Stack[i] == _Ptr) {
			_Temp = g_PathStack.Stack[i];
			g_PathStack.Stack[i] = g_PathStack.Stack[_Bottom];
			g_PathStack.Stack[_Bottom] = _Temp;
			break;
		}
	SDL_UnlockMutex(g_PathStack.Lock);
}
Exemple #19
0
static int
kaillera_thread(void *arg)
{
	kailleraInit();
	
	/* print_version(); */

	kailleraSetInfos(&kaillera_data);

	kailleraSelectServerDialog(0);
	if (SDL_SemValue(game_start_sem) == 0) {
		/* server dialog returned and game didnt start */
		
		/* release blocking thread */
		my_player = -1;
		SDL_SemPost(game_start_sem);
	}

	return 0;
}
Exemple #20
0
int worker_func(void*)
{ 
// char projectM_data[1024]; 
 SDL_TimerID title_timer = NULL;
 std::string config_file;
 config_file = read_config();
 ConfigFile config(config_file);

 int wvw = config.read<int>( "Window Width", 512 );
 int wvh = config.read<int>( "Window Height", 512 );

 int fullscreen = 0;
 if (config.read("Fullscreen", true)) fullscreen = 1;
      else fullscreen = 0;

  init_display(wvw,wvh,&fvw,&fvh,fullscreen); 
  SDL_WM_SetCaption("projectM v1.00", "projectM v1.00");

  /** Initialise projectM */
    
  globalPM = new projectM(config_file);
  SDL_SemPost(sem);
  title_timer = SDL_AddTimer(500, get_xmms_title, NULL);
    /** Initialise the thread */
  // SDL_SemTryWait(sem);
  while ( SDL_SemValue(sem)==1 ) {
        projectMEvent evt;
        projectMKeycode key;
        projectMModifier mod;

        /** Process SDL events */
        SDL_Event event;
        while ( SDL_PollEvent( &event ) ) {
            /** Translate into projectM codes and process */
            evt = sdl2pmEvent( event );	  

            key = sdl2pmKeycode( event.key.keysym.sym );
            mod = sdl2pmModifier( event.key.keysym.mod );

            if ( evt == PROJECTM_KEYDOWN ) {
	   
	      if(key == PROJECTM_K_c)
		{
		  //SDL_SaveBMP(screen, "/home/pete/1.bmp");
		  saveSnapshotToFile();
		}
	      if(key == PROJECTM_K_v)
		{
		  // capture++;
		}
	      if(key == PROJECTM_K_f)
		{
		 

		 int w, h;
                    if (fullscreen == 0) {
                        w = fvw;
                        h = fvh;
			fullscreen = 1;
                    } else {
                        w = wvw;
                        h = wvh;
			fullscreen = 0;
                    }
                  
                    resize_display(w, h, fullscreen);
                    globalPM->projectM_resetGL( w, h ); 
                }
	      else  globalPM->key_handler(evt,key,mod);

              }
	    else if ( evt == PROJECTM_VIDEORESIZE )
	      {

	       

		wvw=event.resize.w;
		wvh=event.resize.h;
	       
		
		resize_display(wvw,wvh,fullscreen);
		globalPM->projectM_resetGL( wvw, wvh ); 
 
              } 
	    else if ( evt == PROJECTM_VIDEOQUIT ) {
	      
	      (void) gtk_idle_add (disable_projectm, NULL);
	    }
	}
	
        /** Add the waveform data */
      

        /** Render the new frame */
	//	 strcpy(title,xmms_remote_get_playlist_title(projectM_vtable.xmms_session, xmms_remote_get_playlist_pos(projectM_vtable.xmms_session))); 

	 //printf("%s\n",title);
	// strcpy(globalPM->title,title);
	
	  globalPM->renderFrame();
	
      

        SDL_GL_SwapBuffers();

	if (capture % 2 == 1) saveSnapshotToFile();
	//	SDL_SemPost(sem);
      }

 if(title_timer) 
  	SDL_RemoveTimer(title_timer);
 delete globalPM;


 return 0;
}
Exemple #21
0
	unsigned int Semaphore::value()
	{
		return SDL_SemValue(semaphore);
	}
Exemple #22
0
template <typename T> bool queue<T>::empty()
{
    return (SDL_SemValue(full_slots) == 0);
}
Exemple #23
0
/*
 * =================
 * BrushBSP
 *
 * The incoming list will be freed before exiting
 * =================
 */
tree_t *BrushBSP(brush_t *brushlist, vec3_t mins, vec3_t maxs) {
	node_t *node;
	brush_t *b;
	int32_t c_faces, c_nonvisfaces;
	int32_t c_brushes;
	tree_t *tree;
	int32_t i;
	vec_t volume;

	Com_Debug(DEBUG_ALL, "--- BrushBSP ---\n");

	tree = AllocTree();

	c_faces = 0;
	c_nonvisfaces = 0;
	c_brushes = 0;
	for (b = brushlist; b; b = b->next) {
		c_brushes++;

		volume = BrushVolume(b);
		if (volume < microvolume) {
			Mon_SendSelect(ERROR_WARN, b->original->entity_num, b->original->brush_num, "Microbrush");
		}

		for (i = 0; i < b->num_sides; i++) {
			if (b->sides[i].bevel) {
				continue;
			}
			if (!b->sides[i].winding) {
				continue;
			}
			if (b->sides[i].texinfo == TEXINFO_NODE) {
				continue;
			}
			if (b->sides[i].visible) {
				c_faces++;
			} else {
				c_nonvisfaces++;
			}
		}

		AddPointToBounds(b->mins, tree->mins, tree->maxs);
		AddPointToBounds(b->maxs, tree->mins, tree->maxs);
	}

	Com_Debug(DEBUG_ALL, "%5i brushes\n", c_brushes);
	Com_Debug(DEBUG_ALL, "%5i visible faces\n", c_faces);
	Com_Debug(DEBUG_ALL, "%5i nonvisible faces\n", c_nonvisfaces);

	SDL_DestroySemaphore(semaphores.vis_nodes);
	semaphores.vis_nodes = SDL_CreateSemaphore(0);

	SDL_DestroySemaphore(semaphores.nonvis_nodes);
	semaphores.nonvis_nodes = SDL_CreateSemaphore(0);

	node = AllocNode();

	node->volume = BrushFromBounds(mins, maxs);

	tree->head_node = node;

	node = BuildTree_r(node, brushlist);

	const uint32_t vis_nodes = SDL_SemValue(semaphores.vis_nodes);
	const uint32_t nonvis_nodes = SDL_SemValue(semaphores.nonvis_nodes);

	Com_Debug(DEBUG_ALL, "%5i visible nodes\n", vis_nodes / 2 - nonvis_nodes);
	Com_Debug(DEBUG_ALL, "%5i nonvis nodes\n", nonvis_nodes);
	Com_Debug(DEBUG_ALL, "%5i leafs\n", (vis_nodes + 1) / 2);

	return tree;
}
Exemple #24
0
bool save_blocker::saves_are_blocked() {
	return SDL_SemValue(sem_) == 0;
}
		Uint32 SemValue(Sem *sem)
		{
			return SDL_SemValue(sem);
		}
Exemple #26
0
def_dll Uint32 sdl_semaphore::value()
{
	return SDL_SemValue(_semaphore);
}
Exemple #27
0
void switch_thread(void)
{
    struct thread_entry *current = cores[CURRENT_CORE].running;

    enable_irq();

    switch (current->state)
    {
    case STATE_RUNNING:
    {
        SDL_UnlockMutex(m);
        /* Any other thread waiting already will get it first */
        SDL_LockMutex(m);
        break;
        } /* STATE_RUNNING: */

    case STATE_BLOCKED:
    {
        int oldlevel;

        SDL_UnlockMutex(m);
        SDL_SemWait(current->context.s);
        SDL_LockMutex(m);

        oldlevel = disable_irq_save();
        current->state = STATE_RUNNING;
        restore_irq(oldlevel);
        break;
        } /* STATE_BLOCKED: */

    case STATE_BLOCKED_W_TMO:
    {
        int result, oldlevel;

        SDL_UnlockMutex(m);
        result = SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
        SDL_LockMutex(m);

        oldlevel = disable_irq_save();

        if (current->state == STATE_BLOCKED_W_TMO)
        {
            /* Timed out */
            remove_from_list_l(current->bqp, current);

#ifdef HAVE_WAKEUP_EXT_CB
            if (current->wakeup_ext_cb != NULL)
                current->wakeup_ext_cb(current);
#endif
            current->state = STATE_RUNNING;
        }

        if (result == SDL_MUTEX_TIMEDOUT)
        {
            /* Other signals from an explicit wake could have been made before
             * arriving here if we timed out waiting for the semaphore. Make
             * sure the count is reset. */
            while (SDL_SemValue(current->context.s) > 0)
                SDL_SemTryWait(current->context.s);
        }

        restore_irq(oldlevel);
        break;
        } /* STATE_BLOCKED_W_TMO: */

    case STATE_SLEEPING:
    {
        SDL_UnlockMutex(m);
        SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
        SDL_LockMutex(m);
        current->state = STATE_RUNNING;
        break;
        } /* STATE_SLEEPING: */
    }

    cores[CURRENT_CORE].running = current;

    if (threads_status != THREADS_RUN)
        thread_exit();
}
Exemple #28
0
template <typename T> bool queue<T>::full()
{
    return (SDL_SemValue(free_slots) == 0);
}
Exemple #29
0
void save_blocker::unblock() {
	assert(SDL_SemValue(sem_) == 0);
	SDL_SemPost(sem_);
}
int Sound_Queue::sample_count() const
{
	int buf_free = SDL_SemValue( free_sem ) * buf_size + (buf_size - write_pos);
	return buf_size * buf_count - buf_free;
}