src/thread/amigaos/SDL_syssem.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@21
    24
/* An implementation of semaphores using mutexes and condition variables */
slouken@0
    25
slouken@0
    26
#include "SDL_thread.h"
slouken@0
    27
#include "SDL_systhread_c.h"
slouken@0
    28
slouken@0
    29
slouken@0
    30
struct SDL_semaphore
slouken@0
    31
{
slouken@1662
    32
    struct SignalSemaphore Sem;
slouken@0
    33
};
slouken@0
    34
slouken@21
    35
#undef D
slouken@0
    36
slouken@0
    37
#define D(x)
slouken@0
    38
slouken@1662
    39
SDL_sem *
slouken@1668
    40
SDL_CreateSemaphore(Uint32 initial_value)
slouken@0
    41
{
slouken@1662
    42
    SDL_sem *sem;
slouken@0
    43
slouken@1668
    44
    sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
slouken@21
    45
slouken@1662
    46
    if (!sem) {
slouken@1668
    47
        SDL_OutOfMemory();
slouken@1662
    48
        return (0);
slouken@1662
    49
    }
slouken@0
    50
slouken@1668
    51
    D(bug("Creating semaphore %lx...\n", sem));
slouken@0
    52
slouken@1668
    53
    SDL_memset(sem, 0, sizeof(*sem));
slouken@21
    54
slouken@1668
    55
    InitSemaphore(&sem->Sem);
slouken@255
    56
slouken@1662
    57
    return (sem);
slouken@0
    58
}
slouken@0
    59
slouken@1662
    60
void
slouken@1668
    61
SDL_DestroySemaphore(SDL_sem * sem)
slouken@0
    62
{
slouken@1668
    63
    D(bug("Destroying semaphore %lx...\n", sem));
slouken@0
    64
slouken@1662
    65
    if (sem) {
slouken@0
    66
// Condizioni per liberare i task in attesa?
slouken@1668
    67
        SDL_free(sem);
slouken@1662
    68
    }
slouken@0
    69
}
slouken@0
    70
slouken@1662
    71
int
slouken@1668
    72
SDL_SemTryWait(SDL_sem * sem)
slouken@0
    73
{
slouken@1662
    74
    if (!sem) {
slouken@1668
    75
        SDL_SetError("Passed a NULL semaphore");
slouken@1662
    76
        return -1;
slouken@1662
    77
    }
slouken@0
    78
slouken@1668
    79
    D(bug("TryWait semaphore...%lx\n", sem));
slouken@0
    80
slouken@1668
    81
    ObtainSemaphore(&sem->Sem);
slouken@1662
    82
//      ReleaseSemaphore(&sem->Sem);
slouken@21
    83
slouken@1662
    84
    return 1;
slouken@0
    85
}
slouken@0
    86
slouken@1662
    87
int
slouken@1668
    88
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
slouken@0
    89
{
slouken@1662
    90
    int retval;
slouken@0
    91
slouken@21
    92
slouken@1662
    93
    if (!sem) {
slouken@1668
    94
        SDL_SetError("Passed a NULL semaphore");
slouken@1662
    95
        return -1;
slouken@1662
    96
    }
slouken@0
    97
slouken@1668
    98
    D(bug("WaitTimeout (%ld) semaphore...%lx\n", timeout, sem));
slouken@0
    99
slouken@1662
   100
    /* A timeout of 0 is an easy case */
slouken@1662
   101
    if (timeout == 0) {
slouken@1668
   102
        ObtainSemaphore(&sem->Sem);
slouken@1662
   103
        return 1;
slouken@1662
   104
    }
slouken@1668
   105
    if (!(retval = AttemptSemaphore(&sem->Sem))) {
slouken@1668
   106
        SDL_Delay(timeout);
slouken@1668
   107
        retval = AttemptSemaphore(&sem->Sem);
slouken@1662
   108
    }
slouken@0
   109
slouken@1662
   110
    if (retval == TRUE) {
slouken@1662
   111
//              ReleaseSemaphore(&sem->Sem);
slouken@1662
   112
        retval = 1;
slouken@1662
   113
    }
slouken@21
   114
slouken@1662
   115
    return retval;
slouken@0
   116
}
slouken@0
   117
slouken@1662
   118
int
slouken@1668
   119
SDL_SemWait(SDL_sem * sem)
slouken@0
   120
{
slouken@1668
   121
    ObtainSemaphore(&sem->Sem);
slouken@1662
   122
    return 0;
slouken@0
   123
}
slouken@0
   124
slouken@1662
   125
Uint32
slouken@1668
   126
SDL_SemValue(SDL_sem * sem)
slouken@0
   127
{
slouken@1662
   128
    Uint32 value;
slouken@255
   129
slouken@1662
   130
    value = 0;
slouken@1662
   131
    if (sem) {
slouken@1662
   132
#ifdef STORMC4_WOS
slouken@1662
   133
        value = sem->Sem.ssppc_SS.ss_NestCount;
slouken@1662
   134
#else
slouken@1662
   135
        value = sem->Sem.ss_NestCount;
slouken@1662
   136
#endif
slouken@1662
   137
    }
slouken@1662
   138
    return value;
slouken@0
   139
}
slouken@0
   140
slouken@1662
   141
int
slouken@1668
   142
SDL_SemPost(SDL_sem * sem)
slouken@0
   143
{
slouken@1662
   144
    if (!sem) {
slouken@1668
   145
        SDL_SetError("Passed a NULL semaphore");
slouken@1662
   146
        return -1;
slouken@1662
   147
    }
slouken@1668
   148
    D(bug("SemPost semaphore...%lx\n", sem));
slouken@0
   149
slouken@1668
   150
    ReleaseSemaphore(&sem->Sem);
slouken@1662
   151
    return 0;
slouken@0
   152
}
slouken@0
   153
slouken@1662
   154
/* vi: set ts=4 sw=4 expandtab: */