PageRenderTime 44ms CodeModel.GetById 19ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/project/jni/sdl-1.3/src/thread/beos/SDL_syssem.c

https://github.com/aichunyu/FFPlayer
C | 155 lines | 108 code | 21 blank | 26 comment | 15 complexity | 248efb7536dca3ece7fc3c84ab905c25 MD5 | raw file
  1/*
  2  Simple DirectMedia Layer
  3  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
  4
  5  This software is provided 'as-is', without any express or implied
  6  warranty.  In no event will the authors be held liable for any damages
  7  arising from the use of this software.
  8
  9  Permission is granted to anyone to use this software for any purpose,
 10  including commercial applications, and to alter it and redistribute it
 11  freely, subject to the following restrictions:
 12
 13  1. The origin of this software must not be misrepresented; you must not
 14     claim that you wrote the original software. If you use this software
 15     in a product, an acknowledgment in the product documentation would be
 16     appreciated but is not required.
 17  2. Altered source versions must be plainly marked as such, and must not be
 18     misrepresented as being the original software.
 19  3. This notice may not be removed or altered from any source distribution.
 20*/
 21#include "SDL_config.h"
 22
 23#ifdef SDL_THREAD_BEOS
 24
 25/* Semaphores in the BeOS environment */
 26
 27#include <be/kernel/OS.h>
 28
 29#include "SDL_thread.h"
 30
 31
 32struct SDL_semaphore
 33{
 34    sem_id id;
 35};
 36
 37/* Create a counting semaphore */
 38SDL_sem *
 39SDL_CreateSemaphore(Uint32 initial_value)
 40{
 41    SDL_sem *sem;
 42
 43    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
 44    if (sem) {
 45        sem->id = create_sem(initial_value, "SDL semaphore");
 46        if (sem->id < B_NO_ERROR) {
 47            SDL_SetError("create_sem() failed");
 48            SDL_free(sem);
 49            sem = NULL;
 50        }
 51    } else {
 52        SDL_OutOfMemory();
 53    }
 54    return (sem);
 55}
 56
 57/* Free the semaphore */
 58void
 59SDL_DestroySemaphore(SDL_sem * sem)
 60{
 61    if (sem) {
 62        if (sem->id >= B_NO_ERROR) {
 63            delete_sem(sem->id);
 64        }
 65        SDL_free(sem);
 66    }
 67}
 68
 69int
 70SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
 71{
 72    int32 val;
 73    int retval;
 74
 75    if (!sem) {
 76        SDL_SetError("Passed a NULL semaphore");
 77        return -1;
 78    }
 79
 80  tryagain:
 81    if (timeout == SDL_MUTEX_MAXWAIT) {
 82        val = acquire_sem(sem->id);
 83    } else {
 84        timeout *= 1000;        /* BeOS uses a timeout in microseconds */
 85        val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout);
 86    }
 87    switch (val) {
 88    case B_INTERRUPTED:
 89        goto tryagain;
 90    case B_NO_ERROR:
 91        retval = 0;
 92        break;
 93    case B_TIMED_OUT:
 94        retval = SDL_MUTEX_TIMEDOUT;
 95        break;
 96    case B_WOULD_BLOCK:
 97        retval = SDL_MUTEX_TIMEDOUT;
 98        break;
 99    default:
100        SDL_SetError("acquire_sem() failed");
101        retval = -1;
102        break;
103    }
104
105    return retval;
106}
107
108int
109SDL_SemTryWait(SDL_sem * sem)
110{
111    return SDL_SemWaitTimeout(sem, 0);
112}
113
114int
115SDL_SemWait(SDL_sem * sem)
116{
117    return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
118}
119
120/* Returns the current count of the semaphore */
121Uint32
122SDL_SemValue(SDL_sem * sem)
123{
124    int32 count;
125    Uint32 value;
126
127    value = 0;
128    if (sem) {
129        get_sem_count(sem->id, &count);
130        if (count > 0) {
131            value = (Uint32) count;
132        }
133    }
134    return value;
135}
136
137/* Atomically increases the semaphore's count (not blocking) */
138int
139SDL_SemPost(SDL_sem * sem)
140{
141    if (!sem) {
142        SDL_SetError("Passed a NULL semaphore");
143        return -1;
144    }
145
146    if (release_sem(sem->id) != B_NO_ERROR) {
147        SDL_SetError("release_sem() failed");
148        return -1;
149    }
150    return 0;
151}
152
153#endif /* SDL_THREAD_BEOS */
154
155/* vi: set ts=4 sw=4 expandtab: */