src/thread/pthread/SDL_syscond.c
author Philipp Wiesemann
Thu, 30 May 2013 12:23:36 +0200
changeset 7242 96f4133dfafa
parent 7037 3fedf1f25b94
child 8093 b43765095a6f
permissions -rw-r--r--
Added deactivated loading of SDL2_net to Java file.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1361
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1361
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1361
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1361
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@1361
    22
slouken@1361
    23
#include <sys/time.h>
slouken@1361
    24
#include <unistd.h>
slouken@1361
    25
#include <errno.h>
slouken@1361
    26
#include <pthread.h>
slouken@1361
    27
slouken@1361
    28
#include "SDL_thread.h"
slouken@1361
    29
#include "SDL_sysmutex_c.h"
slouken@1361
    30
slouken@1361
    31
struct SDL_cond
slouken@1361
    32
{
slouken@1895
    33
    pthread_cond_t cond;
slouken@1361
    34
};
slouken@1361
    35
slouken@1361
    36
/* Create a condition variable */
slouken@1895
    37
SDL_cond *
slouken@1895
    38
SDL_CreateCond(void)
slouken@1361
    39
{
slouken@1895
    40
    SDL_cond *cond;
slouken@1361
    41
slouken@1895
    42
    cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
slouken@1895
    43
    if (cond) {
slouken@1895
    44
        if (pthread_cond_init(&cond->cond, NULL) < 0) {
slouken@1895
    45
            SDL_SetError("pthread_cond_init() failed");
slouken@1895
    46
            SDL_free(cond);
slouken@1895
    47
            cond = NULL;
slouken@1895
    48
        }
slouken@1895
    49
    }
slouken@1895
    50
    return (cond);
slouken@1361
    51
}
slouken@1361
    52
slouken@1361
    53
/* Destroy a condition variable */
slouken@1895
    54
void
slouken@1895
    55
SDL_DestroyCond(SDL_cond * cond)
slouken@1361
    56
{
slouken@1895
    57
    if (cond) {
slouken@1895
    58
        pthread_cond_destroy(&cond->cond);
slouken@1895
    59
        SDL_free(cond);
slouken@1895
    60
    }
slouken@1361
    61
}
slouken@1361
    62
slouken@1361
    63
/* Restart one of the threads that are waiting on the condition variable */
slouken@1895
    64
int
slouken@1895
    65
SDL_CondSignal(SDL_cond * cond)
slouken@1361
    66
{
slouken@1895
    67
    int retval;
slouken@1361
    68
slouken@1895
    69
    if (!cond) {
icculus@7037
    70
        return SDL_SetError("Passed a NULL condition variable");
slouken@1895
    71
    }
slouken@1361
    72
slouken@1895
    73
    retval = 0;
slouken@1895
    74
    if (pthread_cond_signal(&cond->cond) != 0) {
icculus@7037
    75
        return SDL_SetError("pthread_cond_signal() failed");
slouken@1895
    76
    }
slouken@1895
    77
    return retval;
slouken@1361
    78
}
slouken@1361
    79
slouken@1361
    80
/* Restart all threads that are waiting on the condition variable */
slouken@1895
    81
int
slouken@1895
    82
SDL_CondBroadcast(SDL_cond * cond)
slouken@1361
    83
{
slouken@1895
    84
    int retval;
slouken@1361
    85
slouken@1895
    86
    if (!cond) {
icculus@7037
    87
        return SDL_SetError("Passed a NULL condition variable");
slouken@1895
    88
    }
slouken@1361
    89
slouken@1895
    90
    retval = 0;
slouken@1895
    91
    if (pthread_cond_broadcast(&cond->cond) != 0) {
icculus@7037
    92
        return SDL_SetError("pthread_cond_broadcast() failed");
slouken@1895
    93
    }
slouken@1895
    94
    return retval;
slouken@1361
    95
}
slouken@1361
    96
slouken@1895
    97
int
slouken@1895
    98
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
slouken@1361
    99
{
slouken@1895
   100
    int retval;
slouken@1895
   101
    struct timeval delta;
slouken@1895
   102
    struct timespec abstime;
slouken@1361
   103
slouken@1895
   104
    if (!cond) {
icculus@7037
   105
        return SDL_SetError("Passed a NULL condition variable");
slouken@1895
   106
    }
slouken@1361
   107
slouken@1895
   108
    gettimeofday(&delta, NULL);
slouken@1361
   109
slouken@1895
   110
    abstime.tv_sec = delta.tv_sec + (ms / 1000);
slouken@1895
   111
    abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
slouken@1895
   112
    if (abstime.tv_nsec > 1000000000) {
slouken@1895
   113
        abstime.tv_sec += 1;
slouken@1895
   114
        abstime.tv_nsec -= 1000000000;
slouken@1895
   115
    }
slouken@1361
   116
slouken@1361
   117
  tryagain:
slouken@1895
   118
    retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
slouken@1895
   119
    switch (retval) {
slouken@1895
   120
    case EINTR:
slouken@1895
   121
        goto tryagain;
slouken@1895
   122
        break;
slouken@1895
   123
    case ETIMEDOUT:
slouken@1895
   124
        retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   125
        break;
slouken@1895
   126
    case 0:
slouken@1895
   127
        break;
slouken@1895
   128
    default:
icculus@7037
   129
        retval = SDL_SetError("pthread_cond_timedwait() failed");
slouken@1895
   130
    }
slouken@1895
   131
    return retval;
slouken@1361
   132
}
slouken@1361
   133
slouken@1361
   134
/* Wait on the condition variable, unlocking the provided mutex.
slouken@1361
   135
   The mutex must be locked before entering this function!
slouken@1361
   136
 */
slouken@1895
   137
int
slouken@1895
   138
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
slouken@1361
   139
{
slouken@1895
   140
    if (!cond) {
icculus@7037
   141
        return SDL_SetError("Passed a NULL condition variable");
icculus@7037
   142
    } else if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
icculus@7037
   143
        return SDL_SetError("pthread_cond_wait() failed");
slouken@1895
   144
    }
icculus@7037
   145
    return 0;
slouken@1895
   146
}
slouken@1361
   147
slouken@1895
   148
/* vi: set ts=4 sw=4 expandtab: */