int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } D(bug("WaitTimeout (%ld) semaphore...%lx\n",timeout,sem)); /* A timeout of 0 is an easy case */ if ( timeout == 0 ) { ObtainSemaphore(&sem->Sem); return 1; } if(!(retval=AttemptSemaphore(&sem->Sem))) { SDL_Delay(timeout); retval=AttemptSemaphore(&sem->Sem); } if(retval==TRUE) { // ReleaseSemaphore(&sem->Sem); retval=1; } return retval; }
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) { int retval; if ( ! sem ) { SDL_SetError("Passed a NULL semaphore"); return -1; } D(bug("WaitTimeout (%ld) semaphore...%lx\n",timeout,sem)); /* A timeout of 0 is an easy case */ if ( timeout == 0 ) { return SDL_SemTryWait(sem); } /* SDL_LockMutex(sem->count_lock); ++sem->waiters_count; retval = 0; while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) { retval = SDL_CondWaitTimeout(sem->count_nonzero, sem->count_lock, timeout); } --sem->waiters_count; --sem->count; SDL_UnlockMutex(sem->count_lock); */ if(!(retval=AttemptSemaphore(&sem->Sem))) { SDL_Delay(timeout); retval=AttemptSemaphore(&sem->Sem); } if(retval==TRUE) { // ReleaseSemaphore(&sem->Sem); retval=1; } return retval; }
int MorphOS_TryLockMutex(struct SignalSemaphore *mutex) { if (!mutex) { return -1; } if (AttemptSemaphore(mutex) == 0) { return -1; } return 0; }
/// LockMailTransferList() void LockMailTransferList(const struct MailTransferList *tlist) { ENTER(); if(AttemptSemaphore(tlist->lockSemaphore) == FALSE) { if(transferLocks > 0) E(DBF_ALWAYS, "nested (%ld) exclusive lock of MailTransferList %08lx", transferLocks + 1, tlist); ObtainSemaphore(tlist->lockSemaphore); } transferLocks++; LEAVE(); }
APTR ASMCALL ISAPNP_LockDevicesA( REG( d0, ULONG flags ), REG( a0, struct ISAPNP_Device** devices ), REG( a6, struct ISAPNPBase* res ) ) { struct ISAPNP_Device** devices_ptr; struct ISAPNP_Device** result; result = (struct ISAPNP_Device**) MakeSortedArray( (void**) devices ); if( result != NULL ) { for( devices_ptr = result; *devices_ptr != NULL; ++devices_ptr ) { if( flags & ISAPNP_LOCKF_NONBLOCKING ) { if( ! AttemptSemaphore( &(*devices_ptr)->isapnpd_Lock ) ) { struct ISAPNP_Device** devices_end; // Oops! Failed to lock one of the devices! devices_end = devices_ptr; for( devices_ptr = result; devices_ptr != devices_end; ++devices_ptr ); { ReleaseSemaphore( &(*devices_ptr)->isapnpd_Lock ); } FreeVec( result ); result = NULL; break; } } else { ObtainSemaphore( &(*devices_ptr)->isapnpd_Lock ); } } } return (APTR) result; }
APTR ASMCALL ISAPNP_LockCardsA( REG( d0, ULONG flags ), REG( a0, struct ISAPNP_Card** cards ), REG( a6, struct ISAPNPBase* res ) ) { struct ISAPNP_Card** cards_ptr; struct ISAPNP_Card** result; result = (struct ISAPNP_Card**) MakeSortedArray( (void**) cards ); if( result != NULL ) { for( cards_ptr = result; *cards_ptr != NULL; ++cards_ptr ) { if( flags & ISAPNP_LOCKF_NONBLOCKING ) { if( ! AttemptSemaphore( &(*cards_ptr)->isapnpc_Lock ) ) { struct ISAPNP_Card** cards_end; // Oops! Failed to lock one of the cards! cards_end = cards_ptr; for( cards_ptr = result; cards_ptr != cards_end; ++cards_ptr ); { ReleaseSemaphore( &(*cards_ptr)->isapnpc_Lock ); } FreeVec( result ); result = NULL; break; } } else { ObtainSemaphore( &(*cards_ptr)->isapnpc_Lock ); } } } return (APTR) result; }
/************************************************************************** MUIM_Semaphore_Attempt **************************************************************************/ IPTR Semaphore__MUIM_Attempt(struct IClass *cl, Object *obj, struct MUIP_Semaphore_Attempt *msg) { struct MUI_SemaphoreData *data = INST_DATA(cl, obj); return (IPTR)AttemptSemaphore(&data->sem); }
long LIBFUNC L_GetSemaphore( REG(a0, struct SignalSemaphore *sem), REG(d0, long exclusive), REG(a1, char *name)) { #ifdef TRAP_SEMAPHORE SemaphoreNode *node; SemaphoreOwner *owner=0; struct MyLibrary *libbase; struct LibData *data; #endif // Attempt only? if (exclusive&SEMF_ATTEMPT) { long res; // Want exclusive lock? if (exclusive&SEMF_EXCLUSIVE) res=AttemptSemaphore(sem); // Shared lock else { // Try for shared semaphore if (!(res=AttemptSemaphoreShared(sem))) { // Fix <v39 bug if (((struct Library *)SysBase)->lib_Version<39) { // Try for exclusive instead res=AttemptSemaphore(sem); } } } return res; } #ifdef TRAP_SEMAPHORE // Get our library data libbase=GET_DOPUSLIB; data=(struct LibData *)libbase->ml_UserData; // Lock semaphore list ObtainSemaphore(&data->semaphores.lock); // Look for semaphore in list for (node=(SemaphoreNode *)data->semaphores.list.lh_Head; node->node.mln_Succ; node=(SemaphoreNode *)node->node.mln_Succ) { // Try to match semaphore if (node->sem==sem) break; } // Not matched? if (!node->node.mln_Succ) { // Allocate node if (node=AllocVec(sizeof(SemaphoreNode),MEMF_CLEAR)) { // Add to semaphore list AddTail(&data->semaphores.list,(struct Node *)node); // Set semaphore pointer node->sem=sem; // Initialise lists NewList((struct List *)&node->owners); NewList((struct List *)&node->waiters); } } // Got a valid node? if (node) { // Allocate owner node if (owner=AllocVec(sizeof(SemaphoreOwner),MEMF_CLEAR)) { // Fill out owner owner->task=FindTask(0); owner->exclusive=exclusive; owner->data=name; // Add to waiter list AddTail((struct List *)&node->waiters,(struct Node *)owner); } } // Unlock semaphore list ReleaseSemaphore(&data->semaphores.lock); #endif // Try to get the actual semaphore if (exclusive&SEMF_EXCLUSIVE) ObtainSemaphore(sem); else { // Fix <v39 bug if (((struct Library *)SysBase)->lib_Version<39) { // Try to get shared semaphore if (!AttemptSemaphoreShared(sem)) { // Try for exclusive semaphore if (!AttemptSemaphore(sem)) { // Wait for shared lock ObtainSemaphoreShared(sem); } } } // Under 39, just ask for shared lock else ObtainSemaphoreShared(sem); } #ifdef TRAP_SEMAPHORE // Valid owner? if (owner) { // Lock semaphore list ObtainSemaphore(&data->semaphores.lock); // Remove us from the waiter queue Remove((struct Node *)owner); // Add us to the owner queue AddTail((struct List *)&node->owners,(struct Node *)owner); // Unlock semaphore list ReleaseSemaphore(&data->semaphores.lock); } #endif return TRUE; }