src/thread/riscos/SDL_syscond.c
changeset 955 d74fbf56f2f6
child 1312 c9b51268668f
equal deleted inserted replaced
954:3acd16ea0180 955:d74fbf56f2f6
       
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997-2004 Sam Lantinga
       
     4 
       
     5     This library is free software; you can redistribute it and/or
       
     6     modify it under the terms of the GNU Library General Public
       
     7     License as published by the Free Software Foundation; either
       
     8     version 2 of the License, or (at your option) any later version.
       
     9 
       
    10     This library is distributed in the hope that it will be useful,
       
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13     Library General Public License for more details.
       
    14 
       
    15     You should have received a copy of the GNU Library General Public
       
    16     License along with this library; if not, write to the Free
       
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18 
       
    19     Sam Lantinga
       
    20     slouken@libsdl.org
       
    21 */
       
    22 
       
    23 /* RISC OS implementations uses pthreads based on linux code */
       
    24 
       
    25 #ifdef SAVE_RCSID
       
    26 static char rcsid =
       
    27  "@(#) $Id$";
       
    28 #endif
       
    29 
       
    30 #ifdef DISABLE_THREADS
       
    31 #include "../generic/SDL_syscond.c"
       
    32 #else
       
    33 #include <sys/time.h>
       
    34 #include <unistd.h>
       
    35 #include <errno.h>
       
    36 #include <stdlib.h>
       
    37 #include <pthread.h>
       
    38 
       
    39 #include "SDL_error.h"
       
    40 #include "SDL_thread.h"
       
    41 #include "SDL_sysmutex_c.h"
       
    42 
       
    43 struct SDL_cond
       
    44 {
       
    45 	pthread_cond_t cond;
       
    46 };
       
    47 
       
    48 /* Create a condition variable */
       
    49 SDL_cond * SDL_CreateCond(void)
       
    50 {
       
    51 	SDL_cond *cond;
       
    52 
       
    53 	cond = (SDL_cond *) malloc(sizeof(SDL_cond));
       
    54 	if ( cond ) {
       
    55 		if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
       
    56 			SDL_SetError("pthread_cond_init() failed");
       
    57 			free(cond);
       
    58 			cond = NULL;
       
    59 		}
       
    60 	}
       
    61 	return(cond);
       
    62 }
       
    63 
       
    64 /* Destroy a condition variable */
       
    65 void SDL_DestroyCond(SDL_cond *cond)
       
    66 {
       
    67 	if ( cond ) {
       
    68 		pthread_cond_destroy(&cond->cond);
       
    69 		free(cond);
       
    70 	}
       
    71 }
       
    72 
       
    73 /* Restart one of the threads that are waiting on the condition variable */
       
    74 int SDL_CondSignal(SDL_cond *cond)
       
    75 {
       
    76 	int retval;
       
    77 
       
    78 	if ( ! cond ) {
       
    79 		SDL_SetError("Passed a NULL condition variable");
       
    80 		return -1;
       
    81 	}
       
    82 
       
    83 	retval = 0;
       
    84 	if ( pthread_cond_signal(&cond->cond) != 0 ) {
       
    85 		SDL_SetError("pthread_cond_signal() failed");
       
    86 		retval = -1;
       
    87 	}
       
    88 	return retval;
       
    89 }
       
    90 
       
    91 /* Restart all threads that are waiting on the condition variable */
       
    92 int SDL_CondBroadcast(SDL_cond *cond)
       
    93 {
       
    94 	int retval;
       
    95 
       
    96 	if ( ! cond ) {
       
    97 		SDL_SetError("Passed a NULL condition variable");
       
    98 		return -1;
       
    99 	}
       
   100 
       
   101 	retval = 0;
       
   102 	if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
       
   103 		SDL_SetError("pthread_cond_broadcast() failed");
       
   104 		retval = -1;
       
   105 	}
       
   106 	return retval;
       
   107 }
       
   108 
       
   109 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
       
   110 {
       
   111 	int retval;
       
   112 	struct timeval delta;
       
   113 	struct timespec abstime;
       
   114 
       
   115 	if ( ! cond ) {
       
   116 		SDL_SetError("Passed a NULL condition variable");
       
   117 		return -1;
       
   118 	}
       
   119 
       
   120 	gettimeofday(&delta, NULL);
       
   121 
       
   122 	abstime.tv_sec = delta.tv_sec + (ms/1000);
       
   123 	abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
       
   124         if ( abstime.tv_nsec > 1000000000 ) {
       
   125           abstime.tv_sec += 1;
       
   126           abstime.tv_nsec -= 1000000000;
       
   127         }
       
   128 
       
   129   tryagain:
       
   130 	retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
       
   131 	switch (retval) {
       
   132 	    case EINTR:
       
   133 		goto tryagain;
       
   134 		break;
       
   135 	    case ETIMEDOUT:
       
   136 		retval = SDL_MUTEX_TIMEDOUT;
       
   137 		break;
       
   138 	    case 0:
       
   139 		break;
       
   140 	    default:
       
   141 		SDL_SetError("pthread_cond_timedwait() failed");
       
   142 		retval = -1;
       
   143 		break;
       
   144 	}
       
   145 	return retval;
       
   146 }
       
   147 
       
   148 /* Wait on the condition variable, unlocking the provided mutex.
       
   149    The mutex must be locked before entering this function!
       
   150  */
       
   151 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
       
   152 {
       
   153 	int retval;
       
   154 
       
   155 	if ( ! cond ) {
       
   156 		SDL_SetError("Passed a NULL condition variable");
       
   157 		return -1;
       
   158 	}
       
   159 
       
   160 	retval = 0;
       
   161 	if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
       
   162 		SDL_SetError("pthread_cond_wait() failed");
       
   163 		retval = -1;
       
   164 	}
       
   165 	return retval;
       
   166 }
       
   167 #endif