src/thread/pthread/SDL_syscond.c
author Sam Lantinga
Wed, 03 Oct 2012 19:26:07 -0700
changeset 6552 caa7decef32b
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rw-r--r--
Merged Ryan's changes
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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) {
slouken@1895
    70
        SDL_SetError("Passed a NULL condition variable");
slouken@1895
    71
        return -1;
slouken@1895
    72
    }
slouken@1361
    73
slouken@1895
    74
    retval = 0;
slouken@1895
    75
    if (pthread_cond_signal(&cond->cond) != 0) {
slouken@1895
    76
        SDL_SetError("pthread_cond_signal() failed");
slouken@1895
    77
        retval = -1;
slouken@1895
    78
    }
slouken@1895
    79
    return retval;
slouken@1361
    80
}
slouken@1361
    81
slouken@1361
    82
/* Restart all threads that are waiting on the condition variable */
slouken@1895
    83
int
slouken@1895
    84
SDL_CondBroadcast(SDL_cond * cond)
slouken@1361
    85
{
slouken@1895
    86
    int retval;
slouken@1361
    87
slouken@1895
    88
    if (!cond) {
slouken@1895
    89
        SDL_SetError("Passed a NULL condition variable");
slouken@1895
    90
        return -1;
slouken@1895
    91
    }
slouken@1361
    92
slouken@1895
    93
    retval = 0;
slouken@1895
    94
    if (pthread_cond_broadcast(&cond->cond) != 0) {
slouken@1895
    95
        SDL_SetError("pthread_cond_broadcast() failed");
slouken@1895
    96
        retval = -1;
slouken@1895
    97
    }
slouken@1895
    98
    return retval;
slouken@1361
    99
}
slouken@1361
   100
slouken@1895
   101
int
slouken@1895
   102
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
slouken@1361
   103
{
slouken@1895
   104
    int retval;
slouken@1895
   105
    struct timeval delta;
slouken@1895
   106
    struct timespec abstime;
slouken@1361
   107
slouken@1895
   108
    if (!cond) {
slouken@1895
   109
        SDL_SetError("Passed a NULL condition variable");
slouken@1895
   110
        return -1;
slouken@1895
   111
    }
slouken@1361
   112
slouken@1895
   113
    gettimeofday(&delta, NULL);
slouken@1361
   114
slouken@1895
   115
    abstime.tv_sec = delta.tv_sec + (ms / 1000);
slouken@1895
   116
    abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
slouken@1895
   117
    if (abstime.tv_nsec > 1000000000) {
slouken@1895
   118
        abstime.tv_sec += 1;
slouken@1895
   119
        abstime.tv_nsec -= 1000000000;
slouken@1895
   120
    }
slouken@1361
   121
slouken@1361
   122
  tryagain:
slouken@1895
   123
    retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
slouken@1895
   124
    switch (retval) {
slouken@1895
   125
    case EINTR:
slouken@1895
   126
        goto tryagain;
slouken@1895
   127
        break;
slouken@1895
   128
    case ETIMEDOUT:
slouken@1895
   129
        retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   130
        break;
slouken@1895
   131
    case 0:
slouken@1895
   132
        break;
slouken@1895
   133
    default:
slouken@1895
   134
        SDL_SetError("pthread_cond_timedwait() failed");
slouken@1895
   135
        retval = -1;
slouken@1895
   136
        break;
slouken@1895
   137
    }
slouken@1895
   138
    return retval;
slouken@1361
   139
}
slouken@1361
   140
slouken@1361
   141
/* Wait on the condition variable, unlocking the provided mutex.
slouken@1361
   142
   The mutex must be locked before entering this function!
slouken@1361
   143
 */
slouken@1895
   144
int
slouken@1895
   145
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
slouken@1361
   146
{
slouken@1895
   147
    int retval;
slouken@1895
   148
slouken@1895
   149
    if (!cond) {
slouken@1895
   150
        SDL_SetError("Passed a NULL condition variable");
slouken@1895
   151
        return -1;
slouken@1895
   152
    }
slouken@1361
   153
slouken@1895
   154
    retval = 0;
slouken@1895
   155
    if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
slouken@1895
   156
        SDL_SetError("pthread_cond_wait() failed");
slouken@1895
   157
        retval = -1;
slouken@1895
   158
    }
slouken@1895
   159
    return retval;
slouken@1895
   160
}
slouken@1361
   161
slouken@1895
   162
/* vi: set ts=4 sw=4 expandtab: */