src/thread/pthread/SDL_syscond.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1361
    22
slouken@1361
    23
#include <sys/time.h>
slouken@9244
    24
#include <time.h>
slouken@1361
    25
#include <unistd.h>
slouken@1361
    26
#include <errno.h>
slouken@1361
    27
#include <pthread.h>
slouken@1361
    28
slouken@1361
    29
#include "SDL_thread.h"
slouken@1361
    30
#include "SDL_sysmutex_c.h"
slouken@1361
    31
slouken@1361
    32
struct SDL_cond
slouken@1361
    33
{
slouken@1895
    34
    pthread_cond_t cond;
slouken@1361
    35
};
slouken@1361
    36
slouken@1361
    37
/* Create a condition variable */
slouken@1895
    38
SDL_cond *
slouken@1895
    39
SDL_CreateCond(void)
slouken@1361
    40
{
slouken@1895
    41
    SDL_cond *cond;
slouken@1361
    42
slouken@1895
    43
    cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
slouken@1895
    44
    if (cond) {
slouken@1895
    45
        if (pthread_cond_init(&cond->cond, NULL) < 0) {
slouken@1895
    46
            SDL_SetError("pthread_cond_init() failed");
slouken@1895
    47
            SDL_free(cond);
slouken@1895
    48
            cond = NULL;
slouken@1895
    49
        }
slouken@1895
    50
    }
slouken@1895
    51
    return (cond);
slouken@1361
    52
}
slouken@1361
    53
slouken@1361
    54
/* Destroy a condition variable */
slouken@1895
    55
void
slouken@1895
    56
SDL_DestroyCond(SDL_cond * cond)
slouken@1361
    57
{
slouken@1895
    58
    if (cond) {
slouken@1895
    59
        pthread_cond_destroy(&cond->cond);
slouken@1895
    60
        SDL_free(cond);
slouken@1895
    61
    }
slouken@1361
    62
}
slouken@1361
    63
slouken@1361
    64
/* Restart one of the threads that are waiting on the condition variable */
slouken@1895
    65
int
slouken@1895
    66
SDL_CondSignal(SDL_cond * cond)
slouken@1361
    67
{
slouken@1895
    68
    int retval;
slouken@1361
    69
slouken@1895
    70
    if (!cond) {
icculus@7037
    71
        return SDL_SetError("Passed a NULL condition variable");
slouken@1895
    72
    }
slouken@1361
    73
slouken@1895
    74
    retval = 0;
slouken@1895
    75
    if (pthread_cond_signal(&cond->cond) != 0) {
icculus@7037
    76
        return SDL_SetError("pthread_cond_signal() failed");
slouken@1895
    77
    }
slouken@1895
    78
    return retval;
slouken@1361
    79
}
slouken@1361
    80
slouken@1361
    81
/* Restart all threads that are waiting on the condition variable */
slouken@1895
    82
int
slouken@1895
    83
SDL_CondBroadcast(SDL_cond * cond)
slouken@1361
    84
{
slouken@1895
    85
    int retval;
slouken@1361
    86
slouken@1895
    87
    if (!cond) {
icculus@7037
    88
        return SDL_SetError("Passed a NULL condition variable");
slouken@1895
    89
    }
slouken@1361
    90
slouken@1895
    91
    retval = 0;
slouken@1895
    92
    if (pthread_cond_broadcast(&cond->cond) != 0) {
icculus@7037
    93
        return SDL_SetError("pthread_cond_broadcast() failed");
slouken@1895
    94
    }
slouken@1895
    95
    return retval;
slouken@1361
    96
}
slouken@1361
    97
slouken@1895
    98
int
slouken@1895
    99
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
slouken@1361
   100
{
slouken@1895
   101
    int retval;
slouken@9244
   102
#ifndef HAVE_CLOCK_GETTIME
slouken@1895
   103
    struct timeval delta;
slouken@9244
   104
#endif
slouken@1895
   105
    struct timespec abstime;
slouken@1361
   106
slouken@1895
   107
    if (!cond) {
icculus@7037
   108
        return SDL_SetError("Passed a NULL condition variable");
slouken@1895
   109
    }
slouken@1361
   110
slouken@9244
   111
#ifdef HAVE_CLOCK_GETTIME
slouken@9244
   112
    clock_gettime(CLOCK_REALTIME, &abstime);
slouken@9244
   113
slouken@9244
   114
    abstime.tv_nsec += (ms % 1000) * 1000000;
slouken@9244
   115
    abstime.tv_sec += ms / 1000;
slouken@9244
   116
#else
slouken@1895
   117
    gettimeofday(&delta, NULL);
slouken@1361
   118
slouken@1895
   119
    abstime.tv_sec = delta.tv_sec + (ms / 1000);
slouken@1895
   120
    abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
slouken@9244
   121
#endif
slouken@1895
   122
    if (abstime.tv_nsec > 1000000000) {
slouken@1895
   123
        abstime.tv_sec += 1;
slouken@1895
   124
        abstime.tv_nsec -= 1000000000;
slouken@1895
   125
    }
slouken@1361
   126
slouken@1361
   127
  tryagain:
slouken@1895
   128
    retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
slouken@1895
   129
    switch (retval) {
slouken@1895
   130
    case EINTR:
slouken@1895
   131
        goto tryagain;
slouken@10609
   132
        /* break; -Wunreachable-code-break */
slouken@1895
   133
    case ETIMEDOUT:
slouken@1895
   134
        retval = SDL_MUTEX_TIMEDOUT;
slouken@1895
   135
        break;
slouken@1895
   136
    case 0:
slouken@1895
   137
        break;
slouken@1895
   138
    default:
icculus@7037
   139
        retval = SDL_SetError("pthread_cond_timedwait() failed");
slouken@1895
   140
    }
slouken@1895
   141
    return retval;
slouken@1361
   142
}
slouken@1361
   143
slouken@1361
   144
/* Wait on the condition variable, unlocking the provided mutex.
slouken@1361
   145
   The mutex must be locked before entering this function!
slouken@1361
   146
 */
slouken@1895
   147
int
slouken@1895
   148
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
slouken@1361
   149
{
slouken@1895
   150
    if (!cond) {
icculus@7037
   151
        return SDL_SetError("Passed a NULL condition variable");
icculus@7037
   152
    } else if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
icculus@7037
   153
        return SDL_SetError("pthread_cond_wait() failed");
slouken@1895
   154
    }
icculus@7037
   155
    return 0;
slouken@1895
   156
}
slouken@1361
   157
slouken@1895
   158
/* vi: set ts=4 sw=4 expandtab: */